/*
 * 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.StatsPaymentRenovationController;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.views.StatsPaymentRenovationView;
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("statsPaymentRenovationManager")
@ViewScoped
public class StatsPaymentRenovationBean 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 {
                setPaymentRenovation(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 StatsPaymentRenovationController getPaymentCtrl() {
        return paymentCtrl;
    }

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

    public GenericController getGenericCtrl() {
        return genericCtrl;
    }

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

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

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

    public List<StatsPaymentRenovationView> getPaymentRenovation() {
        return paymentRenovation;
    }

    public void setPaymentRenovation(List<StatsPaymentRenovationView> payment) {
        this.paymentRenovation = payment;
    }

    public StatsPaymentRenovationView getSelectedPaymentRenovation() {
        return selectedPaymentRenovation;
    }

    public void setSelectedPaymentRenovation(StatsPaymentRenovationView selectedPayment) {
        this.selectedPaymentRenovation = selectedPayment;
    }

    public BigDecimal getTotalPayment() {
        return totalPayment;
    }

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

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

    private StatsPaymentRenovationController paymentCtrl;
    private GenericController genericCtrl;
    private List<StatsPaymentRenovationView> paymentRenovation;
    private StatsPaymentRenovationView selectedPaymentRenovation;
    private BigDecimal totalPayment;
    private List<User> users;
    private String userSelectedId;

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

        paymentCtrl = new StatsPaymentRenovationController();
        genericCtrl = new GenericController();
        paymentRenovation = fillDatatablePayment();
        setTotalValue();
        users = getUsers();
    }

    @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 (paymentRenovation != null && !paymentRenovation.isEmpty()) {
            setTotalPayment(paymentRenovation.stream().map(StatsPaymentRenovationView::getTotalPayment).reduce(BigDecimal::add).get());
        } else {
            setTotalPayment(BigDecimal.ZERO);
        }
    }
}
