package com.wgb.system.service.component;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ExcelUtils {

    public static boolean isExcel2003(String filePath) {
        try {
            return isExcel2003(new File(filePath));
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isExcel2003(File file) {
        try {
            return isExcel2003(new FileInputStream(file));
        } catch (Exception e) {
            return false;
        }
    }

    private static boolean isExcel2003(InputStream is) {
        try {
            new HSSFWorkbook(is);
            is.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isExcel2007(String filePath) {
        try {
            return isExcel2007(new File(filePath));
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isExcel2007(File file) {
        try {
            return isExcel2007(new FileInputStream(file));
        } catch (Exception e) {
            return false;
        }
    }

    private static boolean isExcel2007(InputStream is) {
        try {
            new XSSFWorkbook(is);
            is.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static List<Map<String, Object>> read(String filePath) {
        return read(new File(filePath));
    }

    public static List<Map<String, Object>> read(File file) {
        if (!file.exists()) {
            return new ArrayList<Map<String, Object>>();
        } else {
            try {
                return read(new FileInputStream(file));
            } catch (Exception e) {
                return new ArrayList<Map<String, Object>>();
            }
        }
    }

    public static List<Map<String, Object>> read(InputStream is) {
        try {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();

            List<Map<String, Object>> data = null;
            InputStream param_is = null;
            param_is = new ByteArrayInputStream(baos.toByteArray());
            if (isExcel2003(new ByteArrayInputStream(baos.toByteArray()))) {
                data = procWorkbook(new HSSFWorkbook(param_is));
            } else if (isExcel2007(new ByteArrayInputStream(baos.toByteArray()))) {
                data = procWorkbook(new XSSFWorkbook(param_is));
            }
            param_is.close();
            baos.close();
            is.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<Map<String, Object>>();
    }

    public static List<Map<String, Object>> procWorkbook(Workbook excel) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        try {
            Sheet sheet = null;
            Row row = null;
            Cell cell = null;

            Map<String, Object> map = null;
            String sheetName = null;
            List<List<String>> data = null;
            List<String> cols = null;

            if (excel != null) {
                for (int i = 0; i < excel.getNumberOfSheets(); i++) {
                    sheet = excel.getSheetAt(i);
                    map = new HashMap<String, Object>();
                    sheetName = excel.getSheetName(i);
                    data = new ArrayList<List<String>>();

                    if (sheet != null) {

                        int rowLength = sheet.getLastRowNum();
                        for (int j = 0; j <= rowLength; j++) {
                            row = sheet.getRow(j);
                            if (row == null) {
                                data.add(null);
                            } else {
                                cols = new ArrayList<String>();
                                short colLength = row.getLastCellNum();

                                for (int k = 0; k < colLength; k++) {
                                    cell = row.getCell(k);
                                    if (cell == null) {
                                        cols.add(null);
                                    } else {
                                        cell.setCellType(1);
                                        cols.add(cell.toString());
                                    }
                                }
                                data.add(cols);
                            }
                        }
                    }
                    map.put("name", sheetName);
                    map.put("data", data.size() == 1 && data.get(0) == null ? null : data);
                    result.add(map);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
