/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.beans.admin.employee;

import com.arrebol.taxiservicios.beans.GenericPersonBean;
import com.arrebol.taxiservicios.controller.admin.DriverController;
import com.arrebol.taxiservicios.model.enums.PersonType;
import com.arrebol.taxiservicios.model.view.PersonListView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
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 Picasso
 */
@Named("employeeListBean")
@ViewScoped
public class EmployeeListBean extends GenericPersonBean implements Serializable {

    private static final long serialVersionUID = 7563498302907468479L;
    final Logger logger = LogManager.getLogger(getClass());

    private List<PersonListView> employees;
    private PersonListView selectedEmployee;
    private String tipoEmpleado;

    @PostConstruct
    public void init() {
        try {
            loadBundlePropertyFile();
            setController(new DriverController());
            setTipoEmpleado(externalContext().getRequestParameterMap().get("tipoEmpleado"));
            if (getTipoEmpleado().equals("1")) {
                logger.info("Cargando ajustadores");
                setTipoEmpleado("1");
                setEmployees(getController().fillEmpleadosByLocationAndAjustador(getLoggedUser().getLocation(), Boolean.TRUE));
            } else {
                logger.info("Cargando empleados");
                setTipoEmpleado("0");
                setEmployees(getController().fillEmpleadosByLocationAndAjustador(getLoggedUser().getLocation(), Boolean.FALSE));
            }
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public void deleteEmployeeSelected() {
        logger.info("deleteEmployeeSelected");
        try {
            if (null == getLoggedUser().getPerson() || null == getLoggedUser().getPerson().getId() || getLoggedUser().getPerson().getId().equals(getSelectedEmployee().getId())) {
                showMessage(FacesMessage.SEVERITY_WARN, getBundlePropertyFile().getString("employee.delete.title"), getBundlePropertyFile().getString(null == getLoggedUser().getPerson() || null == getLoggedUser().getPerson().getId() ? "employee.delete.cannot.empty" : "employee.delete.cannot.delete"));
            } else {
                boolean success = getController().deletePersonById(getSelectedEmployee().getId(), getLoggedUser().getUserName());
                if (success) {
                    getEmployees().remove(getSelectedEmployee());
                    setSelectedEmployee(null);
                    showMessage(FacesMessage.SEVERITY_INFO, ("1".equals(getTipoEmpleado()) ? "Ajustador" : "Empleado") + " eliminado", ("1".equals(getTipoEmpleado()) ? "Ajustador" : "Empleado") + " eliminado correctamente");
                } else {
                    logger.error("Employee " + getSelectedEmployee() + "was not deleted");
                    showMessage(FacesMessage.SEVERITY_ERROR, "Error", "Ocurrió un error al eliminar el " + ("1".equals(getTipoEmpleado()) ? "ajustador" : "empleado"));
                }
            }
        } catch (Exception e) {
            logger.error("deleteEmployeeSelected: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL, "Error", "Ocurrió un error al eliminar el " + ("1".equals(getTipoEmpleado()) ? "ajustador" : "empleado"));
        }
    }

    public List<PersonListView> getEmployees() {
        if (null == employees) {
            employees = new ArrayList<>();
        }
        return employees;
    }

    public void setEmployees(List<PersonListView> employees) {
        this.employees = employees;
    }

    public PersonListView getSelectedEmployee() {
        return selectedEmployee;
    }

    public void setSelectedEmployee(PersonListView selectedEmployee) {
        this.selectedEmployee = selectedEmployee;
    }

    public String getTipoEmpleado() {
        return tipoEmpleado;
    }

    public void setTipoEmpleado(String tipoEmpleado) {
        this.tipoEmpleado = tipoEmpleado;
    }

}
