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


import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddressList;
import ${basePackage}.frame.excel.WHead;

import java.util.List;

public class WSheetWriteHandler implements SheetWriteHandler {
    private List<WHead> wHeads;
    private CellStyle[] styles;

    public WSheetWriteHandler(List<WHead> wHeads, CellStyle[] styles) {
        this.wHeads = wHeads;
        this.styles = styles;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        {
            styles[1] = writeWorkbookHolder.getWorkbook().createCellStyle();
            styles[1].setFillForegroundColor(IndexedColors.RED.getIndex());
            styles[1].setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }

        {
            styles[0] = writeWorkbookHolder.getWorkbook().createCellStyle();
            styles[0].setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            styles[0].setFillPattern(FillPatternType.SOLID_FOREGROUND);
            styles[0].setBorderBottom(BorderStyle.THIN);
            styles[0].setBorderLeft(BorderStyle.THIN);
            styles[0].setBorderRight(BorderStyle.THIN);
            styles[0].setBorderTop(BorderStyle.THIN);
            styles[0].setAlignment(HorizontalAlignment.CENTER);
            Font font = writeWorkbookHolder.getWorkbook().createFont();
            font.setColor(IndexedColors.BLACK.getIndex());
            font.setFontHeightInPoints((short) 12);
            font.setBold(true);
            styles[0].setFont(font);
        }
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        for (int i = 0; i < wHeads.size(); i++) {
            WHead col = wHeads.get(i);
            if (col.getSelectList() != null && col.getSelectList().length > 0) {
                CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(0, 500, i, i);
                DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper();
                DataValidationConstraint constraint = helper.createExplicitListConstraint(col.getSelectList());
                DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
                writeSheetHolder.getSheet().addValidationData(dataValidation);
            }
        }
    }
}
