package cn.iocoder.yudao.module.server.controller.admin.rectifyinforeport;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;

public class RectifyExcelStyleHandler implements SheetWriteHandler {

    private static final int[] COLUMN_WIDTHS = {25, 15, 15, 15, 15, 15}; // 列宽（字符）
    private static final short HEADER_ROW_HEIGHT = 800; // 表头行高（点）
    private static final short DATA_ROW_HEIGHT = 600;   // 数据行高（点）

    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();

        // 设置列宽
        for (int i = 0; i < COLUMN_WIDTHS.length; i++) {
            sheet.setColumnWidth(i, COLUMN_WIDTHS[i] * 256); // 256 是 Excel 默认单位
        }

        // 设置行高（注意：这里只能设置“模板”行高，非数据内容）
        Row headerRow = sheet.getRow(0);
        if (headerRow != null) {
            headerRow.setHeight(HEADER_ROW_HEIGHT);
        }
    }

    public void sheet(int sheetIndex, Sheet sheet) {
        // 强制设置列宽
        for (int i = 0; i < COLUMN_WIDTHS.length; i++) {
            sheet.setColumnWidth(i, COLUMN_WIDTHS[i] * 300); // 使用更大的乘数
        }

        // 强制设置行高
        for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            if (row == null) continue;

            if (rowNum == 0) {
                // 表头行
                row.setHeight(HEADER_ROW_HEIGHT);
                applyHeaderStyle(sheet.getWorkbook(), row);
            } else {
                // 数据行
                row.setHeight(DATA_ROW_HEIGHT);
                applyDataStyle(sheet.getWorkbook(), row);
            }
        }
    }

    private void applyHeaderStyle(Workbook workbook, Row row) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();

        // 样式设置
        style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        // 字体设置
        font.setBold(true);
        font.setFontHeightInPoints((short) 14);
        font.setColor(IndexedColors.WHITE.getIndex());

        style.setFont(font);

        // 应用样式到所有单元格
        for (Cell cell : row) {
            cell.setCellStyle(style);
        }
    }

    private void applyDataStyle(Workbook workbook, Row row) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();

        // 样式设置
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);

        // 字体设置
        font.setFontHeightInPoints((short) 12);

        style.setFont(font);

        // 应用样式到所有单元格
        for (Cell cell : row) {
            cell.setCellStyle(style);
        }
    }
}