package com.arrebol.apc.web.beans.admin;

import com.arrebol.apc.controller.GenericController;
import com.arrebol.apc.controller.admin.FeesController;
import com.arrebol.apc.controller.catalog.RouteController;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.views.FeesView;
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.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;

/*
 * 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.
 */
/**
 *
 * @author David Rodriguez
 */
@Named("feesManager")
@ViewScoped
public class FeesBean 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 {
                setFees(fillDatatableFees());
                setTotalValue();
            }
        } catch (Exception e) {
        }
    }

    public void searchHistoricalActionByRoute() {
        try {
            if (getStarDate().after(getEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                setFees(fillDatatableFeesByRoute());
                setTotalValue();
            }
        } catch (Exception e) {
        }
    }
    
    public BigDecimal searchTotalFeesAction() {
        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 getTotalFees();
    }

    public FeesController getFeesCtrl() {
        return feesCtrl;
    }

    public void setFeesCtrl(FeesController feesCtrl) {
        this.feesCtrl = feesCtrl;
    }

    public GenericController getGenericCtrl() {
        return genericCtrl;
    }

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

    public List<FeesView> fillDatatableFees() {
        return getFeesCtrl().fillFeesDataTable(getStarDate(), getEndDate(), getUserSelectedId());
    }
    
    public List<FeesView> fillDatatableFeesByRoute() {
        return getFeesCtrl().fillFeesDataTableByRoute(getStarDate(), getEndDate(), getRouteSelectedId());
    }

    public BigDecimal fillTotalFees() {
        return getFeesCtrl().fillTotalFees(getStarDate(), getEndDate());
    }

    public List<FeesView> getFees() {
        return fees;
    }

    public void setFees(List<FeesView> fees) {
        this.fees = fees;
    }

    public FeesView getSelectedFees() {
        return selectedFees;
    }

    public void setSelectedFees(FeesView selectedFees) {
        this.selectedFees = selectedFees;
    }

    public BigDecimal getTotalFees() {
        return totalFees;
    }

    public void setTotalFees(BigDecimal totalFees) {
        this.totalFees = totalFees;
    }
    
    public List<User> getUsers() {
        return genericCtrl.getAllUsersByOffice(getLoggedUser().getOffice().getId());
    }

    public String getUserSelectedId() {
        return userSelectedId;
    }

    public void setUserSelectedId(String userSelectedId) {
        this.userSelectedId = userSelectedId;
    }
    
    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 FeesController feesCtrl;
    private GenericController genericCtrl;
    private List<FeesView> fees;
    private FeesView selectedFees;
    private BigDecimal totalFees;
    
    private RouteController routeCtrl;
    private List<RouteCtlg> routes;
    private String routeSelectedId;
    
    private List<User> users;
    private String userSelectedId;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        initOneWeekBeforeToCurrdate();

        feesCtrl = new FeesController();
        genericCtrl = new GenericController();
        routeCtrl = new RouteController();
        fees = fillDatatableFees();
        setTotalValue();
        routes = routeCtrl.fillRoutesDatatable(getLoggedUser().getOffice().getId());
    }

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