/*
 * 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.StatsAdvancesController;
import com.arrebol.apc.controller.catalog.RouteController;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.views.StatsAdvancesView;
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("statsAdvancesManager")
@ViewScoped
public class StatsAdvancesBean 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 {
                setAdvances(fillDatatableAdvances());
                setTotalValue();
            }
        } catch (Exception e) {
        }
    }

    public BigDecimal searchTotalAdvancesAction() {
        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 getTotalAdvances();
    }

    public StatsAdvancesController getAdvancesCtrl() {
        return advancesCtrl;
    }

    public void setAdvancesCtrl(StatsAdvancesController advancesCtrl) {
        this.advancesCtrl = advancesCtrl;
    }

    public GenericController getGenericCtrl() {
        return genericCtrl;
    }

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

    public List<StatsAdvancesView> fillDatatableAdvances() {
        return getAdvancesCtrl().fillAdvancesDataTable(getStarDate(), getEndDate(), getRouteSelectedId());
    }

    public BigDecimal fillTotalAdvances() {
        return getAdvancesCtrl().fillTotalAdvances(getStarDate(), getEndDate());
    }

    public List<StatsAdvancesView> getAdvances() {
        return advances;
    }

    public void setAdvances(List<StatsAdvancesView> advances) {
        this.advances = advances;
    }

    public StatsAdvancesView getSelectedAdvances() {
        return selectedAdvances;
    }

    public void setSelectedAdvances(StatsAdvancesView selectedAdvances) {
        this.selectedAdvances = selectedAdvances;
    }

    public BigDecimal getTotalAdvances() {
        return totalAdvances;
    }

    public void setTotalAdvances(BigDecimal totalAdvances) {
        this.totalAdvances = totalAdvances;
    }

    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 StatsAdvancesController advancesCtrl;
    private GenericController genericCtrl;
    private RouteController routeCtrl;
    private List<StatsAdvancesView> advances;
    private StatsAdvancesView selectedAdvances;
    private BigDecimal totalAdvances;
    private List<RouteCtlg> routes;
    private String routeSelectedId;

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

        advancesCtrl = new StatsAdvancesController();
        genericCtrl = new GenericController();
        routeCtrl = new RouteController();
        advances = fillDatatableAdvances();
        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 (advances != null && !advances.isEmpty()) {
            setTotalAdvances(advances.stream().map(StatsAdvancesView::getTotalAdvances).reduce(BigDecimal::add).get());
        } else {
            setTotalAdvances(BigDecimal.ZERO);
        }
    }
}
