package ${basePackage}.frame.excel.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import ${basePackage}.frame.excel.WHead;
import ${basePackage}.frame.utils.StringUtil;

import java.util.List;
import java.util.Map;

public class WRowWriteHandler implements RowWriteHandler {
    private List<WHead> wHeads;
    private Map<Integer, List<String>> errMap;

    public WRowWriteHandler(List<WHead> wHeads, Map<Integer, List<String>> errMap) {
        this.wHeads = wHeads;
        this.errMap = errMap;
    }

    @Override
    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean isHead) {

    }

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean isHead) {

    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Boolean isHead) {
        if (isHead && getHeadMaxRow() == rowIndex) {
            for (int i = 0; i < wHeads.size(); i++) {
                WHead wHead = wHeads.get(i);
                if (StringUtil.isNotEmpty(wHead.getNote())) {
                    Drawing<?> drawingPatriarch = writeSheetHolder.getSheet().createDrawingPatriarch();
                    // 创建一个批注
                    int headRow = getHeadRow(wHead.getHead());
                    Comment comment = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) i, headRow, (short) i + 1, headRow + 3));
                    // 输入批注信息
                    comment.setString(new XSSFRichTextString(wHead.getNote()));
                    // 将批注添加到标题对象中
                    writeSheetHolder.getSheet().getRow(headRow).getCell(i).setCellComment(comment);
                }
            }
        }

        if (!isHead && errMap.get(rowIndex) != null) {
            Cell cell = row.createCell(wHeads.size());
            cell.getCellStyle().setWrapText(true);
            List<String> errs = errMap.get(rowIndex);
            cell.setCellValue(String.join("\r\n", errs));
        }
    }

    public int getHeadMaxRow() {
        int r = 0;
        for (WHead wHead : wHeads) {
            if (wHead.getHead().getHeadNameList().size() - 1 > r) r = wHead.getHead().getHeadNameList().size() - 1;
        }
        return r;
    }

    public int getHeadRow(Head head) {
        List<String> nameList = head.getHeadNameList();
        if (nameList.size() == 1) {
            return 0;
        }
        int r = nameList.size() - 1;
        String last = nameList.get(r);
        for (int i = nameList.size() - 2; i >= 0; i--) {
            String s = nameList.get(i);
            if (s.equals(last)) {
                r--;
            }
        }
        return r;
    }
}
