/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.essar.automation.dhaval.exceltojava.reader;

import com.essar.automation.dhaval.exceltojava.*;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author dhaval
 */
public class ExcelReader {

    private int FIELD_NAME = 0;
    private int FIELD_TYPE = 1;
    private int FIELD_GETTER = 2;
    private int FIELD_SETTER = 3;
    private int FIELD_RANGE_DISCRETE = 4;
    private int FIELD_SERVICE = 5;

    private boolean autoCreateGetterSetter = true;
    private boolean autoCreateServiceField = true;

    private String fileName;

    public ExcelReader(String fileName) {
        this.fileName = fileName;
    }

    public static void main(String[] args) throws Exception {
        ExcelReader excelTest = new ExcelReader("c:\\TestExcelForJava.xlsx");
        excelTest.generateJava();
    }

    public void generateJava() throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook(fileName);
        Iterator<XSSFSheet> sheets = wb.iterator();

        while(sheets.hasNext()){
            XSSFSheet sheet = sheets.next();

            JavaBean bean = new JavaBean();
            bean.setClassName(sheet.getSheetName());

            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                getField(bean, (XSSFRow) rows.next());
            }

            if (!bean.getFields().isEmpty()) {
                System.out.println("************************");
                System.out.println("************************");
                System.out.println(bean.generateCode());
                System.out.println("************************");
                System.out.println("************************");
            }
        }
    }

    private void getField(JavaBean bean, XSSFRow row) {
        Field field = new Field();

        Iterator cells = row.cellIterator();
        while (cells.hasNext()) {
            org.apache.poi.xssf.usermodel.XSSFCell cell = (XSSFCell) cells.next();

            switch (cell.getCellType()) {
                case XSSFCell.CELL_TYPE_NUMERIC:
                    break;
                case XSSFCell.CELL_TYPE_STRING:
                    if(cell.getColumnIndex() == FIELD_NAME){
                        if(cell.getStringCellValue().equalsIgnoreCase("field")){
                            return;
                        }
                        field.setName(convertToJavaField(cell.getStringCellValue()));
                        field.setOriginalName(cell.getStringCellValue());

                        if(autoCreateGetterSetter){
                            bean.addMethod(createGetter(field));
                            bean.addMethod(createSetter(field));
                        }

                        if(autoCreateServiceField){
                            field.addAnnotation(createServiceFieldAnnotation(field));
                        }
                    }else if(cell.getColumnIndex() == FIELD_TYPE){
                        field.setType(cell.getStringCellValue());

                    }else if(cell.getColumnIndex() == FIELD_RANGE_DISCRETE){
                        if(cell.getStringCellValue().equalsIgnoreCase("range")){
                            field.addAnnotation(createRangeAnnotation(field));
                        }else if(cell.getStringCellValue().equalsIgnoreCase("discrete")){
                            field.addAnnotation(createDiscreteAnnotation(field));
                        }
                    }
                    break;
                case XSSFCell.CELL_TYPE_BOOLEAN:
                    if(!autoCreateGetterSetter && cell.getColumnIndex() == FIELD_GETTER){
                        if(cell.getBooleanCellValue()){
                            bean.addMethod(createGetter(field));
                        }
                    }else if(!autoCreateGetterSetter && cell.getColumnIndex() == FIELD_SETTER){
                        if(cell.getBooleanCellValue()){
                            bean.addMethod(createSetter(field));
                        }
                    }else if(!autoCreateServiceField && cell.getColumnIndex() == FIELD_SERVICE){
                        field.addAnnotation(createServiceFieldAnnotation(field));
                    }
                    break;
            }
        }

        bean.addField(field);
    }

    public Method createGetter(Field field){
        Method method = new Method();
        method.setField(field);
        method.setGetter(true);
        return method;
    }

    public Method createSetter(Field field){
        Method method = new Method();
        method.setField(field);
        method.setSetter(true);
        return method;
    }

    public Annotation createRangeAnnotation(Field field){
        Annotation rangeAnnotation = new Annotation();
        rangeAnnotation.setName("RangeValue");
        rangeAnnotation.addValue("name", field.getOriginalName());
        return rangeAnnotation;
    }

    public Annotation createServiceFieldAnnotation(Field field){
        Annotation serviceFieldAnnotation = new Annotation();
        serviceFieldAnnotation.setName("ServiceField");
        serviceFieldAnnotation.addValue("name", field.getOriginalName());
        return serviceFieldAnnotation;
    }

    public Annotation createDiscreteAnnotation(Field field){
        Annotation discreteAnnotation = new Annotation();
        discreteAnnotation.setName("DescreteValue");
        discreteAnnotation.addValue("name", field.getOriginalName());
        return discreteAnnotation;
    }

    public String convertToJavaField(String field){
        String javaField = "";

        String[] fieldParts = field.split("_");
        for(int i=0;i<fieldParts.length;i++){
            if(i==0){
                javaField += fieldParts[i].toLowerCase();
            }else{
                javaField += fieldParts[i].toLowerCase().substring(0,1).toUpperCase() + fieldParts[i].toLowerCase().substring(1);
            }
        }

        return javaField;
    }
}
