/*
 * 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.apc.web.beans.admin;

import com.arrebol.apc.controller.GenericController;
import com.arrebol.apc.controller.admin.StatsPaymentRouteController;
import com.arrebol.apc.controller.catalog.RouteController;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.views.StatsPaymentRouteView;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author David Rodriguez
 */
@Named("statsPaymentRouteManager")
@ViewScoped
public class StatsPaymentRouteBean extends GenericBean implements Serializable, Datatable {

    public void searchHistoricalAction() {
        try {
            if (getStarDate().after(getEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                setPayment(fillDatatablePayment());
                setTotalValue();
            }
        } catch (Exception e) {
        }
    }

    public BigDecimal searchTotalPaymentAction() {
        try {
            if (getStarDate().after(getEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                setTotalValue();
            }
        } catch (Exception e) {
        }
        return getTotalPayment();
    }

    public StatsPaymentRouteController getPaymentCtrl() {
        return paymentCtrl;
    }

    public void setPaymentCtrl(StatsPaymentRouteController paymentCtrl) {
        this.paymentCtrl = paymentCtrl;
    }

    public GenericController getGenericCtrl() {
        return genericCtrl;
    }

    public void setGenericCtrl(GenericController genericCtrl) {
        this.genericCtrl = genericCtrl;
    }

    public List<StatsPaymentRouteView> fillDatatablePayment() {
        return getPaymentCtrl().fillPaymentDataTable(getStarDate(), getEndDate(), getRouteSelectedId());
    }

    public BigDecimal fillTotalPayment() {
        return getPaymentCtrl().fillTotalPayment(getStarDate(), getEndDate());
    }

    public List<StatsPaymentRouteView> getPayment() {
        return payment;
    }

    public void setPayment(List<StatsPaymentRouteView> payment) {
        this.payment = payment;
    }

    public StatsPaymentRouteView getSelectedPayment() {
        return selectedPayment;
    }

    public void setSelectedPayment(StatsPaymentRouteView selectedPayment) {
        this.selectedPayment = selectedPayment;
    }

    public BigDecimal getTotalPayment() {
        return totalPayment;
    }

    public void setTotalPayment(BigDecimal totalPayment) {
        this.totalPayment = totalPayment;
    }

    public List<RouteCtlg> getRoutes() {
        return routes;
    }

    public RouteController getRouteCtrl() {
        return routeCtrl;
    }
    
    public void setRouteCtrl(RouteController routeCtrl) {
        this.routeCtrl = routeCtrl;
    }
    
    public String getRouteSelectedId() {
        return routeSelectedId;
    }

    public void setRouteSelectedId(String routeSelectedId) {
        this.routeSelectedId = routeSelectedId;
    }
    
    private StatsPaymentRouteController paymentCtrl;
    private GenericController genericCtrl;
    private RouteController routeCtrl;
    private List<StatsPaymentRouteView> payment;
    private StatsPaymentRouteView selectedPayment;
    private BigDecimal totalPayment;
    private List<RouteCtlg> routes;
    private String routeSelectedId;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        initStartAndEndDates(Calendar.DAY_OF_WEEK, 2);

        paymentCtrl = new StatsPaymentRouteController();
        genericCtrl = new GenericController();
        routeCtrl = new RouteController();
        payment = fillDatatablePayment();
        setTotalValue();
        routes = routeCtrl.fillRoutesDatatable(getLoggedUser().getOffice().getId());
    }

    @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) {
        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.
    }
    
    private void setTotalValue() {
        if (payment != null && !payment.isEmpty()) {
            setTotalPayment(payment.stream().map(StatsPaymentRouteView::getTotalPayment).reduce(BigDecimal::add).get());
        } else {
            setTotalPayment(BigDecimal.ZERO);
        }
    }
}
