/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.apache.poi.hssf.usermodel.HSSFCell
 *  org.apache.poi.hssf.usermodel.HSSFCellStyle
 *  org.apache.poi.hssf.usermodel.HSSFDataFormat
 *  org.apache.poi.hssf.usermodel.HSSFFont
 *  org.apache.poi.hssf.usermodel.HSSFRow
 *  org.apache.poi.hssf.usermodel.HSSFSheet
 *  org.apache.poi.hssf.usermodel.HSSFWorkbook
 *  org.apache.poi.hssf.util.Region
 *  org.apache.poi.poifs.filesystem.POIFSFileSystem
 */
package com.hst.framework.common;

import com.hst.framework.common.SpecialType;
import com.hst.framework.util.HstReflectUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class SimpleXlsCreator {
    protected static final short DEFAULT_FONT_SIZE = 12;
    protected static final String DEFAULT_FONT_NAME = "\u5b8b\u4f53";
    protected static final short DEFAULT_FONT_WIDTH = 320;
    public static final String DATEFORMAT_SECOND = "yyyy-mm-dd hh:mm:ss";
    public static final String DATEFORMAT_DAY = "yyyy-mm-dd";
    protected static final int MAX_SHORT = Short.MAX_VALUE;
    protected HSSFWorkbook wb;
    protected HSSFSheet sheet;
    protected HSSFCellStyle csHead;
    protected HSSFCellStyle csContext;
    protected String dateFormat;
    protected String numberFormat;
    protected short fontWidth;
    protected int currentRow;
    protected int styleRow;
    protected boolean ignoreNull;

    public SimpleXlsCreator() {
        this.dateFormat = DATEFORMAT_DAY;
        this.numberFormat = "General";
        this.fontWidth = (short)320;
        this.wb = new HSSFWorkbook();
        this.sheet = this.wb.createSheet();
        this.csHead = this.wb.createCellStyle();
        this.csContext = this.wb.createCellStyle();
        HSSFFont fontHead = this.wb.createFont();
        HSSFFont fontContext = this.wb.createFont();
        fontHead.setFontName(DEFAULT_FONT_NAME);
        fontHead.setFontHeightInPoints((short)12);
        fontHead.setBoldweight((short)700);
        this.csHead.setFont(fontHead);
        this.csHead.setWrapText(false);
        this.csHead.setVerticalAlignment((short)1);
        this.csHead.setAlignment((short)2);
        this.csHead.setBorderBottom((short)1);
        this.csHead.setBottomBorderColor((short)8);
        this.csHead.setBorderLeft((short)1);
        this.csHead.setLeftBorderColor((short)8);
        this.csHead.setBorderRight((short)1);
        this.csHead.setRightBorderColor((short)8);
        this.csHead.setBorderTop((short)1);
        this.csHead.setTopBorderColor((short)8);
        fontContext.setFontName(DEFAULT_FONT_NAME);
        fontContext.setFontHeightInPoints((short)12);
        fontContext.setBoldweight((short)400);
        this.csContext.setFont(fontContext);
        this.csContext.setWrapText(false);
        this.csContext.setVerticalAlignment((short)1);
        this.csContext.setAlignment((short)1);
        this.csContext.setBorderBottom((short)1);
        this.csContext.setBottomBorderColor((short)8);
        this.csContext.setBorderLeft((short)1);
        this.csContext.setLeftBorderColor((short)8);
        this.csContext.setBorderRight((short)1);
        this.csContext.setRightBorderColor((short)8);
        this.csContext.setBorderTop((short)1);
        this.csContext.setTopBorderColor((short)8);
        this.ignoreNull = false;
        this.currentRow = 0;
    }

    public SimpleXlsCreator(String srcPath) throws FileNotFoundException, IOException {
        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isFile()) {
            throw new FileNotFoundException("\u6587\u4ef6" + srcPath + "\u4e0d\u5b58\u5728\uff01");
        }
        this.dateFormat = DATEFORMAT_DAY;
        this.numberFormat = "General";
        this.fontWidth = (short)320;
        POIFSFileSystem fs = new POIFSFileSystem((InputStream)new FileInputStream(srcFile));
        this.wb = new HSSFWorkbook(fs);
        this.sheet = this.wb.getSheetAt(0);
        this.csHead = null;
        this.csContext = null;
        this.ignoreNull = false;
        this.currentRow = 0;
        this.styleRow = 0;
    }

    public void createXls(String path) throws IOException {
        File file = new File(path);
        if (file == null) {
            throw new IOException("\u6587\u4ef6\u8def\u5f84\u4e0d\u5b58\u5728\u3002");
        }
        File directory = file.getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("\u521b\u5efa\u76ee\u5f55\u5931\u8d25\uff1a" + directory.getPath() + "\uff01");
        }
        FileOutputStream fileOut = new FileOutputStream(path);
        try {
            this.wb.write((OutputStream)fileOut);
        }
        finally {
            if (fileOut != null) {
                fileOut.close();
            }
        }
    }

    public void append(List lst) throws Exception {
        if (lst == null) {
            throw new IllegalArgumentException("\u5217\u8868\u4e3a\u7a7a\uff01");
        }
        HSSFCellStyle cellStyle = null;
        HSSFCellStyle numberStyle = null;
        HSSFCellStyle dateStyle = null;
        HSSFDataFormat df = this.wb.createDataFormat();
        if (this.csContext != null) {
            cellStyle = this.createCellStyle(this.csContext);
            numberStyle = this.createCellStyle(this.csContext);
            numberStyle.setDataFormat(df.getFormat(this.numberFormat));
            dateStyle = this.createCellStyle(this.csContext);
            dateStyle.setDataFormat(df.getFormat(this.dateFormat));
        }
        Object thisRow2 = null;
        for (Object thisRow2 : lst) {
            if (thisRow2 == null) continue;
            if (thisRow2.getClass().getComponentType() != null) {
                this.appendRow((Object[])thisRow2, cellStyle, dateStyle, numberStyle);
                continue;
            }
            if (thisRow2 instanceof List) {
                this.appendRow(((List)thisRow2).toArray(), cellStyle, dateStyle, numberStyle);
                continue;
            }
            throw new Exception("\u5217\u8868\u7c7b\u578b\u9519\u8bef\uff01");
        }
    }

    public void appendBean(List lst, String[] fieldNames) throws Exception {
        if (lst == null) {
            throw new IllegalArgumentException("\u5217\u8868\u4e3a\u7a7a\uff01");
        }
        if (fieldNames == null) {
            throw new IllegalArgumentException("\u663e\u793a\u7684\u5c5e\u6027\u5217\u8868\u4e3a\u7a7a\uff01");
        }
        Class<?> beanClass = lst.get(0).getClass();
        this.appendBean(lst, beanClass, fieldNames);
    }

    public void appendBean(List lst, Class beanClass, String[] fieldNames) throws Exception {
        if (lst == null) {
            throw new IllegalArgumentException("\u5217\u8868\u4e3a\u7a7a\uff01");
        }
        if (beanClass == null) {
            throw new IllegalArgumentException("JavaBean\u7c7b\u578b\u4e3a\u7a7a\uff01");
        }
        if (fieldNames == null) {
            throw new IllegalArgumentException("\u663e\u793a\u7684\u5c5e\u6027\u5217\u8868\u4e3a\u7a7a\uff01");
        }
        HSSFCellStyle cellStyle = null;
        HSSFCellStyle numberStyle = null;
        HSSFCellStyle dateStyle = null;
        HSSFDataFormat df = this.wb.createDataFormat();
        if (this.csContext != null) {
            cellStyle = this.createCellStyle(this.csContext);
            numberStyle = this.createCellStyle(this.csContext);
            numberStyle.setDataFormat(df.getFormat(this.numberFormat));
            dateStyle = this.createCellStyle(this.csContext);
            dateStyle.setDataFormat(df.getFormat(this.dateFormat));
        }
        Object[] thisRow = null;
        Object beanObject2 = null;
        Method[] getMethods = HstReflectUtil.getMethods(beanClass, fieldNames);
        Object[] noArg = new Object[]{};
        for (Object beanObject2 : lst) {
            if (beanObject2 == null) continue;
            thisRow = new Object[getMethods.length];
            int j = 0;
            while (j < getMethods.length) {
                thisRow[j] = getMethods[j].invoke(beanObject2, noArg);
                ++j;
            }
            this.appendRow(thisRow, cellStyle, dateStyle, numberStyle);
        }
    }

    public void appendHead(List lst) throws Exception {
        if (lst == null) {
            throw new IllegalArgumentException("\u5217\u8868\u4e3a\u7a7a\uff01");
        }
        HSSFCellStyle cellStyle = null;
        HSSFCellStyle numberStyle = null;
        HSSFCellStyle dateStyle = null;
        HSSFDataFormat df = this.wb.createDataFormat();
        if (this.csHead != null) {
            cellStyle = this.createCellStyle(this.csHead);
            numberStyle = this.createCellStyle(this.csHead);
            numberStyle.setDataFormat(df.getFormat(this.numberFormat));
            dateStyle = this.createCellStyle(this.csHead);
            dateStyle.setDataFormat(df.getFormat(this.dateFormat));
        }
        Object thisRow2 = null;
        for (Object thisRow2 : lst) {
            if (thisRow2 == null) continue;
            if (thisRow2.getClass().isArray()) {
                this.appendRow((Object[])thisRow2, cellStyle, dateStyle, numberStyle);
                continue;
            }
            if (thisRow2 instanceof List) {
                this.appendRow(((List)thisRow2).toArray(), cellStyle, dateStyle, numberStyle);
                continue;
            }
            throw new Exception("\u5217\u8868\u7c7b\u578b\u9519\u8bef\uff01");
        }
    }

    protected synchronized void appendRow(Object[] values, HSSFCellStyle cellStyle, HSSFCellStyle dateStyle, HSSFCellStyle numberStyle) throws Exception {
        if (values == null) {
            throw new IllegalArgumentException("\u6570\u636e\u4e3a\u7a7a\uff01");
        }
        HSSFRow row = null;
        HSSFCell cell = null;
        HSSFCell styleCell = null;
        Object value = null;
        String sValue = null;
        Region region = null;
        int iCellWidth = 0;
        short shortMaxWidth = 0;
        row = cellStyle == null && this.currentRow == this.styleRow ? this.sheet.getRow(this.currentRow) : this.sheet.createRow(this.currentRow);
        int i = 0;
        while (i < values.length) {
            value = values[i];
            if (value != null || !this.ignoreNull) {
                cell = cellStyle == null && this.currentRow == this.styleRow ? row.getCell((short)i) : row.createCell((short)i);
                if (cellStyle != null) {
                    cell.setCellStyle(cellStyle);
                } else {
                    styleCell = this.sheet.getRow(this.styleRow).getCell((short)i);
                    cell.setCellStyle(styleCell.getCellStyle());
                    cell.setCellType(styleCell.getCellType());
                }
                if (value != null) {
                    if (value instanceof SpecialType) {
                        SpecialType st = (SpecialType)value;
                        switch (st.getTypeCode()) {
                            case 1: {
                                if (this.currentRow > 0) {
                                    region = new Region(this.currentRow - 1, (short)i, this.currentRow, (short)i);
                                    break;
                                }
                                region = null;
                                break;
                            }
                            case 2: {
                                if (i > 0) {
                                    region = new Region(this.currentRow, (short)(i - 1), this.currentRow, (short)i);
                                    break;
                                }
                                region = null;
                                break;
                            }
                            default: {
                                region = null;
                            }
                        }
                        if (region != null) {
                            this.sheet.addMergedRegion(region);
                        }
                    } else if (value instanceof Date) {
                        if (cellStyle != null) {
                            cell.setCellStyle(dateStyle);
                            shortMaxWidth = this.sheet.getColumnWidth((short)i);
                            iCellWidth = i + 1 < values.length && values[i + 1] != null && values[i + 1] instanceof SpecialType && ((SpecialType)values[i + 1]).getTypeCode() == 2 ? 0 : this.dateFormat.getBytes().length * this.fontWidth;
                            if (iCellWidth > Short.MAX_VALUE || iCellWidth < 0) {
                                iCellWidth = 0;
                            }
                            if (iCellWidth > shortMaxWidth) {
                                this.sheet.setColumnWidth((short)i, (short)iCellWidth);
                            }
                        }
                        cell.setCellValue((Date)value);
                    } else if (value instanceof Number) {
                        sValue = value.toString();
                        if (cellStyle != null) {
                            cell.setCellStyle(numberStyle);
                            cell.setCellType(0);
                            shortMaxWidth = this.sheet.getColumnWidth((short)i);
                            iCellWidth = i + 1 < values.length && values[i + 1] != null && values[i + 1] instanceof SpecialType && ((SpecialType)values[i + 1]).getTypeCode() == 2 ? 0 : (sValue.getBytes().length + 1) * this.fontWidth;
                            if (iCellWidth > Short.MAX_VALUE || iCellWidth < 0) {
                                iCellWidth = 0;
                            }
                            if (iCellWidth > shortMaxWidth) {
                                this.sheet.setColumnWidth((short)i, (short)iCellWidth);
                            }
                        }
                        cell.setCellValue(new Double(sValue).doubleValue());
                    } else {
                        sValue = value.toString();
                        cell.setEncoding((short)1);
                        if (cellStyle != null) {
                            shortMaxWidth = this.sheet.getColumnWidth((short)i);
                            iCellWidth = i + 1 < values.length && values[i + 1] != null && values[i + 1] instanceof SpecialType && ((SpecialType)values[i + 1]).getTypeCode() == 2 ? 0 : sValue.getBytes().length * this.fontWidth;
                            if (iCellWidth > Short.MAX_VALUE || iCellWidth < 0) {
                                iCellWidth = 0;
                            }
                            if (iCellWidth > shortMaxWidth) {
                                this.sheet.setColumnWidth((short)i, (short)iCellWidth);
                            }
                        }
                        cell.setCellValue(sValue);
                    }
                }
            }
            ++i;
        }
        ++this.currentRow;
    }

    public void setCsContext(HSSFCellStyle csContext) {
        this.csContext = csContext;
    }

    public void setCsHead(HSSFCellStyle csHead) {
        this.csHead = csHead;
    }

    public String getDateFormat() {
        return this.dateFormat;
    }

    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }

    public short getFontWidth() {
        return this.fontWidth;
    }

    public void setFontWidth(short fontWidth) {
        this.fontWidth = fontWidth;
    }

    public HSSFCellStyle createCellStyle() {
        return this.wb.createCellStyle();
    }

    public HSSFFont createFont() {
        return this.wb.createFont();
    }

    public void setStartRow(int row) {
        if (--row >= 0) {
            this.currentRow = row;
            this.styleRow = row;
        }
    }

    public int getStartRow() {
        return this.currentRow + 1;
    }

    public boolean isIgnoreNull() {
        return this.ignoreNull;
    }

    public void setBIgnoreNull(boolean ignoreNull) {
        this.ignoreNull = ignoreNull;
    }

    public HSSFCellStyle getCsContext() {
        return this.csContext;
    }

    public HSSFCellStyle getCsHead() {
        return this.csHead;
    }

    public HSSFCellStyle createCellStyle(HSSFCellStyle cellStyle) {
        HSSFCellStyle result = this.wb.createCellStyle();
        result.setAlignment(cellStyle.getAlignment());
        result.setBorderBottom(cellStyle.getBorderBottom());
        result.setBorderLeft(cellStyle.getBorderLeft());
        result.setBorderRight(cellStyle.getBorderRight());
        result.setBorderTop(cellStyle.getBorderTop());
        result.setBottomBorderColor(cellStyle.getBottomBorderColor());
        result.setDataFormat(cellStyle.getDataFormat());
        result.setFillBackgroundColor(cellStyle.getFillBackgroundColor());
        result.setFillForegroundColor(cellStyle.getFillForegroundColor());
        result.setFillPattern(cellStyle.getFillPattern());
        result.setFont(this.wb.getFontAt(cellStyle.getFontIndex()));
        result.setHidden(cellStyle.getHidden());
        result.setIndention(cellStyle.getIndention());
        result.setLeftBorderColor(cellStyle.getLeftBorderColor());
        result.setLocked(cellStyle.getLocked());
        result.setRightBorderColor(cellStyle.getRightBorderColor());
        result.setRotation(cellStyle.getRotation());
        result.setTopBorderColor(cellStyle.getTopBorderColor());
        result.setVerticalAlignment(cellStyle.getVerticalAlignment());
        result.setWrapText(cellStyle.getWrapText());
        return result;
    }

    public String getNumberFormat() {
        return this.numberFormat;
    }

    public void setNumberFormat(String numberFormat) {
        this.numberFormat = numberFormat;
    }
}
