/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.web.beans.dashboard;

import com.arrebol.apc.controller.admin.CustomerController;
import com.arrebol.apc.controller.admin.EndorsementController;
import com.arrebol.apc.controller.admin.LoanController;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.enums.LoanDetailsType;
import com.arrebol.apc.model.loan.Loan;
import com.arrebol.apc.model.loan.LoanDetails;
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.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Named(value = "loanDetailManager")
@ViewScoped
public class LoanDetailBean extends GenericBean implements Serializable, Datatable {

    public List<LoanDetails> getDetails(String id) {
        try {
            loanDetails = loanCtrl.getLoanDetailsbyId(id);
            getSaldoInsolutoAbono();
        } catch (Exception e) {
        }
        return null == loanDetails ? new ArrayList<>() : loanDetails;
    }
    
    public void getSaldoInsolutoAbono( ) {
        Double sumaAbonosAnteriores;
        Double totalAPagar;
        Double saldoInsoluto;
        Double saldoMultas;
        // 
        for (LoanDetails detail : loanDetails) {
            sumaAbonosAnteriores = loanDetails.stream().filter(p -> p.getReferenceNumber() <= detail.getReferenceNumber() && !p.getLoanDetailsType().equals(LoanDetailsType.FEE) )
                    .mapToDouble(LoanDetails::getAbonoD)
                    .sum();
            saldoMultas = loanDetails.stream().filter(p -> p.getReferenceNumber() <= detail.getReferenceNumber() && p.getLoanDetailsType().equals(LoanDetailsType.FEE))
                    .mapToDouble(LoanDetails::getAbonoD)
                    .sum();
            
            totalAPagar = detail.getLoan().getLoanType().getPaymentTotal().doubleValue();
            saldoInsoluto = totalAPagar - sumaAbonosAnteriores + saldoMultas;

            detail.setSaldoInsoluto(new BigDecimal(saldoInsoluto));
        }
    }

    public People getCustomerDetail(String peopleId) {
        return customerCtrl.findPeopleById(peopleId);
    }

    public List<Loan> getLoanByCustomer(String peopleId) {
        return customerCtrl.findLoanByCustomer(peopleId);
    }

    public List<Loan> getLoanByEndorsement(String peopleId) {
        return endorsementCtrl.findLoanByEndorsement(peopleId);
    }

    @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.
    }

    public CustomerController getCustomerCtrl() {
        return customerCtrl;
    }

    public void setCustomerCtrl(CustomerController customerCtrl) {
        this.customerCtrl = customerCtrl;
    }

    public People getCustomer() {
        return customer;
    }

    public void setCustomer(People customer) {
        this.customer = customer;
    }

    public List<Loan> getLoanCustomer() {
        return loanCustomer;
    }

    public void setLoanCustomer(List<Loan> loanCustomer) {
        this.loanCustomer = loanCustomer;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public LoanController getLoanCtrl() {
        return loanCtrl;
    }

    public void setLoanCtrl(LoanController loanCtrl) {
        this.loanCtrl = loanCtrl;
    }

    public String getLoanId() {
        return loanId;
    }

    public void setLoanId(String loanId) {
        this.loanId = loanId;
    }

    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
    }

    public EndorsementController getEndorsementCtrl() {
        return endorsementCtrl;
    }

    public void setEndorsementCtrl(EndorsementController endorsementCtrl) {
        this.endorsementCtrl = endorsementCtrl;
    }

    public People getEndorsement() {
        return endorsement;
    }

    public void setEndorsement(People endorsement) {
        this.endorsement = endorsement;
    }

    public String getEndorsementId() {
        return endorsementId;
    }

    public void setEndorsementId(String endorsementId) {
        this.endorsementId = endorsementId;
    }

    public List<Loan> getLoanEndorsement() {
        return loanEndorsement;
    }

    public void setLoanEndorsement(List<Loan> loanEndorsement) {
        this.loanEndorsement = loanEndorsement;
    }

    public Loan getSelectedLoanCustomer() {
        return selectedLoanCustomer;
    }

    public void setSelectedLoanCustomer(Loan selectedLoanCustomer) {
        this.selectedLoanCustomer = selectedLoanCustomer;
    }

    public List<LoanDetails> getLoanDetails() {
        return loanDetails;
    }

    public void setLoanDetails(List<LoanDetails> loanDetails) {
        this.loanDetails = loanDetails;
    }

    private CustomerController customerCtrl;
    private LoanController loanCtrl;
    private EndorsementController endorsementCtrl;

    private People customer;
    private Loan loan;
    private People endorsement;

    private String customerId;
    private String loanId;
    private String endorsementId;

    private List<Loan> loanEndorsement;
    private List<Loan> loanCustomer;
    private Loan selectedLoanCustomer;

    private List<LoanDetails> loanDetails;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        customerCtrl = new CustomerController();
        loanCtrl = new LoanController();
        endorsementCtrl = new EndorsementController();

        setLoanId(externalContext().getRequestParameterMap().get("selectedId"));
        loan = loanCtrl.getLoanById(getLoanId());
        customer = loan.getCustomer();
        endorsement = loan.getEndorsement();
        loanEndorsement = getLoanByEndorsement(loan.getEndorsement().getId());
        loanCustomer = getLoanByCustomer(loan.getCustomer().getId());
        
        if(loanCustomer.size() > 2){
            loanCustomer = getLoanByCustomer(loan.getCustomer().getId()).subList(0, 2);
        }
    }

}
