package com.mortals.xhx.common.excel.core;

import lombok.SneakyThrows;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @描述
 * @创建人 zxfei
 * @创建时间 2021/3/4
 * @修改人和其它信息
 */
public abstract class AbstractExcelUtils<T> extends AbstractExcelUtil<T> {

    @SneakyThrows
    @Override
    public List<T> importExcel(String sheetName, String fileName, InputStream input) {
        List<T> list = null;
        try {
            Workbook workbook = WorkbookFactory.create(input);
            Sheet sheet = workbook.getSheetAt(0);
            list = dispatch(sheet);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean exportExcel(List<T> list, String sheetName, OutputStream output, Map<String, String> configFieldMap) {
        //此处 对类型进行转换
        List<T> ilist = new ArrayList<>();
        for (T t : list) {
            ilist.add(t);
        }
        List<T>[] lists = new ArrayList[1];
        lists[0] = ilist;

        String[] sheetNames = new String[1];
        sheetNames[0] = sheetName;
        return exportExcel(lists, sheetNames, output, configFieldMap);
    }

    @Override
    public boolean exportExcel(List<T>[] lists, String[] sheetNames, OutputStream output, Map<String, String> configFieldMap) {
        if (lists.length != sheetNames.length) {
            System.out.println("数组长度不一致");
            return false;
        }

        // 创建excel工作簿
        HSSFWorkbook wb = ExcelTool.createWorkbook();
        // 创建第一个sheet（页），命名为 new sheet
        for (int i = 0; i < lists.length; i++) {
            List<T> list = lists[i];
            ExcelTool.setNum(0);
            // 产生工作表对象
            HSSFSheet sheet = ExcelTool.createSheet(i, sheetNames[i]);
            // 创建表头
            createHeader(wb, sheet, configFieldMap);
            // 写入数据
            int rowStart = 1;
            for (T t : list) {
                createRow(wb, t, sheet);
                ExcelTool.setNum(ExcelTool.getNum() + 1);
                //  rowStart = mergedRegio(t, sheet, rowStart);
            }

        }
        try {
            output.flush();
            wb.write(output);
            output.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Output is closed ");
            return false;
        }

    }


    @SuppressWarnings("unchecked")
    @Override
    public boolean exportExcelFlat(List<T> list, String sheetName, OutputStream output, Map<String, String> configFieldMap) {
        //此处 对类型进行转换
        List<T> ilist = new ArrayList<>();
        for (T t : list) {
            ilist.add(t);
        }
        List<T>[] lists = new ArrayList[1];
        lists[0] = ilist;

        String[] sheetNames = new String[1];
        sheetNames[0] = sheetName;
        return exportExcelFlat(lists, sheetNames, output, configFieldMap);
    }

    @Override
    public boolean exportExcelFlat(List<T>[] lists, String[] sheetNames, OutputStream output, Map<String, String> configFieldMap) {
        if (lists.length != sheetNames.length) {
            System.out.println("数组长度不一致");
            return false;
        }
        //这里创建工作簿和sheet 构建初始化的模板
        // 创建excel工作簿
        HSSFWorkbook wb = ExcelTool.createWorkbook();
        List<T> list = lists[0];
        // 产生工作表对象及其表名
        HSSFSheet sheet = ExcelTool.createSheet(0, sheetNames[0]);
        ExcelTool.setNum(0);

        // 创建第一行与第二行相关模板数据
        createHeaderFlat(wb, sheet, configFieldMap);

        //创建第三行表调数据
        ExcelTool.setNum(2);
        // 创建表头
        createHeader(wb, sheet, configFieldMap);
        // 填充写入数据

        for (T t : list) {
            //填充第一行与第二行数据
            createRowFlat(wb, t, sheet);
            //填充第三行以后数据
            createRow(wb, t, sheet);
            ExcelTool.setNum(ExcelTool.getNum() + 1);

        }


//
//        // 创建excel工作簿
//        HSSFWorkbook wb = ExcelTool.createWorkbook();
//        // 创建第一个sheet（页），命名为 new sheet
//        for (int i = 0; i < lists.length; i++) {
//            List<T> list = lists[i];
//            ExcelTool.setNum(2);
//            // 产生工作表对象
//            HSSFSheet sheet = ExcelTool.createSheet(i, sheetNames[i]);
//            // 创建表头
//            createHeader(wb, sheet, configFieldMap);
//            // 写入数据
//            for (T t : list) {
//                createRow(wb, t, sheet);
//                ExcelTool.setNum(ExcelTool.getNum() + 1);
//
//            }
//
//        }
        try {
            output.flush();
            wb.write(output);
            output.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Output is closed ");
            return false;
        }

    }
}
