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

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.FeeByDriverAdminViewController;
import com.arrebol.taxiservicios.model.enums.FeeType;
import com.arrebol.taxiservicios.model.view.PersonListView;
import com.arrebol.taxiservicios.model.view.admin.FeeByDriverAdminView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
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.SelectEvent;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named("feeBean")
@ViewScoped
public class FeeBean extends TaxiGenericBean implements Serializable {

    /**
     *
     * @param likeFullName
     * @return
     */
    public List<PersonListView> likeDriverName(String likeFullName) {
        try {
            return getController().likeDriverName("%" + likeFullName + "%");
        } catch (Exception e) {
            logger.error("likeDriverName", e);
            return new ArrayList<>();
        }
    }

    /**
     *
     * @param event
     */
    public void onDriverSelected(SelectEvent event) {
        try {
            setDriverId(((PersonListView) event.getObject()).getId());
            //setListFromView(getController().searchFees(getDriverId(), getGenericStartDate(), getGenericEndDate(), getSelectedFeeTypes()));
        } catch (Exception e) {
            logger.error("onDriverSelected", e);
        }
    }
    
    public void getPorcentajeEfectividad(){
        porcentaje = ((numTotalNoPagadas*100.0)/(numTotal - numTotalCondonadas)) - 100.0;
    }

    /**
     *
     */
    public void searchFees() {
        try {
            if (getDriverId() == null || getDriverId().length() != 36) {
                setDriverId(null);
            }
            setListFromView(getController().searchFees(getDriverId(), getGenericStartDate(), getGenericEndDate(), getSelectedFeeTypes(), getLoggedUser().getLocation()));
            total = 0D;
            totalPagadas = 0D;
            totalCondonadas = 0D;
            totalNoPagadas = 0D;
            numTotal = 0;
            numTotalCondonadas = 0;
            numTotalNoPagadas = 0;
            numTotalPagadas = 0;
            porcentaje = 0;
            if(getListFromView() != null && !getListFromView().isEmpty())
            {
                for(FeeByDriverAdminView fees : getListFromView())
                {
                    total += fees.getAmount();
                    numTotal ++;
                    if(fees.getStatus() == FeeType.PAID)
                    {
                        totalPagadas += fees.getAmount();
                        numTotalPagadas ++;
                    }
                    if(fees.getStatus() == FeeType.NO_PAID)
                    {
                        totalNoPagadas += fees.getAmount();
                        numTotalNoPagadas ++;
                    }
                    if(fees.getStatus() == FeeType.DELETED)
                    {
                        totalCondonadas += fees.getAmount();
                        numTotalCondonadas ++;
                    }
                }
                getPorcentajeEfectividad();
            }
        } catch (Exception e) {
            logger.error("findFeesBetweenDates", e);
        }
    }

    /**
     *
     */
    public void findFeesInStatus() {
        try {
            setListFromView(getController().findFeesInStatus(getFeeTypes(), getLoggedUser().getLocation()));
            total = 0D;
            totalPagadas = 0D;
            totalCondonadas = 0D;
            totalNoPagadas = 0D;
            numTotal = 0;
            numTotalCondonadas = 0;
            numTotalNoPagadas = 0;
            numTotalPagadas = 0;
            porcentaje = 0;
            if(getListFromView() != null && !getListFromView().isEmpty())
            {
                for(FeeByDriverAdminView fees : getListFromView())
                {
                    total += fees.getAmount();
                    numTotal ++;
                    if(fees.getStatus() == FeeType.PAID)
                    {
                        totalPagadas += fees.getAmount();
                        numTotalPagadas ++;
                    }
                    if(fees.getStatus() == FeeType.NO_PAID)
                    {
                        totalNoPagadas += fees.getAmount();
                        numTotalNoPagadas ++;
                    }
                    if(fees.getStatus() == FeeType.DELETED)
                    {
                        totalCondonadas += fees.getAmount();
                        numTotalCondonadas ++;
                    }
                }
                getPorcentajeEfectividad();
            }
        } catch (Exception e) {
            logger.error("findFeesInStatus", e);
        }
    }

