package com.eactive.eai.transformer.dao.excel;

import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.eactive.eai.common.util.Logger;


public class ExcelSheet implements Sheet {
    protected static Logger logger = Logger.getLogger("transformer.dao");

    private HSSFSheet sheet;

    private ExcelSheet(HSSFWorkbook workbook, int sheetIndex) {
        this.sheet = workbook.getSheetAt(sheetIndex);
    }

    public static ExcelSheet[] getExcelSheets(InputStream in) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook(in);
        ExcelSheet[] sheets = new ExcelSheet[workbook.getNumberOfSheets()];

        for (int i = 0; i < sheets.length; i++) {
            sheets[i] = new ExcelSheet(workbook, i);
        }

        return sheets;
    }

    public String getCellString(int row, int column) throws Exception {
        HSSFRow hrow = sheet.getRow(row);

        if (hrow == null) {
            throw new Exception("cannt read excel file line number : " + (row + 1));
        }

        HSSFCell cell = hrow.getCell((short) column);

        if (cell == null) {
        	return "";
        }
        
        try {
			if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
				return "" + ((int) cell.getNumericCellValue());
			} else {
		        if (cell.getStringCellValue() == null) { 	
		            return "";
		        }
				return cell.getStringCellValue().trim();
			}
        } catch(Exception e) {
    		throw new Exception("getCellString(" + (row+1) + "," + column + ") operation failed", e);
        }
    }

    @SuppressWarnings("deprecation")
	public int getCellInt(int row, int column) throws Exception {
    	HSSFCell cell = sheet.getRow(row).getCell((short) column);
    	try {
    		if (cell==null) {
    			throw new Exception("getCellInt(" + row + "," + column + ") operation failed - cell value is null");
    		}
    		if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
    			return (int) cell.getNumericCellValue();
    		} else {
    			return Integer.parseInt(cell.getStringCellValue().trim());
    		}
    	} catch(Exception e) {
    		throw new Exception("getCellInt(" + (row+1) + "," + column + ") operation failed", e);
    	}
    }

    public boolean existsRow(int row) throws Exception {
    	boolean isExist = true;
    	if( row > sheet.getLastRowNum()
    		|| this.getCellValue(row, 0) == null 
    		|| getCellValue(row, 1) == null) {
    		isExist = false;
    	}
    	return isExist;
    }
    
    public boolean existsCell(int row, int col) throws Exception {
    	boolean isExist = true;
    	if (!existsRow(row) || this.getCellValue(row, col) == null) 
    		isExist = false;
    	return isExist;
    }    

    public boolean isEquals(int row, int column, String value) throws Exception {
        return getCellString(row, column).equals(value);
    }

    public String getCellValue(int row, int column) {
        HSSFRow hrow = sheet.getRow(row);        
        if (hrow == null)
        	return null;        
        HSSFCell cell = hrow.getCell((short) column);        
        if (cell == null)
        	return null;

        switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_BLANK:
                return "";
    
            case HSSFCell.CELL_TYPE_BOOLEAN:
                return Boolean.toString(cell.getBooleanCellValue());
    
            case HSSFCell.CELL_TYPE_ERROR:
                return Byte.toString(cell.getErrorCellValue());
    
            case HSSFCell.CELL_TYPE_FORMULA:
                return cell.getCellFormula();
    
            case HSSFCell.CELL_TYPE_NUMERIC:
                return Double.toString(cell.getNumericCellValue());
    
            case HSSFCell.CELL_TYPE_STRING:
                return cell.getStringCellValue();
            default :
            	return null;
        }        
    }
    
    public String toString() {
    	StringBuilder sb = new StringBuilder();
    	for (int i=0; i<sheet.getLastRowNum(); i++) {
    		if (sheet.getRow(i) != null && sheet.getRow(i).getCell(0) != null)
    			sb.append("sheet[" + i + "] : " + sheet.getRow(i).getCell(0).getStringCellValue() + "\n");
    	}
    	return sb.toString();
    }   
}
