/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.beans.catalog;

import com.arrebol.taxiservicios.beans.catalog.ExpenseIncomeCatalogBean;
import com.arrebol.taxiservicios.controller.catalog.ExpenseIncomeCatalogController;
import com.arrebol.taxiservicios.model.catalog.ExpenseIncomeCatalog;
import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.ClaveProductoServicioController;
import com.arrebol.taxiservicios.controller.catalog.ClaveUnidadController;
import com.arrebol.taxiservicios.model.catalog.ClaveProductoServicio;
import com.arrebol.taxiservicios.model.catalog.ClaveUnidad;
import com.arrebol.taxiservicios.model.enums.BasicType;
import com.arrebol.taxiservicios.model.enums.BoxType;
import com.arrebol.taxiservicios.model.enums.ExpenseCatalogType;
import com.arrebol.taxiservicios.model.enums.ExpenseIncomeType;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named("expenseIncomeCatalogBean")
@ViewScoped
public class ExpenseIncomeCatalogBean extends TaxiGenericBean implements Serializable, Datatable {
    private static final long serialVersionUID = -2400590341745672164L;
    private List<ExpenseIncomeCatalog> expenseIncomeCatalogList;
    private ExpenseIncomeCatalog selectedExpenseIncomeCatalog;
    private String id;
    private String typeBox;
    private String typeExpense;
    private String typeExpenseCatalog;
    private String typePaymentCustomer;
    private String typeExpenseEnum;
    private String name;
    private ExpenseIncomeCatalogController controllerExpenseIncomeCatalog;
    final Logger logger = LogManager.getLogger(ExpenseIncomeCatalogBean.class);
    private ClaveUnidadController claveUnidadController;
    private ClaveProductoServicio claveProductoServicio;
    private ClaveProductoServicioController claveProductoServicioController;
    private ClaveUnidad claveUnidad;
    private String idClaveProductoServicio;
    private String idClaveUnidad;
    private Boolean validateExpenseSelection;
    private Boolean validateGastoAPP;

    @PostConstruct
    public void init() {
        logger.info("init ExpenseIncomeCatalogBean");
        setClaveUnidadController(new ClaveUnidadController());
        setClaveProductoServicioController(new ClaveProductoServicioController());
        controllerExpenseIncomeCatalog = new ExpenseIncomeCatalogController();
        validateExpenseSelection = true;
        validateGastoAPP = false;
        expenseIncomeCatalogList = controllerExpenseIncomeCatalog.findSExpenseIncomeCatalog(getLoggedUser().getLocation());
        
    }
    public void handleClose(){
        setId(null);
        setName(null);
        setTypeBox(null);
        setTypeExpense(null);
        setTypeExpenseCatalog(null);
        setTypePaymentCustomer(null);
        setClaveUnidad(null);
        setClaveProductoServicio(null);
        setIdClaveUnidad(null);
        setIdClaveProductoServicio(null);
    }