    @Deprecated
    public void findFeesByDriver() {
        try {
            setListFromView(getController().findFeesByDriver(getDriverId(), getLoggedUser().getLocation()));
            total = 0D;
            if(getListFromView() != null && !getListFromView().isEmpty())
            {
                for(FeeByDriverAdminView fees : getListFromView())
                {
                    total += fees.getAmount();
                }
            }
        } catch (Exception e) {
            logger.error("findFeesByDriver", e);
        }
    }

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

            setController(new FeeByDriverAdminViewController());
            setFeeTypes(Arrays.asList(FeeType.values()));
            total = 0D;
            totalPagadas = 0D;
            totalNoPagadas = 0D;
            totalCondonadas = 0D;
            numTotal = 0;
            numTotalCondonadas = 0;
            numTotalNoPagadas = 0;
            numTotalPagadas = 0;
            porcentaje = 0;
        } catch (Exception e) {
            logger.error("FeeBean init", e);
        }
    }

    private static final long serialVersionUID = -1L;

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

    private FeeByDriverAdminViewController controller;
    private List<FeeByDriverAdminView> listFromView;
    private List<PersonListView> drivers;
    private String driverId;
    private String likeFullName;
    private List<FeeType> feeTypes;
    private List<FeeType> selectedFeeTypes;
    private PersonListView selectedDriver;
    private double total;
    private double totalPagadas;
    private double totalNoPagadas;
    private double totalCondonadas;
    private int numTotal;
    private int numTotalPagadas;
    private int numTotalNoPagadas;
    private int numTotalCondonadas;
    private double porcentaje;

    public double getPorcentaje() {
        return porcentaje;
    }

    public void setPorcentaje(double porcentaje) {
        this.porcentaje = porcentaje;
    }

    public int getNumTotal() {
        return numTotal;
    }

    public void setNumTotal(int numTotal) {
        this.numTotal = numTotal;
    }

    public int getNumTotalPagadas() {
        return numTotalPagadas;
    }

    public void setNumTotalPagadas(int numTotalPagadas) {
        this.numTotalPagadas = numTotalPagadas;
    }

    public int getNumTotalNoPagadas() {
        return numTotalNoPagadas;
    }

    public void setNumTotalNoPagadas(int numTotalNoPagadas) {
        this.numTotalNoPagadas = numTotalNoPagadas;
    }

    public int getNumTotalCondonadas() {
        return numTotalCondonadas;
    }

    public void setNumTotalCondonadas(int numTotalCondonadas) {
        this.numTotalCondonadas = numTotalCondonadas;
    }
    
    

    public FeeByDriverAdminViewController getController() {
        return controller;
    }

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

    public List<FeeByDriverAdminView> getListFromView() {
        return listFromView;
    }

    public void setListFromView(List<FeeByDriverAdminView> listFromView) {
        this.listFromView = listFromView;
    }

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

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

    public String getDriverId() {
        return driverId;
    }

    public void setDriverId(String driverId) {
        this.driverId = driverId;
    }

    public String getLikeFullName() {
        return likeFullName;
    }

    public void setLikeFullName(String likeFullName) {
        this.likeFullName = likeFullName;
    }

    public List<FeeType> getFeeTypes() {
        return feeTypes;
    }

    public void setFeeTypes(List<FeeType> feeTypes) {
        this.feeTypes = feeTypes;
    }

    public List<FeeType> getSelectedFeeTypes() {
        if (null == selectedFeeTypes) {
            selectedFeeTypes = new ArrayList<>();
        }
        return selectedFeeTypes;
    }

    public void setSelectedFeeTypes(List<FeeType> selectedFeeTypes) {
        this.selectedFeeTypes = selectedFeeTypes;
    }

    public PersonListView getSelectedDriver() {
        return selectedDriver;
    }

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

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public double getTotalPagadas() {
        return totalPagadas;
    }

    public void setTotalPagadas(double totalPagadas) {
        this.totalPagadas = totalPagadas;
    }

    public double getTotalNoPagadas() {
        return totalNoPagadas;
    }

    public void setTotalNoPagadas(double totalNoPagadas) {
        this.totalNoPagadas = totalNoPagadas;
    }

    public double getTotalCondonadas() {
        return totalCondonadas;
    }

    public void setTotalCondonadas(double totalCondonadas) {
        this.totalCondonadas = totalCondonadas;
    }

}
