/*
 * 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.Datatable;
import com.arrebol.taxiservicios.beans.GenericPersonBean;
import com.arrebol.taxiservicios.controller.admin.DriverController;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
import com.arrebol.taxiservicios.model.view.DriverInactiveAndBlackListView;
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;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named("driversBlackListBean")
@ViewScoped
public class DriversBlackListBean extends GenericPersonBean implements Serializable, Datatable {

    public void returnToPoolDriver() {
        logger.info("returnToPoolDriver");
        try {
            boolean success = getController().returnToPoolBlackListDriver(getSelectedDriver().getId());

            if (success) {
                setDrivers(getController().findListOfInactiveDriversByType(GenericEnumType.ENABLED, getLoggedUser().getLocation()));
                setSelectedDriver(null);

                showMessage(FacesMessage.SEVERITY_INFO,
                        "Regreso a banca",
                        "Se regreso correctamente el chofer de lista negra a banca");

            } else {
                logger.error("Driver " + getSelectedDriver() + "was not deleted");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("driver.active.title"),
                        getBundlePropertyFile().getString("driver.active.fail.desc"));
            }

        } catch (Exception e) {
            logger.error("returnToPoolDriver: " + e);

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

    @Override
    public void editRow(RowEditEvent event) {
        DriverInactiveAndBlackListView person = (DriverInactiveAndBlackListView) event.getObject();
        try {
            if (person != null) {
                if (getController().editBlackListDriver(person.getId(), person.getBlackListDesc(), person.getBlackListAdeudo(), getLoggedUser())) {
                    setDrivers(getController().findListOfInactiveDriversByType(GenericEnumType.ENABLED, getLoggedUser().getLocation()));
                    showMessage(FacesMessage.SEVERITY_INFO, "Edición", "Se hizo el cambio correctamente");
                } else {
                    showMessage(FacesMessage.SEVERITY_ERROR, "Edición", "No se edito chofer");
                }
            } else {
                showMessage(FacesMessage.SEVERITY_WARN, "Edición", "El chofer no se selecciono");
            }
        } catch (Exception e) {
            showMessage(FacesMessage.SEVERITY_ERROR, "Edición", "Error al editar el registro selecionado");
        }
    }

    @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 addRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

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

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

            setController(new DriverController());
            setDrivers(getController().findListOfInactiveDriversByType(GenericEnumType.ENABLED, getLoggedUser().getLocation()));
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    private static final long serialVersionUID = 6166262474494793034L;

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

    private List<DriverInactiveAndBlackListView> drivers;
    private DriverInactiveAndBlackListView selectedDriver;
    private String blackListDescription;

    public List<DriverInactiveAndBlackListView> getDrivers() {
        if (null == drivers) {
            drivers = new ArrayList<>();
        }
        return drivers;
    }

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

    public DriverInactiveAndBlackListView getSelectedDriver() {
        return selectedDriver;
    }

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

    public String getBlackListDescription() {
        return blackListDescription;
    }

    public void setBlackListDescription(String blackListDescription) {
        this.blackListDescription = blackListDescription;
    }

}
