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

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.TaxiController;
import com.arrebol.taxiservicios.model.admin.Taxi;
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;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author oscar vargas
 */
@Named("taxiListBean")
@ViewScoped
public class TaxiListBean extends TaxiGenericBean implements Serializable, Datatable {

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

    @Override
    public void onRowReorder(ReorderEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
    }

    @Override
    public void deleteRow() {
        logger.info("deleteRow");
        try {

            boolean success = getController().deleteTaxiById(
                    getSelectedTaxi().getId(), getLoggedUser().getId(), commentsBitacora);

            if (success) {
                getTaxi().clear();
                setTaxi(getController().findAllTaxis(getLoggedUser().getLocation().getId()));

                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("taxi.delete.title"),
                        getBundlePropertyFile().getString("taxi.delete.success.desc"));
            } else {
                logger.error("Taxi " + getSelectedTaxi() + "was not deleted");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("taxi.delete.title"),
                        getBundlePropertyFile().getString("taxi.delete.fail.desc"));
            }

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

            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("taxi.delete.title"),
                    getBundlePropertyFile().getString("taxi.delete.exception.desc"));
        }
    }
    
    public void activeRow() {
        logger.info("activeRow");
        try {

            boolean success = getController().activeTaxiById(
                    getSelectedTaxi().getId(), getLoggedUser().getId());

            if (success) {
                getTaxi().clear();
                setTaxi(getController().findAllTaxis(getLoggedUser().getLocation().getId()));

                showMessage(FacesMessage.SEVERITY_INFO,
                        "Vehículo activado",
                        "Se activo correctamente");
            } else {
                logger.error("Taxi " + getSelectedTaxi() + "was not active");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        "Vehículo activado",
                        "Ocurrió un error al activar el vehículo");
            }

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

            showMessage(FacesMessage.SEVERITY_FATAL,
                    "Vehículo activado",
                    "Ocurrió un error al activar el vehículo");
        }
    }

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

    private static final long serialVersionUID = 3037927455450369703L;

    final Logger logger = LogManager.getLogger(TaxiListBean.class);

    TaxiController controller;
    private List<Taxi> taxi;
    private List<Taxi> banca;
    private List<Taxi> sinContrato;
    private List<Taxi> activos;
    private List<Taxi> taller;
    private List<Taxi> apc;
    private List<Taxi> tallerExterno;
    private Taxi selectedTaxi;
    private int totalActives;
    private int totalBanca;
    private int totalDeleted;
    private int totalTaller;
    private String commentsBitacora;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new TaxiController());
            setTaxi(getController().findAllTaxis(getLoggedUser().getLocation().getId()));
            setBanca(getController().findAllTaxisBanca(getLoggedUser().getLocation().getId()));
            setSinContrato(getController().findAllTaxisSinContrato(getLoggedUser().getLocation().getId()));
            setActivos(getController().findAllTaxisActivos(getLoggedUser().getLocation().getId()));
            setTaller(getController().findAllTaxisTaller(getLoggedUser().getLocation().getId()));
            setApc(getController().findAllTaxisApc(getLoggedUser().getLocation().getId()));
            setTallerExterno(getController().findAllTaxisTallerExterno(getLoggedUser().getLocation().getId()));

            totalDeleted = getSinContrato() == null ? 0 : getSinContrato().size();
            totalBanca = getBanca() == null ? 0 : getBanca().size();
            totalActives = getActivos() == null ? 0 : getActivos().size();
            
            if(getTaller() != null && !getTaller().isEmpty())
            {
                totalTaller = getTaller().size();
                
            }

            logger.info("Taxis loaded");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public List<Taxi> getTallerExterno() {
        return tallerExterno;
    }

    public void setTallerExterno(List<Taxi> tallerExterno) {
        this.tallerExterno = tallerExterno;
    }

    public List<Taxi> getApc() {
        return apc;
    }

    public void setApc(List<Taxi> apc) {
        this.apc = apc;
    }

    public List<Taxi> getTaller() {
        return taller;
    }

    public void setTaller(List<Taxi> taller) {
        this.taller = taller;
    }

    public List<Taxi> getActivos() {
        return activos;
    }

    public void setActivos(List<Taxi> activos) {
        this.activos = activos;
    }

    public List<Taxi> getSinContrato() {
        return sinContrato;
    }

    public void setSinContrato(List<Taxi> sinContrato) {
        this.sinContrato = sinContrato;
    }

    public List<Taxi> getBanca() {
        return banca;
    }

    public void setBanca(List<Taxi> banca) {
        this.banca = banca;
    }

    public int getTotalActives() {
        return totalActives;
    }

    public void setTotalActives(int totalActives) {
        this.totalActives = totalActives;
    }

    public int getTotalBanca() {
        return totalBanca;
    }

    public void setTotalBanca(int totalBanca) {
        this.totalBanca = totalBanca;
    }

    public int getTotalDeleted() {
        return totalDeleted;
    }

    public void setTotalDeleted(int totalDeleted) {
        this.totalDeleted = totalDeleted;
    }

    public TaxiController getController() {
        return controller;
    }

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

    public List<Taxi> getTaxi() {
        return taxi;
    }

    public void setTaxi(List<Taxi> taxi) {
        this.taxi = taxi;
    }

    public Taxi getSelectedTaxi() {
        return selectedTaxi;
    }

    public void setSelectedTaxi(Taxi selectedTaxi) {
        this.selectedTaxi = selectedTaxi;
    }

    public String getCommentsBitacora() {
        return commentsBitacora;
    }

    public void setCommentsBitacora(String commentsBitacora) {
        this.commentsBitacora = commentsBitacora;
    }

    public int getTotalTaller() {
        return totalTaller;
    }

    public void setTotalTaller(int totalTaller) {
        this.totalTaller = totalTaller;
    }

}
