/*
 * 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.apc.web.beans.admin;

import com.arrebol.apc.controller.admin.VehicleController;
import com.arrebol.apc.web.beans.system.employee.*;
import com.arrebol.apc.model.admin.Bonus;
import com.arrebol.apc.model.catalog.RoleCtlg;
import com.arrebol.apc.model.catalog.Vehicle;
import com.arrebol.apc.model.core.HumanResource;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.enums.HumanResourceStatus;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named
@ViewScoped
public class VehicleBean extends GenericBean implements Serializable {

    public void saveVehicleRow() {
        logger.debug("saveVehicle");
        try {
            getSaveVehicle().setCreatedBy(getLoggedUser().getUser().getId());
            getSaveVehicle().setCreatedOn(new Date());
            getSaveVehicle().setVehicleStatus(ActiveStatus.ENEBLED);
            getSaveVehicle().setIdDriver(getUpdateId());
            
            String messafeFormat = getBundlePropertyFile().getString("message.format.failure");
            FacesMessage.Severity severity = FacesMessage.SEVERITY_WARN;

            if (getController().saveVehicle(getSaveVehicle())) {
                setSaveVehicle(new Vehicle());

                messafeFormat = getBundlePropertyFile().getString("message.format.sucess");
                severity = FacesMessage.SEVERITY_INFO;

                refreshDropBox();
            }
            Object[] param = {"Vehicle", getBundlePropertyFile().getString("created")};

            buildAndSendMessage(param, messafeFormat, severity, "Vehicle");
        } catch (Exception e) {
            logger.error("saveVehicle", e);
            Object[] param = {getBundlePropertyFile().getString("created")};

            buildAndSendMessage(
                    param,
                    getBundlePropertyFile().getString("message.format.fatal"),
                    FacesMessage.SEVERITY_FATAL,
                    "Vehicle"
            );
        }
    }

    /**
     *
     * @param action 1 = TO DISABLED, 2 = TO ENEBLED, 3 = TO DELETED, 4 NOT
     * ALLOWED
     */
    public void actionDropBox(int action) {
        logger.debug("actionDropBox");
        try {
            String messageTitle = getBundlePropertyFile().getString("employee");
            String actionUserIdSelected = null;
            String messageAction = null;

            HumanResourceStatus status = null;

            switch (action) {
                case 1:
                    actionUserIdSelected = getEnebledId();
                    messageAction = getBundlePropertyFile().getString("disabled");
                    status = HumanResourceStatus.DISABLED;
                    break;
                case 2:
                    actionUserIdSelected = getDisabledId();
                    messageAction = getBundlePropertyFile().getString("enebled");
                    status = HumanResourceStatus.ENEBLED;
                    break;
                case 3:
                    actionUserIdSelected = getDeletedId();
                    messageAction = getBundlePropertyFile().getString("deleted");
                    status = HumanResourceStatus.DELETED;
                    break;
                default:
                    throw new Exception(action + " is NOT valid a option");
            }

            if (executeAction(status, actionUserIdSelected, messageTitle, messageAction)) {
                refreshDropBox();
            }
        } catch (Exception e) {
            logger.error("actionDropBox", e);
            Object[] param = {getBundlePropertyFile().getString("employee")};

            buildAndSendMessage(
                    param,
                    getBundlePropertyFile().getString("message.format.fatal"),
                    FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("process")
            );
        }
    }

    public void updateSlctBtnHRAction() {
//        logger.debug("updateHR");
//        try {
//            getUpdateHumanResource().setLastUpdatedBy(getLoggedUser().getUser().getId());
//            getUpdateHumanResource().setLastUpdatedOn(new Date());
//            getUpdateHumanResource().setRoleCtlg(new RoleCtlg(roleUpdate));
//            
//            if (getUpdateHumanResource().getEmployeeSaving() == null) {
//                getUpdateHumanResource().setEmployeeSaving(BigDecimal.ZERO);
//            }
//
//            String messafeFormat = getBundlePropertyFile().getString("message.format.failure");
//            FacesMessage.Severity severity = FacesMessage.SEVERITY_WARN;
//
//            if (null != getBonusId() && getBonusId().length() == 36) {
//                getUpdateHumanResource().setBonus(new Bonus(getBonusId()));
//            }
//
//            if (getController().updateByHumanResourceId(getUpdateHumanResource(), false)) {
//                messafeFormat = getBundlePropertyFile().getString("message.format.sucess");
//                severity = FacesMessage.SEVERITY_INFO;
//            }
//
//            Object[] param = {getBundlePropertyFile().getString("employee"), getBundlePropertyFile().getString("updated")};
//
//            buildAndSendMessage(param, messafeFormat, severity, getBundlePropertyFile().getString("employee"));
//        } catch (Exception e) {
//            logger.error("updateSlctBtnHRActionListener", e);
//            Object[] param = {getBundlePropertyFile().getString("updated")};
//
//            buildAndSendMessage(
//                    param,
//                    getBundlePropertyFile().getString("message.format.fatal"),
//                    FacesMessage.SEVERITY_FATAL,
//                    getBundlePropertyFile().getString("employee")
//            );
//        }
    }

    public void loadVehicleToUpdate() {
//        try {
//            setUpdateHumanResource(getController().findHumanResourceById(getUpdateId()));
//            setRoleUpdate(getUpdateHumanResource().getBonus().getId());
//            updateBonusId = getRoleUpdate();
//        } catch (Exception e) {
//            logger.error("updateSlctBtnHRActionListener", e);
//            Object[] param = {getBundlePropertyFile().getString("searching")};
//
//            buildAndSendMessage(
//                    param,
//                    getBundlePropertyFile().getString("message.format.fatal"),
//                    FacesMessage.SEVERITY_FATAL,
//                    getBundlePropertyFile().getString("employee")
//            );
//        }
    }

    /**
     *
     * @param option 1 = to disabled, 2 = to enebled, 4 = to updated
     */
    public void enebledDisabledDropBoxListener(int option) {
//        logger.debug("enebledDisabledDropBoxListener");
//        try {
//            HumanResourceStatus status = HumanResourceStatus.ENEBLED;
//
//            boolean goAHead = false;
//
//            switch (option) {
//                case 1:
//                    if (isEnebledHR()) {
//                        goAHead = isEnebledHR();
//                    } else {
//                        setEnebledHumanResourcesLst(null);
//                    }
//                    break;
//                case 2:
//                    if (isDisabledHR()) {
//                        goAHead = isDisabledHR();
//                    } else {
//                        setDeletedHumanResourcesLst(null);
//                    }
//                    break;
//                case 4:
//                    if (isSelectedUpdateHR()) {
//                        goAHead = isSelectedUpdateHR();
//                    } else {
//                        setUpdateHumanResourcesLst(null);
//                        setUpdateVehicle(new Vehicle());
//                    }
//                    break;
//            }
//
//            if (goAHead) {
//                if (2 == option) {
//                    status = HumanResourceStatus.DISABLED;
//                }
//
//                List<HumanResource> results = getController().findEmployeesByType(
//                        new Office(getLoggedUser().getOffice().getId()),
//                        status,
//                        getLoggedUser().getUser().getHumanResource().getId()
//                );
//
//                switch (option) {
//                    case 1:
//                        setEnebledHumanResourcesLst(results);
//                        break;
//                    case 2:
//                        setDisabledHumanResourcesLst(results);
//                        break;
//                    case 4:
//                        setUpdateHumanResourcesLst(results);
//                        setUpdateHumanResource(new HumanResource());
//                        break;
//                }
//            }
//        } catch (Exception e) {
//            logger.error("enebledDisabledDropBoxListener");
//
//            Object[] param = {getBundlePropertyFile().getString("searching")};
//
//            buildAndSendMessage(
//                    param,
//                    getBundlePropertyFile().getString("message.format.fatal"),
//                    FacesMessage.SEVERITY_FATAL,
//                    getBundlePropertyFile().getString("employee")
//            );
//        }
    }

    public void deletedHRListener() {
//        try {
//            if (isDeletedHR()) {
//                List<HumanResourceStatus> statusLst = new ArrayList<>();
//
//                statusLst.add(HumanResourceStatus.ENEBLED);
//                statusLst.add(HumanResourceStatus.DISABLED);
//
//                setDeletedHumanResourcesLst(
//                        getController().findEmployeesInType(
//                                new Office(getLoggedUser().getOffice().getId()),
//                                statusLst,
//                                getLoggedUser().getUser().getHumanResource().getId()
//                        )
//                );
//            } else {
//                setDeletedHumanResourcesLst(null);
//            }
//        } catch (Exception e) {
//            logger.error("deletedHRListener", e);
//            Object[] param = {getBundlePropertyFile().getString("searching")};
//
//            buildAndSendMessage(
//                    param,
//                    getBundlePropertyFile().getString("message.format.fatal"),
//                    FacesMessage.SEVERITY_FATAL,
//                    getBundlePropertyFile().getString("employee")
//            );
//        }
    }

    /**
     *
     * @param status
     * @param userIdSelected
     * @param msgTitle
     * @param msgAction
     */
    private boolean executeAction(HumanResourceStatus status, String userIdSelected, String msgTitle, String msgAction) {
        logger.debug("executeAction");

        boolean success = false;

        try {
            String messafeFormat = getBundlePropertyFile().getString("message.format.failure");
            FacesMessage.Severity severity = FacesMessage.SEVERITY_WARN;

            success = getController().updateHRByStatus(status, userIdSelected, getLoggedUser().getUser().getId());

            if (success) {
                logger.debug("executeAction");

                messafeFormat = getBundlePropertyFile().getString("message.format.sucess");
                severity = FacesMessage.SEVERITY_INFO;
            }

            Object[] param = {msgTitle, msgAction};

            buildAndSendMessage(param, messafeFormat, severity, msgTitle);
        } catch (Exception e) {
            logger.error("executeAction", e);
            Object[] param = {msgTitle};

            buildAndSendMessage(
                    param,
                    getBundlePropertyFile().getString("message.format.fatal"),
                    FacesMessage.SEVERITY_FATAL,
                    msgTitle
            );
        }
        return success;
    }

    private void refreshDropBox() {
        try {
            if (isEnebledHR()) {
                enebledDisabledDropBoxListener(1);
            }
            if (isDisabledHR()) {
                enebledDisabledDropBoxListener(2);
            }

            if (isDeletedHR()) {
                deletedHRListener();
            }

            if (isSelectedUpdateVehicle()) {
                enebledDisabledDropBoxListener(4);
            }
        } catch (Exception e) {
            logger.error("executeAction", e);
        }
    }

    private static final long serialVersionUID = 2969985354193657703L;
    final Logger logger = LogManager.getLogger(EmployeeBean.class);

    private VehicleController controller;

    private Vehicle saveVehicle;
    private Vehicle updateVehicle;

    private String role;
    private List<RoleCtlg> roles;

    private boolean selectedUpdateVehicle;
    private String updateId;

    private String roleUpdate;
    private List<HumanResource> updateHumanResourcesLst;
    private List<RoleCtlg> typeLst;

    private boolean enebledHR;
    private String enebledId;
    private List<HumanResource> enebledHumanResourcesLst;

    private boolean disabledHR;
    private String disabledId;
    private List<HumanResource> disabledHumanResourcesLst;

    private boolean deletedHR;
    private String deletedId;
    private List<HumanResource> deletedHumanResourcesLst;

    private String bonusId;
    private List<Bonus> bonuses;
    private String updateBonusId;
    
    private List<String> vehicleType;

    @PostConstruct()
    public void init() {
        try {
            loadBundlePropertyFile();
            setController(new VehicleController());
            setSaveVehicle(new Vehicle());
            setUpdateVehicle(new Vehicle());

            List<HumanResource> results = getController().findEmployeesByType(
                    new Office(getLoggedUser().getOffice().getId()),
                    HumanResourceStatus.ENEBLED,
                    getLoggedUser().getUser().getHumanResource().getId()
            );

            setUpdateHumanResourcesLst(results);
            
            vehicleType = new ArrayList<>();
            vehicleType.add("Automóvil");
            vehicleType.add("Motocicleta");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    @PreDestroy
    public void finish() {
        try {
            setSaveVehicle(new Vehicle());
            setUpdateVehicle(new Vehicle());
            setController(null);
        } catch (Exception e) {
            logger.error("finish", e);
        }
    }

    public VehicleController getController() {
        return controller;
    }

    public void setController(VehicleController controller) {
        this.controller = controller;
    }

    public Vehicle getSaveVehicle() {
        return saveVehicle;
    }

    public void setSaveVehicle(Vehicle saveVehicle) {
        this.saveVehicle = saveVehicle;
    }

    public Vehicle getUpdateVehicle() {
        return updateVehicle;
    }

    public void setUpdateVehicle(Vehicle updateVehicle) {
        this.updateVehicle = updateVehicle;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<RoleCtlg> getRoles() {
        if (null == roles) {
            /*
            roles = Stream.of(
                    HumanResourceType.values())
                    .map(Enum::name)
                    .collect(Collectors.toList()
                    );
             */
        }
        return roles;
    }

    public void setRoles(List<RoleCtlg> roles) {
        this.roles = roles;
    }

    public boolean isSelectedUpdateVehicle() {
        return selectedUpdateVehicle;
    }

    public void setSelectedUpdateVehicle(boolean selectedUpdateVehicle) {
        this.selectedUpdateVehicle = selectedUpdateVehicle;
    }

    public String getUpdateId() {
        return updateId;
    }

    public void setUpdateId(String updateId) {
        this.updateId = updateId;
    }

    public String getRoleUpdate() {
        return roleUpdate;
    }

    public void setRoleUpdate(String roleUpdate) {
        this.roleUpdate = roleUpdate;
    }

    public List<HumanResource> getUpdateHumanResourcesLst() {
        return updateHumanResourcesLst;
    }

    public void setUpdateHumanResourcesLst(List<HumanResource> updateHumanResourcesLst) {
        this.updateHumanResourcesLst = updateHumanResourcesLst;
    }

    public List<RoleCtlg> getTypeLst() {
        return typeLst;
    }

    public void setTypeLst(List<RoleCtlg> typeLst) {
        this.typeLst = typeLst;
    }

    public boolean isEnebledHR() {
        return enebledHR;
    }

    public void setEnebledHR(boolean enebledHR) {
        this.enebledHR = enebledHR;
    }

    public String getEnebledId() {
        return enebledId;
    }

    public void setEnebledId(String enebledId) {
        this.enebledId = enebledId;
    }

    public List<HumanResource> getEnebledHumanResourcesLst() {
        return enebledHumanResourcesLst;
    }

    public void setEnebledHumanResourcesLst(List<HumanResource> enebledHumanResourcesLst) {
        this.enebledHumanResourcesLst = enebledHumanResourcesLst;
    }

    public boolean isDisabledHR() {
        return disabledHR;
    }

    public void setDisabledHR(boolean disabledHR) {
        this.disabledHR = disabledHR;
    }

    public String getDisabledId() {
        return disabledId;
    }

    public void setDisabledId(String disabledId) {
        this.disabledId = disabledId;
    }

    public List<HumanResource> getDisabledHumanResourcesLst() {
        return disabledHumanResourcesLst;
    }

    public void setDisabledHumanResourcesLst(List<HumanResource> disabledHumanResourcesLst) {
        this.disabledHumanResourcesLst = disabledHumanResourcesLst;
    }

    public boolean isDeletedHR() {
        return deletedHR;
    }

    public void setDeletedHR(boolean deletedHR) {
        this.deletedHR = deletedHR;
    }

    public String getDeletedId() {
        return deletedId;
    }

    public void setDeletedId(String deletedId) {
        this.deletedId = deletedId;
    }

    public List<HumanResource> getDeletedHumanResourcesLst() {
        return deletedHumanResourcesLst;
    }

    public void setDeletedHumanResourcesLst(List<HumanResource> deletedHumanResourcesLst) {
        this.deletedHumanResourcesLst = deletedHumanResourcesLst;
    }

    public String getBonusId() {
        return bonusId;
    }

    public void setBonusId(String bonusId) {
        this.bonusId = bonusId;
    }

    public List<Bonus> getBonuses() {
        return bonuses;
    }

    public void setBonuses(List<Bonus> bonuses) {
        this.bonuses = bonuses;
    }

    public String getUpdateBonusId() {
        return updateBonusId;
    }

    public void setUpdateBonusId(String updateBonusId) {
        this.updateBonusId = updateBonusId;
    }

    public List<String> getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(List<String> vehicleType) {
        this.vehicleType = vehicleType;
    }
}
