package com.fp.excel;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Clase que obtiene el archivo de excel a partir de un arreglo de bytes
 *
 * @author malmeida
 */
public class WorkbookFactoryStreamBytes implements ExcelToMapMessages {

    private InputStream inp;
    private String workbookFactoryStatus, file_extension, extension;
    private Workbook wb;
    private byte[] arrayBytesExcel;

    /**
     *
     * @param arrayBytesExcel
     * @param extension
     */
    public WorkbookFactoryStreamBytes(byte[] arrayBytesExcel, String extension) {
        this.workbookFactoryStatus = null;
        this.arrayBytesExcel = arrayBytesExcel;
        this.inp = inputStreamBuilder(this.arrayBytesExcel);
        this.extension = extension;
        this.wb = this.excelWorkbookBuilder(this.extension);
    }

    /**
     *
     * @param arrayBytesExcel
     * @return
     */
    private InputStream inputStreamBuilder(byte[] arrayBytesExcel) {
        try {
            InputStream inpm = new ByteArrayInputStream(arrayBytesExcel);
            this.workbookFactoryStatus = WorkbookFactoryStreamBytes.INPUT_STREAM_CREATED;
            return inpm;
        } catch (Exception ex) {
            this.workbookFactoryStatus = WorkbookFactoryStreamBytes.FILE_NOT_FOUND;
            return null;
        }
    }

    /**
     *
     * @param fileExtension
     * @return
     */
    private Workbook excelWorkbookBuilder(String fileExtension) {
        if (this.workbookFactoryStatus.equals(WorkbookFactoryStreamBytes.FILE_NOT_FOUND)) {
            this.workbookFactoryStatus = WorkbookFactoryStreamBytes.WORKBOOK_NOT_CREATED;
            return null;
        } else {
            Workbook wbm = null;
            try {
                this.file_extension = fileExtension;
                if (this.file_extension.equals("xls")) {
                    wbm = new HSSFWorkbook(this.inp);
                }
                if (this.file_extension.equals("xlsx")) {
                    wbm = new XSSFWorkbook(this.inp);
                }
                this.workbookFactoryStatus = WorkbookFactoryStreamBytes.WORKBOOK_CREATED;
                return wbm;
            } catch (IOException ex) {
                this.workbookFactoryStatus = WorkbookFactoryStreamBytes.WORKBOOK_NOT_CREATED;
                return null;
            } catch (Exception ex) {
                this.workbookFactoryStatus = WorkbookFactoryStreamBytes.WORKBOOK_NOT_CREATED;
                return null;
            }
        }

    }

    /**
     *
     * @return
     */
    public String getWorkbookFactoryStatus() {
        return this.workbookFactoryStatus;
    }

    /**
     *
     * @return
     */
    public String getWorkbookFactoryStatusByte() {
        return this.workbookFactoryStatus;
    }

    /**
     *
     * @return
     */
    public Workbook getExcelWorkBook() {
        return this.wb;
    }
}