package com.yc.ship.module.trade.utils.excel;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

/**
 * 长江行游轮游客数据及营收统计 导出样式处理器
 * 处理多级表头合并
 */
public class YangtzePassengerSummaryExportStyleHandler implements CellWriteHandler {

    /** 缓存样式 */
    private CellStyle headerStyle;

    public YangtzePassengerSummaryExportStyleHandler() {
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        Sheet sheet = context.getWriteSheetHolder().getSheet();

        // 表头样式处理（EasyExcel 会自动处理多级表头合并，无需手动合并）
        if (Boolean.TRUE.equals(context.getHead())) {
            applyHeaderStyle(cell, sheet.getWorkbook());
        }
    }

    /**
     * 表头样式
     */
    private void applyHeaderStyle(Cell cell, Workbook workbook) {
        if (headerStyle == null) {
            headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setBorderTop(BorderStyle.THIN);
            headerStyle.setBorderBottom(BorderStyle.THIN);
            headerStyle.setBorderLeft(BorderStyle.THIN);
            headerStyle.setBorderRight(BorderStyle.THIN);

            if (headerStyle instanceof XSSFCellStyle) {
                XSSFColor headerColor = new XSSFColor(new byte[]{(byte) 0x4A, (byte) 0x90, (byte) 0xD9}, null);
                ((XSSFCellStyle) headerStyle).setFillForegroundColor(headerColor);
            }

            Font font = workbook.createFont();
            font.setBold(true);
            font.setFontHeightInPoints((short) 11);
            font.setColor(IndexedColors.WHITE.getIndex());
            headerStyle.setFont(font);
        }
        cell.setCellStyle(headerStyle);
    }


}
