package cn.iocoder.yudao.module.bpm.service.financialpayment;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

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

public class CustomCellStyleHandler implements CellWriteHandler {

    private final Map<String, CellStyle> styleCache = new HashMap<>();

    @Override
    public void afterCellDispose(
            WriteSheetHolder writeSheetHolder,
            WriteTableHolder writeTableHolder,
            List<WriteCellData<?>> cellDataList,
            Cell cell,
            Head head,
            Integer relativeRowIndex,
            Boolean isHead) {

        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), 7120);

        // 获取或创建样式
        CellStyle cellStyle = getOrCreateCellStyle(workbook, isHead, relativeRowIndex, cellDataList);

        cell.setCellStyle(cellStyle);
    }

    private CellStyle getOrCreateCellStyle(Workbook workbook, Boolean isHead, Integer relativeRowIndex, List<WriteCellData<?>> cellDataList) {
        String key = generateStyleKey(isHead, relativeRowIndex);
        if (styleCache.containsKey(key)) {
            return styleCache.get(key);
        }

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

//        if (isHead != null && isHead) {
//            setHeaderStyle(workbook, cellStyle, relativeRowIndex, cellDataList);
//        }

        styleCache.put(key, cellStyle);
        return cellStyle;
    }

    private void setHeaderStyle(Workbook workbook, CellStyle cellStyle, Integer relativeRowIndex, List<WriteCellData<?>> cellDataList) {
        if (relativeRowIndex == 0 || relativeRowIndex == 1) {
            applyStyle(workbook, cellStyle, IndexedColors.TURQUOISE.getIndex(), (short) 20, cellDataList);
        } else if (relativeRowIndex == 2 || relativeRowIndex == 3) {
            applyStyle(workbook, cellStyle, IndexedColors.LIGHT_YELLOW.getIndex(), (short) 20, cellDataList);
        }
    }


    private void applyStyle(Workbook workbook, CellStyle cellStyle, short backgroundColor, short fontSize, List<WriteCellData<?>> cellDataList) {
        cellStyle.setFillForegroundColor(backgroundColor);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        Font font = workbook.createFont();
        font.setFontHeightInPoints(fontSize);
        cellStyle.setFont(font);
        cellDataList.get(0).setWriteCellStyle(null);
    }

    private String generateStyleKey(Boolean isHead, Integer relativeRowIndex) {
        return "isHead:" + isHead + ",rowIndex:" + relativeRowIndex;
    }
}
