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

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.FeeByDriverController;
import com.arrebol.taxiservicios.model.admin.AvisosChofer;
import com.arrebol.taxiservicios.model.admin.FeeByDriverGroup;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.enums.BasicType;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
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
 */
@Named("feeByDriverGroupBean")
@ViewScoped
public class FeeByDriverGroupBean extends TaxiGenericBean implements Serializable, Datatable {

    public List<AvisosChofer> getDetails(String id) {
        logger.info("getDetails");
        try {
            setAvisos(getController().findAvisosChofer(id));

            logger.info("Avisos loaded size " + avisos != null ? avisos.size() : 0);
        } catch (Exception e) {
            logger.error("getDetails", e);
        }
        return null == avisos ? new ArrayList<>() : avisos;
    }
    
    public void fillDataTableBetweenDates() {
        try {
            if (getGenericStartDate().after(getGenericEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                if(getKardex() != null)
                    getKardex().clear();
                
                Calendar start = Calendar.getInstance();
                Calendar end = Calendar.getInstance();

                start.setTime(getGenericStartDate());
                start.set(Calendar.HOUR_OF_DAY, 7);
                start.set(Calendar.MINUTE, 30);
                start.set(Calendar.SECOND, 0);

                end.setTime(null == getGenericEndDate() ? getToday() : getGenericEndDate());
                end.set(Calendar.HOUR_OF_DAY, 23);
                end.set(Calendar.MINUTE, 59);
                end.set(Calendar.SECOND, 59);
                
                setKardex(getController().callReportFeeByDriverGroupToMySQLQuery(DATE_FORMAT.format(start.getTime()), DATE_FORMAT.format(end.getTime()), getLoggedUser().getLocation()));
                
                calcularTotales();
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }
    
    @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 onRowReorder(ReorderEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
    }

    @Override
    public void addRow() {
        
        AvisosChofer aviso = new AvisosChofer();
        aviso.setActiveStatus(BasicType.ENEBLED);
        aviso.setComments(comments);
        aviso.setCreatedBy(getLoggedUser());
        aviso.setCreatedOn(new Date());
        aviso.setId(UUID.randomUUID().toString());
        aviso.setPerson(new Person(personId));
        
        getController().saveAvisosByDriver(aviso);
        showMessage(FacesMessage.SEVERITY_INFO,
                        "Registro guardado",
                        "Se registró satisfactoriamente");
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public void calcularTotales(){
        
       totalPagado = 0F;
       totalAdeudo = 0F;
       numGeneradas = 0L;
       numPagadas = 0L;
       numPorPagar = 0L;
       numCondonadas = 0L;
       for(FeeByDriverGroup temp : kardex)
       {
           totalPagado += temp.getTotalFeePaid();
           totalAdeudo += temp.getTotalFeeToPaid();
           numGeneradas += temp.getNumFee();
           numPagadas += temp.getTotalNumFeePaid();
           numPorPagar += temp.getTotalNumFeeToPaid();
           numCondonadas += temp.getTotalNumFeeCondonadas();
       }
    }
    
    final Logger logger = LogManager.getLogger(FeeByDriverGroupBean.class);
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    
    FeeByDriverController controller;
    private Date startDate;
    private Date endDate;
    private List<FeeByDriverGroup> kardex;
    private List<AvisosChofer> avisos;
    private float totalPagado;
    private float totalAdeudo;
    private Long numGeneradas;
    private Long numPagadas;
    private Long numPorPagar;
    private Long numCondonadas;
    
    private String comments;
    private String personId;
    
    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setGenericUpDates(true);
            setController(new FeeByDriverController());
            kardex = new ArrayList();
            
            Calendar start = Calendar.getInstance();
                Calendar end = Calendar.getInstance();

                start.setTime(getGenericStartDate());
                start.set(Calendar.HOUR_OF_DAY, 7);
                start.set(Calendar.MINUTE, 30);
                start.set(Calendar.SECOND, 0);

                end.setTime(null == getGenericEndDate() ? getToday() : getGenericEndDate());
                end.set(Calendar.HOUR_OF_DAY, 23);
                end.set(Calendar.MINUTE, 59);
                end.set(Calendar.SECOND, 59);
                
                setKardex(getController().callReportFeeByDriverGroupToMySQLQuery(DATE_FORMAT.format(start.getTime()), DATE_FORMAT.format(end.getTime()), getLoggedUser().getLocation()));
            
            calcularTotales();

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

    public List<AvisosChofer> getAvisos() {
        return avisos;
    }

    public void setAvisos(List<AvisosChofer> avisos) {
        this.avisos = avisos;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public float getTotalPagado() {
        return totalPagado;
    }

    public void setTotalPagado(float totalPagado) {
        this.totalPagado = totalPagado;
    }

    public float getTotalAdeudo() {
        return totalAdeudo;
    }

    public void setTotalAdeudo(float totalAdeudo) {
        this.totalAdeudo = totalAdeudo;
    }

    public Long getNumGeneradas() {
        return numGeneradas;
    }

    public void setNumGeneradas(Long numGeneradas) {
        this.numGeneradas = numGeneradas;
    }

    public Long getNumPagadas() {
        return numPagadas;
    }

    public void setNumPagadas(Long numPagadas) {
        this.numPagadas = numPagadas;
    }

    public Long getNumPorPagar() {
        return numPorPagar;
    }

    public void setNumPorPagar(Long numPorPagar) {
        this.numPorPagar = numPorPagar;
    }

    public Long getNumCondonadas() {
        return numCondonadas;
    }

    public void setNumCondonadas(Long numCondonadas) {
        this.numCondonadas = numCondonadas;
    }
    
    public FeeByDriverController getController() {
        return controller;
    }

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

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public List<FeeByDriverGroup> getKardex() {
        return kardex;
    }

    public void setKardex(List<FeeByDriverGroup> kardex) {
        this.kardex = kardex;
    }
    
}
