/*
 * 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.admin.driver;

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.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 Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named("driversListBean")
@ViewScoped
public class DriversListBean extends GenericPersonBean implements Serializable {

    /**
     *
     * @param outcome
     * @return
     */
    public String editDriver(String outcome) {
        return outcome;
    }

    public void deleteSelectedDriver() {
        logger.info("deleteRow");
        try {
            if (null == getLoggedUser().getPerson()
                    || null == getLoggedUser().getPerson().getId()
                    || getLoggedUser().getPerson().getId().equals(getSelectedDriver().getId())) {
                showMessage(FacesMessage.SEVERITY_WARN,
                        getBundlePropertyFile().getString("driver.delete.title"),
                        getBundlePropertyFile().getString(
                                null == getLoggedUser().getPerson()
                                || null == getLoggedUser().getPerson().getId()
                                ? "driver.delete.cannot.empty"
                                : "driver.delete.cannot.delete"
                        ));
            } else {
                String tmpId = getSelectedDriver().getId();
                boolean success = getController().updateStatusDriverAndRemoveFromTaxi(
                        getSelectedDriver().getId(), getLoggedUser());

                if (success) {
                    getDrivers().remove(getSelectedDriver());
                    setSelectedDriver(null);

                    showMessage(FacesMessage.SEVERITY_INFO,
                            getBundlePropertyFile().getString("driver.released.title"),
                            getBundlePropertyFile().getString("driver.released.success.desc"));

                    if (!getController().deteleToCollectByDriverId(tmpId)) {
                        logger.error("To collect was not deleted");
                        showMessage(FacesMessage.SEVERITY_ERROR,
                                getBundlePropertyFile().getString("driver.delete.title"),
                                getBundlePropertyFile().getString("sale.delete.all.to.collect.fail.desc"));
                    }
                } else {
                    logger.error("Provider " + getSelectedDriver() + "was not deleted");
                    showMessage(FacesMessage.SEVERITY_ERROR,
                            getBundlePropertyFile().getString("driver.delete.title"),
                            getBundlePropertyFile().getString("driver.delete.fail.desc"));
                }
            }
        } catch (Exception e) {
            logger.error("deleteRow: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("driver.delete.title"),
                    getBundlePropertyFile().getString("driver.delete.exception.desc"));
        }
    }

    @PostConstruct
    public void init() {
        try {
            loadBundlePropertyFile();

            setController(new DriverController());
            setDrivers(getController().findListOfPersonsByType(PersonType.DRIVER, getLoggedUser().getLocation()));
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    private static final long serialVersionUID = 6166262474494793034L;

    final Logger logger = LogManager.getLogger(getClass());

    private List<PersonListView> drivers;
    private PersonListView selectedDriver;

    public List<PersonListView> getDrivers() {
        return drivers;
    }

    public void setDrivers(List<PersonListView> drivers) {
        this.drivers = drivers;
    }

    public PersonListView getSelectedDriver() {
        return selectedDriver;
    }

    public void setSelectedDriver(PersonListView selectedDriver) {
        this.selectedDriver = selectedDriver;
    }

}
