package com.bocloud.ims.service.utils.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.RichTextString;
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.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * excel构造工厂
 * 
 * @author tangcq
 *
 */
public class ExcelFactory {
	private static Logger logger = LoggerFactory.getLogger(ExcelFactory.class);

	/**
	 * 
	 * @author tangcq EXCEL2000 老版 EXCEL2007新版
	 */
	public enum ExcelType {
		/**
		 * 老版本 office
		 */
		EXCEL2000,
		/**
		 * 新版本office
		 */
		EXCEL2007
	};

	// excel类型
	protected ExcelType excelType = null;
	private InputStream fis = null;

	public ExcelType getExcelType() {
		return excelType;
	}

	public void setExcelType(ExcelType excelType) {
		this.excelType = excelType;
	}

	/**
	 * 创建文本单元格
	 * 
	 * @param args
	 * @return
	 */
	public RichTextString newRichTextString(String args) {
		switch (excelType) {
		case EXCEL2000:
			return new HSSFRichTextString(args);
		case EXCEL2007:
			return new XSSFRichTextString(args);
		default:
			return new HSSFRichTextString(args);
		}
	}

	public ExcelFactory(File file, String fileName) {
		try {
			if (fileName == null || "".equals(fileName)) {
				if (file.getName().endsWith(".xls")) {
					excelType = ExcelType.EXCEL2000;
				} else {
					excelType = ExcelType.EXCEL2007;
				}
			} else {
				if (fileName.endsWith(".xls")) {
					excelType = ExcelType.EXCEL2000;
				} else {
					excelType = ExcelType.EXCEL2007;
				}
			}

			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			logger.error("文件处理失败", e);
		}
	}

	public ExcelFactory(InputStream fis, String fileName) {
		try {

			if (fileName.endsWith(".xls")) {
				excelType = ExcelType.EXCEL2000;
			} else {
				excelType = ExcelType.EXCEL2007;
			}
			this.fis = fis;
		} catch (Exception e) {
			logger.error("文件处理失败", e);
		}

	}

	public ExcelFactory(Row row) {
		if (row instanceof HSSFRow) {
			excelType = ExcelType.EXCEL2000;
		} else {
			excelType = ExcelType.EXCEL2007;
		}
	}

	public ExcelFactory(Workbook wb) {
		if (wb instanceof HSSFWorkbook) {
			excelType = ExcelType.EXCEL2000;
		} else {
			excelType = ExcelType.EXCEL2007;
		}

	}

	public ExcelFactory(Sheet sheet) {
		if (sheet instanceof HSSFSheet) {
			excelType = ExcelType.EXCEL2000;
		} else {
			excelType = ExcelType.EXCEL2007;
		}
	}

	public ExcelFactory() {
		excelType = ExcelType.EXCEL2000;

	}

	public ExcelFactory(String fileName) {

		if (fileName.endsWith(".xls")) {
			excelType = ExcelType.EXCEL2000;
		} else {
			excelType = ExcelType.EXCEL2007;
		}
	}

	public void closeQuietly() {
		IOUtils.closeQuietly(fis);
	}

	public Workbook newWorkbook() {
		switch (excelType) {
		case EXCEL2000:
			if (fis == null) {
				return new HSSFWorkbook();
			} else {
				try {
					return new HSSFWorkbook(fis);
				} catch (IOException e) {
					logger.error("文件处理失败", e);
				}
			}

		case EXCEL2007:
			if (fis == null) {
				return new XSSFWorkbook();
			} else {
				try {
					return new XSSFWorkbook(fis);
				} catch (IOException e) {
					logger.error("文件处理失败", e);
				}
			}
		default:
			return new HSSFWorkbook();
		}

	}

	public DataValidation newDataValidation(CellRangeAddressList cellRangeAddressList, String[] textlist, Sheet sheet,
			String formula) {
		DataValidation validation = null;
		switch (excelType) {
		case EXCEL2000:
			validation = new HSSFDataValidation(cellRangeAddressList, getExcel2000DataValidation(textlist, formula));
			return validation;
		case EXCEL2007:
			XSSFDataValidationHelper helper = new XSSFDataValidationHelper((XSSFSheet) sheet);
			validation = helper.createValidation(getExcel2007DataValidation(helper, textlist, formula),
					cellRangeAddressList);
			return validation;
		default:
			return validation;
		}

	}

	public static DVConstraint getExcel2000DataValidation(String[] textlist, String formula) {
		DVConstraint constraint = null;
		if (formula == null) {
			constraint = DVConstraint.createExplicitListConstraint(textlist);
		} else {
			constraint = DVConstraint.createFormulaListConstraint(formula);
		}
		return constraint;
	}

	public static DataValidationConstraint getExcel2007DataValidation(XSSFDataValidationHelper helper,
			String[] textlist, String formula) {
		DataValidationConstraint constraint = null;
		if (formula == null) {
			constraint = helper.createExplicitListConstraint(textlist);
		} else {
			constraint = helper.createFormulaListConstraint(formula);
		}
		return constraint;
	}
}
