package com.njyunzhi.pet_identity.common;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFColor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ExcelUtils {

//    public static void fill(HttpServletResponse response, String tpl, List data, Map<String, Object> otherData, String fileName) throws IOException {
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName)+".xlsx");
//
//        ClassPathResource classPathResource = new ClassPathResource(tpl);
//        InputStream tplStream =classPathResource.getInputStream();
//
//        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(tplStream).build();
//        WriteSheet writeSheet = EasyExcel.writerSheet().build();
//        // 写入内容部分
//        for(Object it: data) {
//            excelWriter.fill(it, writeSheet);
//        }
//        // 写入其余部分
//        excelWriter.fill(otherData, writeSheet);
//        excelWriter.finish();
//    }

    public static XSSFColor color(int r, int g, int b) {
        return new XSSFColor(new java.awt.Color(r, g, b), new DefaultIndexedColorMap());
    }

    public static HorizontalCellStyleStrategy defaultStyle() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景
        headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("微软雅黑");
        headWriteFont.setFontHeightInPoints((short)14);
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontName("微软雅黑");
        contentWriteFont.setFontHeightInPoints((short)12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);   // 下边框
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);     // 左边框
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);      // 上边框
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);    // 右边框
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

    /**
     * 发送 excel 到客户端
     * 暂时只支持单 sheet 页
     * @param response
     * @param data
     * @param fileName
     * @throws IOException
     */
    public static void flush(HttpServletResponse response, Class dataClass, List data, String fileName, HorizontalCellStyleStrategy style) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName)+".xlsx");
            ExcelWriterBuilder builder = EasyExcel.write(response.getOutputStream(), dataClass);
            if (null == style) {
                style = defaultStyle();
                builder.registerWriteHandler(style);
            }
            builder.autoCloseStream(Boolean.FALSE).sheet("sheet1").doWrite(data);
        } catch (Exception e) {
            e.printStackTrace();

            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            ResponseBean res = ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
            response.getWriter().println(JSON.toJSONString(res));
        }

    }
}
