/*
 * 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.StatsOpeningFeesController;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.views.StatsOpeningFeesView;
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("statsOpeningFeesManager")
@ViewScoped
public class StatsOpeningFeesBean 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 {
                setOpeningFees(fillDatatableOpeningFees());
                setTotalValue();
            }
        } catch (Exception e) {
        }
    }

    public BigDecimal searchTotalOpeningFeesAction() {
        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 getTotalOpeningFees();
    }

    public StatsOpeningFeesController getOpeningFeesCtrl() {
        return openingFeesCtrl;
    }

    public void setOpeningFeesCtrl(StatsOpeningFeesController openingFeesCtrl) {
        this.openingFeesCtrl = openingFeesCtrl;
    }

    public GenericController getGenericCtrl() {
        return genericCtrl;
    }

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

    public List<StatsOpeningFeesView> fillDatatableOpeningFees() {
        return getOpeningFeesCtrl().fillOpeningFeesDataTable(getStarDate(), getEndDate(), getUserSelectedId());
    }

    public BigDecimal fillTotalOpeningFees() {
        return getOpeningFeesCtrl().fillTotalOpeningFees(getStarDate(), getEndDate());
    }

    public List<StatsOpeningFeesView> getopeningFees() {
        return openingFees;
    }

    public void setOpeningFees(List<StatsOpeningFeesView> openingFees) {
        this.openingFees = openingFees;
    }

    public StatsOpeningFeesView getSelectedOpeningFees() {
        return selectedOpeningFees;
    }

    public void setSelectedOpeningFees(StatsOpeningFeesView selectedOpeningFees) {
        this.selectedOpeningFees = selectedOpeningFees;
    }

    public BigDecimal getTotalOpeningFees() {
        return totalOpeningFees;
    }

    public void setTotalOpeningFees(BigDecimal totalOpeningFees) {
        this.totalOpeningFees = totalOpeningFees;
    }
    
    public List<User> getUsers() {
        return genericCtrl.getAllUsersByOffice(getLoggedUser().getOffice().getId());
    }
    
    public String getUserSelectedId() {
        return userSelectedId;
    }

    public void setUserSelectedId(String userSelectedId) {
        this.userSelectedId = userSelectedId;
    }

    private StatsOpeningFeesController openingFeesCtrl;
    private GenericController genericCtrl;
    private List<StatsOpeningFeesView> openingFees;
    private StatsOpeningFeesView selectedOpeningFees;
    private BigDecimal totalOpeningFees;
    private List<User> users;
    private String userSelectedId;

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

        openingFeesCtrl = new StatsOpeningFeesController();
        genericCtrl = new GenericController();
        openingFees = fillDatatableOpeningFees();
        setTotalValue();
    }

    @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 (openingFees != null && !openingFees.isEmpty()) {
            setTotalOpeningFees(openingFees.stream().map(StatsOpeningFeesView::getTotalOpeningFees).reduce(BigDecimal::add).get());
        } else {
            setTotalOpeningFees(BigDecimal.ZERO);
        }
    }
}
