package com.xdap.self_development.utils;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
// import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

@Slf4j
// @Logger
public class DescriptionWriteUtils implements SheetWriteHandler {
    // 字段说明数组
    private final String[] descriptions;

    public DescriptionWriteUtils(String[] descriptions) {
        this.descriptions = descriptions;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        Workbook workbook = writeWorkbookHolder.getWorkbook();

        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setColor(IndexedColors.GREY_50_PERCENT.getIndex()); // 灰色字体
        style.setFont(font);
        style.setWrapText(true);

        log.info("Creating description row for sheet: {}", writeSheetHolder.getSheetName());
        // 获取表头行的下一行
        Row descRow = sheet.createRow(1);
        // 填充每个字段的说明
        for (int i = 0; i < descriptions.length; i++) {
            Cell cell = descRow.createCell(i);
            cell.setCellValue(descriptions[i]);
            cell.setCellStyle(style);
            log.info("Set cell value to: {}", descriptions[i]);
        }
    }
}