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

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.yc.ship.module.trade.controller.admin.report.vo.VoyageStockBoardRespVO;
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 VoyageStockBoardExportStyleHandler implements SheetWriteHandler, CellWriteHandler {

    private static final int TOTAL_COLUMNS = 12;

    private final VoyageStockBoardRespVO summary;

    private CellStyle headerStyle;
    private CellStyle summaryLabelStyle;
    private CellStyle summaryValueStyle;
    private CellStyle titleStyle;

    public VoyageStockBoardExportStyleHandler(VoyageStockBoardRespVO summary) {
        this.summary = summary;
    }

    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        Sheet sheet = context.getWriteSheetHolder().getSheet();
        Workbook workbook = sheet.getWorkbook();

        // 第0行：航次名称标题
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(summary.getVoyageName() != null ? summary.getVoyageName() : "");
        titleCell.setCellStyle(createTitleStyle(workbook));
        // 合并标题行
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, TOTAL_COLUMNS - 1));

        // 第1行：该航次总库存
        Row row1 = sheet.createRow(1);
        Cell label1 = row1.createCell(0);
        label1.setCellValue("该航次总库存");
        label1.setCellStyle(createSummaryLabelStyle(workbook));
        Cell val1 = row1.createCell(1);
        val1.setCellValue(summary.getTotalStock() != null ? summary.getTotalStock().toString() : "0");
        val1.setCellStyle(createSummaryValueStyle(workbook));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, TOTAL_COLUMNS - 1));

        // 第2行：该航次已售库存
        Row row2 = sheet.createRow(2);
        Cell label2 = row2.createCell(0);
        label2.setCellValue("该航次已售库存");
        label2.setCellStyle(createSummaryLabelStyle(workbook));
        Cell val2 = row2.createCell(1);
        val2.setCellValue(summary.getSoldStock() != null ? summary.getSoldStock().toString() : "0");
        val2.setCellStyle(createSummaryValueStyle(workbook));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, TOTAL_COLUMNS - 1));

        // 第3行：该航次剩余库存
        Row row3 = sheet.createRow(3);
        Cell label3 = row3.createCell(0);
        label3.setCellValue("该航次剩余库存");
        label3.setCellStyle(createSummaryLabelStyle(workbook));
        Cell val3 = row3.createCell(1);
        val3.setCellValue(summary.getRemainingStock() != null ? summary.getRemainingStock().toString() : "0");
        val3.setCellStyle(createSummaryValueStyle(workbook));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, TOTAL_COLUMNS - 1));
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        if (Boolean.TRUE.equals(context.getHead())) {
            applyHeaderStyle(context.getCell(), context.getWriteSheetHolder().getSheet().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);
    }

    private CellStyle createTitleStyle(Workbook workbook) {
        if (titleStyle == null) {
            titleStyle = workbook.createCellStyle();
            titleStyle.setAlignment(HorizontalAlignment.CENTER);
            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            if (titleStyle instanceof XSSFCellStyle) {
                XSSFColor color = new XSSFColor(new byte[]{(byte) 0xE8, (byte) 0xF4, (byte) 0xF8}, null);
                ((XSSFCellStyle) titleStyle).setFillForegroundColor(color);
            }
            Font font = workbook.createFont();
            font.setBold(true);
            font.setFontHeightInPoints((short) 12);
            titleStyle.setFont(font);
        }
        return titleStyle;
    }

    private CellStyle createSummaryLabelStyle(Workbook workbook) {
        if (summaryLabelStyle == null) {
            summaryLabelStyle = workbook.createCellStyle();
            summaryLabelStyle.setAlignment(HorizontalAlignment.LEFT);
            summaryLabelStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            summaryLabelStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            if (summaryLabelStyle instanceof XSSFCellStyle) {
                XSSFColor color = new XSSFColor(new byte[]{(byte) 0xF2, (byte) 0xF2, (byte) 0xF2}, null);
                ((XSSFCellStyle) summaryLabelStyle).setFillForegroundColor(color);
            }
            Font font = workbook.createFont();
            font.setBold(true);
            summaryLabelStyle.setFont(font);
        }
        return summaryLabelStyle;
    }

    private CellStyle createSummaryValueStyle(Workbook workbook) {
        if (summaryValueStyle == null) {
            summaryValueStyle = workbook.createCellStyle();
            summaryValueStyle.setAlignment(HorizontalAlignment.CENTER);
            summaryValueStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            summaryValueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            if (summaryValueStyle instanceof XSSFCellStyle) {
                XSSFColor color = new XSSFColor(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, null);
                ((XSSFCellStyle) summaryValueStyle).setFillForegroundColor(color);
            }
        }
        return summaryValueStyle;
    }
}
