package com.cf.imes.module.executor.service.veneer;

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.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

/**
 * 贴皮excel模版分类名称填充处理器
 *
 * @author Gqr
 * @since 2026/1/22 18:50
 */
public class VeneerExcelTemplateClassificationNameWriteHandler implements SheetWriteHandler {
    private final int columnIndex; // 0-based，C 列 = 2
    private final String[] options;
    private final int firstRow;
    private final int lastRow;

    public VeneerExcelTemplateClassificationNameWriteHandler(int columnIndex,
                                                             String[] options,
                                                             int firstRow,
                                                             int lastRow) {
        this.columnIndex = columnIndex;
        this.options = options;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder workbookHolder,
                                 WriteSheetHolder sheetHolder) {

        Sheet sheet = sheetHolder.getSheet();

        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint =
                helper.createExplicitListConstraint(options);

        CellRangeAddressList addressList =
                new CellRangeAddressList(firstRow, lastRow, columnIndex, columnIndex);

        DataValidation validation =
                helper.createValidation(constraint, addressList);

        validation.setShowErrorBox(true);
        validation.setSuppressDropDownArrow(true);

        sheet.addValidationData(validation);
    }
}
