package com.engine.organization.util.excel;

import com.engine.organization.util.OrganizationDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;

import java.util.Date;
import java.util.List;

public class ExcelUtil {
    /**
     * 生成excel
     *
     * @param rowList
     * @return
     */
    public static XSSFWorkbook genWorkbook(List<List<String>> rowList, String sheetName) {
        XSSFWorkbook workbook = new XSSFWorkbook();

        // 设置title样式
        XSSFCellStyle titleCellStyle = workbook.createCellStyle();
        XSSFFont titleFont = workbook.createFont();
        titleFont.setFontName("仿宋");
        titleFont.setFontHeightInPoints((short) 15);
        titleCellStyle.setFont(titleFont);
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
        titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);


        // 设置主体样式
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);// 设置字体大小
        cellStyle.setFont(font);// 选择需要用到的字体格式
        cellStyle.setWrapText(true);

        XSSFSheet sheet = workbook.createSheet(sheetName);
        //自适应宽度
        sheet.autoSizeColumn(0, true);
        //默认列宽
        sheet.setDefaultColumnWidth(40);
        //默认行高
        sheet.setDefaultRowHeightInPoints(18);

        for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
            List<String> infoList = rowList.get(rowIndex);
            XSSFRow row = sheet.createRow(rowIndex);
            for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
                XSSFCell cell = row.createCell(cellIndex);
                cell.setCellType(CellType.STRING);
                if (rowIndex == 0) {
                    cell.setCellStyle(titleCellStyle);
                } else {
                    cell.setCellStyle(cellStyle);
                }
                cell.setCellValue(infoList.get(cellIndex));
//                sheet.setColumnWidth(cellIndex, 35 * 256);
            }
        }
        return workbook;
    }


    public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName) {
        XSSFWorkbook workbook = new XSSFWorkbook();

        // 设置title样式
        XSSFCellStyle titleCellStyle = workbook.createCellStyle();
        XSSFFont titleFont = workbook.createFont();
        titleFont.setFontName("仿宋");
        titleFont.setFontHeightInPoints((short) 15);
        titleCellStyle.setFont(titleFont);
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
        titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);


        // 设置主体样式
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);// 设置字体大小
        cellStyle.setFont(font);// 选择需要用到的字体格式
        cellStyle.setWrapText(true);

        XSSFSheet sheet = workbook.createSheet(sheetName);
        //自适应宽度
        sheet.autoSizeColumn(0, true);
        //默认列宽
        sheet.setDefaultColumnWidth(25);
        //默认行高
        sheet.setDefaultRowHeightInPoints(18);

        for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
            List<Object> infoList = rowList.get(rowIndex);
            XSSFRow row = sheet.createRow(rowIndex);
            for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
                XSSFCell cell = row.createCell(cellIndex);
                if (rowIndex == 0) {
                    cell.setCellStyle(titleCellStyle);
                } else {
                    cell.setCellStyle(cellStyle);
                }
                Object o = infoList.get(cellIndex);
                if (o instanceof String) {
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue(String.valueOf(o));
                } else if (o instanceof Boolean) {
                    cell.setCellType(CellType.BOOLEAN);
                    cell.setCellValue(String.valueOf(o));
                } else if (o instanceof Date) {
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue(OrganizationDateUtil.getFormatLocalDate((Date) o));
                } else {
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue(o == null ? "" : o.toString());
                }
            }
        }
        return workbook;
    }


    public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName, List<ExcelComment> comments) {
        XSSFWorkbook workbook = new XSSFWorkbook();

        // 设置title样式
        XSSFCellStyle titleCellStyle = workbook.createCellStyle();
        XSSFFont titleFont = workbook.createFont();
        titleFont.setFontName("仿宋");
        titleFont.setFontHeightInPoints((short) 15);
        titleCellStyle.setFont(titleFont);
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
        titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);


        // 设置主体样式
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);// 设置字体大小
        cellStyle.setFont(font);// 选择需要用到的字体格式
        cellStyle.setWrapText(true);

        XSSFSheet sheet = workbook.createSheet(sheetName);
        //自适应宽度
        sheet.autoSizeColumn(0, true);
        //默认列宽
        sheet.setDefaultColumnWidth(20);
        //默认行高
        sheet.setDefaultRowHeightInPoints(18);

        for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
            List<Object> infoList = rowList.get(rowIndex);
            XSSFRow row = sheet.createRow(rowIndex);
            for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
                XSSFCell cell = row.createCell(cellIndex);
                if (rowIndex == 0) {
                    cell.setCellStyle(titleCellStyle);
                } else {
                    cell.setCellStyle(cellStyle);
                }
                Object o = infoList.get(cellIndex);
                if (o instanceof String) {
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue(String.valueOf(o));
                } else if (o instanceof Boolean) {
                    cell.setCellType(CellType.BOOLEAN);
                    cell.setCellValue(String.valueOf(o));
                } else if (o instanceof Date) {
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue(OrganizationDateUtil.getFormatLocalDate((Date) o));
                } else {
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue(String.valueOf(o));
                }
            }
        }

        if (CollectionUtils.isNotEmpty(comments)) {
            for (ExcelComment c : comments) {
                XSSFDrawing patr = sheet.createDrawingPatriarch();
                XSSFComment cellComment = patr.createCellComment(new XSSFClientAnchor(c.dx1, c.dy1, c.dx2, c.dy2, c.col1, c.row1, c.col2, c.row2));
                cellComment.setString(c.content);
            }
        }
        return workbook;
    }
}