    @Override
    public void addRow() {
        if(getTypeBox().equals("SMALL")){
            selectedExpenseIncomeCatalog.setActiveBox(BoxType.SMALL);
        }else if (getTypeBox().equals("GENERAL")){
            selectedExpenseIncomeCatalog.setActiveBox(BoxType.GENERAL);
        }else if(getTypeBox().equals("BOTH")){
            selectedExpenseIncomeCatalog.setActiveBox(BoxType.BOTH);
        }
        
        if(getTypeExpense().equals("EARNING")){ 
            selectedExpenseIncomeCatalog.setExpenseIncomeCatalogType(ExpenseIncomeType.EARNING);
            selectedExpenseIncomeCatalog.setExpenseCatalogType(ExpenseCatalogType.DISABLED);
        }else if(getTypeExpense().equals("EXPENSE")){
            selectedExpenseIncomeCatalog.setExpenseIncomeCatalogType(ExpenseIncomeType.EXPENSE);
            if(getTypeExpenseCatalog().equals("")){
                FacesMessage msg = new FacesMessage("Tipo de gasto.", "El tipo de gasto es necesario.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                setIdClaveProductoServicio(null);
                setIdClaveUnidad(null);
                setClaveUnidadController(new ClaveUnidadController());
                setClaveProductoServicioController(new ClaveProductoServicioController());
                return;
                
            }else {
                if(getTypeExpenseCatalog().equals("EXPENSE")){
                    selectedExpenseIncomeCatalog.setExpenseCatalogType(ExpenseCatalogType.EXPENSE);
                }else{
                    selectedExpenseIncomeCatalog.setExpenseCatalogType(ExpenseCatalogType.INVESTMENT);
                }
            
            }
            
        }
       if(getTypeBox().equals("SMALL")){
        if(getTypePaymentCustomer().equals("ENEBLED")){
            selectedExpenseIncomeCatalog.setActivePaymentCustomer(BasicType.ENEBLED);
        }else if(getTypePaymentCustomer().equals("DISABLED")){
            selectedExpenseIncomeCatalog.setActivePaymentCustomer(BasicType.DISABLED);
        }}
       else{
           selectedExpenseIncomeCatalog.setActivePaymentCustomer(BasicType.DISABLED);
       }
        selectedExpenseIncomeCatalog.setName(name);
        selectedExpenseIncomeCatalog.setClaveProductoServicio(getClaveProductoServicio());
        selectedExpenseIncomeCatalog.setClaveUnidad(getClaveUnidad());
         FacesMessage msg;
        if(selectedExpenseIncomeCatalog.getId()==null){
             msg = new FacesMessage("Nuevo Producto", "Se agregó correctamente");
        }else{
            msg = new FacesMessage("Activision de producto ", "Se actualizó correctamente");
        } 
        getSelectedExpenseIncomeCatalog().setLocation(getLoggedUser().getLocation());
        controllerExpenseIncomeCatalog.saveExpense(selectedExpenseIncomeCatalog, getLoggedUser().getId());       
        FacesContext.getCurrentInstance().addMessage(null, msg);
        selectedExpenseIncomeCatalog = new ExpenseIncomeCatalog();
        setName(null);
        setIdClaveProductoServicio(null);
        setIdClaveUnidad(null);
        init();
    }
    public void updateInfo() {
        setName(getSelectedExpenseIncomeCatalog().getName());
        setTypeBox(getSelectedExpenseIncomeCatalog().getActiveBox().name());
        setTypeExpense(getSelectedExpenseIncomeCatalog().getExpenseCatalogType().name());
        setTypeExpenseCatalog(getSelectedExpenseIncomeCatalog().getExpenseIncomeCatalogType().name());
        setTypePaymentCustomer(getSelectedExpenseIncomeCatalog().getActive().name());
        setClaveUnidad(getSelectedExpenseIncomeCatalog().getClaveUnidad());
        setClaveProductoServicio(getSelectedExpenseIncomeCatalog().getClaveProductoServicio());
        setIdClaveUnidad(getSelectedExpenseIncomeCatalog().getClaveUnidad().toString());
        setIdClaveProductoServicio(getSelectedExpenseIncomeCatalog().getClaveProductoServicio().toString());
        setTypePaymentCustomer(getSelectedExpenseIncomeCatalog().getActivePaymentCustomer().toString());
    }
 
    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void deleteRow() {
        controllerExpenseIncomeCatalog.delete(selectedExpenseIncomeCatalog,getLoggedUser().getId());
        expenseIncomeCatalogList.remove(selectedExpenseIncomeCatalog);
        selectedExpenseIncomeCatalog = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Ingreso/Egreso eliminado", "Se eliminó correctamente.");
    }

    
    public List<ClaveUnidad> searchClaveUnidad(String query) {
        
        List<ClaveUnidad> list = null;
        try {
            list = getClaveUnidadController().getClaveUnidadLike(query);
        } catch (Exception e) {
            
            showMessage(FacesMessage.SEVERITY_FATAL,
                    "No se pudo buscar la clave producto servicio",
                    "Error");
        }
        return list;
    }
    
    public void onItemSelectClaveUnidad(SelectEvent event) {
        
        try {
            setClaveUnidad(claveUnidadController.getById(getIdClaveUnidad()));
        } catch (Exception e) {
            
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("garage.title"),
                    getBundlePropertyFile().getString("garage.generic.exception"));
        }
    }
    
