package com.dk.mdm.model.report;

import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

public class ExcelExportStyleImpl extends ExcelExportStylerDefaultImpl {

    private static final String DEFAULT_BACKGROUND_COLOR = "#C1C1C1";

    /* 起个名叫title其实是header */
    @Override
    public CellStyle getTitleStyle(short color) {
        CellStyle titleStyle = this.workbook.createCellStyle();
        Font font = this.workbook.createFont();
        font.setBold(true);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        titleStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        titleStyle.setFillBackgroundColor(IndexedColors.PALE_BLUE.getIndex());
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        titleStyle.setWrapText(true);
        titleStyle.setFont(font);
        titleStyle.setBorderBottom(BorderStyle.NONE);
        return titleStyle;
    }

    /* 起个名叫header其实是title */
    @Override
    public CellStyle getHeaderStyle(short color) {
        CellStyle titleStyle = this.workbook.createCellStyle();
        Font font = this.workbook.createFont();
        font.setFontHeightInPoints((short)12);
        titleStyle.setFont(font);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return titleStyle;
    }


    @Override
    public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {


        int r = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(1,3)),16);
        int g = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(3,5)),16);
        int b = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(5,7)),16);

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFPalette palette = wb.getCustomPalette();
        HSSFColor hssfColor = palette.findSimilarColor(r, g, b);


        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setDataFormat(STRING_FORMAT);
        style.setFillForegroundColor(hssfColor.getIndex());
        style.setFillBackgroundColor(hssfColor.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.WHITE.getIndex());
        style.setRightBorderColor(IndexedColors.WHITE.getIndex());
        if (isWarp) {
            style.setWrapText(true);
        }

        return style;
    }

    public ExcelExportStyleImpl(Workbook workbook) {
        super(workbook);
    }



}