    public void onItemSelectClaveProductoServicio(SelectEvent event) {
        
        try {
            setClaveProductoServicio(claveProductoServicioController.getById(getIdClaveProductoServicio()));
        } catch (Exception e) {
            
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("garage.title"),
                    getBundlePropertyFile().getString("garage.generic.exception"));
        }
    }
    
    public List<ClaveProductoServicio> searchClaveProductoServicios(String query) {
        
        List<ClaveProductoServicio> list = null;
        try {
            list = getClaveProductoServicioController().getClaveProductoServicioLike(query);
        } catch (Exception e) {
            
            showMessage(FacesMessage.SEVERITY_FATAL,
                    "No se pudo buscar la clave producto servicio",
                    "Error");
        }
        return list;
    }
    
    public void onChangeTypeExpenseCombox(){
        if(getTypeExpense().equals("EXPENSE")){
            setValidateExpenseSelection(false);
        }else{
            setValidateExpenseSelection(true);
        }
    }
    public void onChangeBoxCombox(){
        if(getTypeBox().equals("SMALL")){
            setValidateGastoAPP(false);
        }else{
            setValidateGastoAPP(true);
        }
    }

    public List<ExpenseIncomeCatalog> getExpenseIncomeCatalogList() {
        return expenseIncomeCatalogList;
    }

    public void setExpenseIncomeCatalogList(List<ExpenseIncomeCatalog> expenseIncomeCatalogList) {
        this.expenseIncomeCatalogList = expenseIncomeCatalogList;
    }

    public ExpenseIncomeCatalog getSelectedExpenseIncomeCatalog() {
        return selectedExpenseIncomeCatalog;
    }

    public void setSelectedExpenseIncomeCatalog(ExpenseIncomeCatalog selectedExpenseIncomeCatalog) {
        this.selectedExpenseIncomeCatalog = selectedExpenseIncomeCatalog;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ExpenseIncomeCatalogController getControllerExpenseIncomeCatalog() {
        return controllerExpenseIncomeCatalog;
    }

    public void setControllerExpenseIncomeCatalog(ExpenseIncomeCatalogController controllerExpenseIncomeCatalog) {
        this.controllerExpenseIncomeCatalog = controllerExpenseIncomeCatalog;
    }

    public String getTypeBox() {
        return typeBox;
    }

    public void setTypeBox(String typeBox) {
        this.typeBox = typeBox;
    }

    public String getTypeExpense() {
        return typeExpense;
    }

    public void setTypeExpense(String typeExpense) {
        this.typeExpense = typeExpense;
    }

    public String getTypePaymentCustomer() {
        return typePaymentCustomer;
    }

    public void setTypePaymentCustomer(String typePaymentCustomer) {
        this.typePaymentCustomer = typePaymentCustomer;
    }
    
    public void openNew() {
        this.selectedExpenseIncomeCatalog = new ExpenseIncomeCatalog();
        
    }

    public ClaveUnidadController getClaveUnidadController() {
        return claveUnidadController;
    }

    public void setClaveUnidadController(ClaveUnidadController claveUnidadController) {
        this.claveUnidadController = claveUnidadController;
    }

    public ClaveProductoServicioController getClaveProductoServicioController() {
        return claveProductoServicioController;
    }

    public void setClaveProductoServicioController(ClaveProductoServicioController claveProductoServicioController) {
        this.claveProductoServicioController = claveProductoServicioController;
    }

    public String getIdClaveProductoServicio() {
        return idClaveProductoServicio;
    }

    public void setIdClaveProductoServicio(String idClaveProductoServicio) {
        this.idClaveProductoServicio = idClaveProductoServicio;
    }

    public String getIdClaveUnidad() {
        return idClaveUnidad;
    }

    public void setIdClaveUnidad(String idClaveUnidad) {
        this.idClaveUnidad = idClaveUnidad;
    }

    public ClaveUnidad getClaveUnidad() {
        return claveUnidad;
    }

    public void setClaveUnidad(ClaveUnidad claveUnidad) {
        this.claveUnidad = claveUnidad;
    }

    public ClaveProductoServicio getClaveProductoServicio() {
        return claveProductoServicio;
    }

    public void setClaveProductoServicio(ClaveProductoServicio claveProductoServicio) {
        this.claveProductoServicio = claveProductoServicio;
    }  

    public String getTypeExpenseCatalog() {
        return typeExpenseCatalog;
    }

    public void setTypeExpenseCatalog(String typeExpenseCatalog) {
        this.typeExpenseCatalog = typeExpenseCatalog;
    }

    public Boolean getValidateExpenseSelection() {
        return validateExpenseSelection;
    }

    public void setValidateExpenseSelection(Boolean validateExpenseSelection) {
        this.validateExpenseSelection = validateExpenseSelection;
    }

    public String getTypeExpenseEnum() {
        return typeExpenseEnum;
    }

    public void setTypeExpenseEnum(String typeExpenseEnum) {
        this.typeExpenseEnum = typeExpenseEnum;
    }

    public Boolean getValidateGastoAPP() {
        return validateGastoAPP;
    }

    public void setValidateGastoAPP(Boolean validateGastoAPP) {
        this.validateGastoAPP = validateGastoAPP;
    }
    
    
    
}
