/*
 * 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.admin;

import com.arrebol.apc.controller.GenericController;
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.controller.catalog.LoanTypeController;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.enums.LoanRenovationStatus;
import com.arrebol.apc.model.enums.LoanStatus;
import com.arrebol.apc.model.loan.Loan;
import com.arrebol.apc.model.loan.LoanByRenovation;
import com.arrebol.apc.model.loan.LoanDetails;
import com.arrebol.apc.model.loan.LoanType;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
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 Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Named("loanPendingDetailManager")
@ViewScoped
public class LoanPendingDetailBean  extends GenericBean implements Serializable, Datatable {
        
    public void changeLoanType(){
        loan.setLoanType(new LoanType(typeLoanId));
        loan.setAmountToPay(loanTypeCtrl.getLoanTypeById(typeLoanId).getPaymentTotal());
        if(loanCtrl.updateLoan(loan)){
           showMessage(FacesMessage.SEVERITY_INFO, "Cambio de tipo de préstamo", "El préstamo se cambió correctamente"); 
        }
        
    }
    
    public void changeDateAndApprovedLoan(){
        loan.setCreatedOn(createdOn);
        loan.setComments(comments);
        if(loanCtrl.updateLoan(loan)){
           approvedLoan();
        }
        
    }
    
    public List<LoanDetails> getDetails(String id) {
        try {
          loanDetails = loanCtrl.getLoanDetailsbyId(id);

        } catch (Exception e) {
        }
        return null == loanDetails ? new ArrayList<>() : loanDetails;
    }

    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);
    }
    
    public void rejectedLoan() {
        loanCtrl.updateLoanByStatus(LoanStatus.REJECTED, loan.getId(), getLoggedUser().getUser().getId());
        loanCtrl.updateLoanByUserByStatus(LoanStatus.REJECTED, loan);
        
        LoanByRenovation renovation;
        renovation = loanCtrl.getLoanByRenovationByIdLoanNew(loan);
        if(renovation != null)
        {
           loanCtrl.updateLoanByRenovationByStatus(LoanRenovationStatus.REJECTED, loan, getLoggedUser().getUser().getId());
           loanCtrl.updateLoanByStatus(LoanStatus.APPROVED, renovation.getLoanOld().getId(), getLoggedUser().getUser().getId());
           loanCtrl.updateLoanByUserByStatus(LoanStatus.APPROVED, renovation.getLoanOld());          
        }
        
        showMessage(FacesMessage.SEVERITY_INFO, "Cambio de estatus", "El préstamo se cambió a estatus 'Rechazado' de forma correcta.");
    }
    
    public void approvedLoan() {
        loanCtrl.updateLoanByStatus(LoanStatus.TO_DELIVERY, loan.getId(), getLoggedUser().getUser().getId());  
        loanCtrl.updateLoanByUserByStatus(LoanStatus.TO_DELIVERY, loan);
        showMessage(FacesMessage.SEVERITY_INFO, "Cambio de estatus", "El préstamo se cambió a estatus 'A conciliar' de forma correcta.");
    }
    
    @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;
    }

    public String getTypeLoanId() {
        return typeLoanId;
    }

    public void setTypeLoanId(String typeLoanId) {
        this.typeLoanId = typeLoanId;
    }

    public LoanTypeController getLoanTypeCtrl() {
        return loanTypeCtrl;
    }

    public void setLoanTypeCtrl(LoanTypeController loanTypeCtrl) {
        this.loanTypeCtrl = loanTypeCtrl;
    }

    public List<LoanType> getLoanType() {
        return loanType;
    }

    public void setLoanType(List<LoanType> loanType) {
        this.loanType = loanType;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public List<People> getCustomers() {
        return customers;
    }

    public void setCustomers(List<People> customers) {
        this.customers = customers;
    }

    public String getCustomerTest() {
        return customerTest;
    }

    public void setCustomerTest(String customerTest) {
        this.customerTest = customerTest;
    }
    
    private CustomerController customerCtrl;
    private LoanController loanCtrl;
    private EndorsementController endorsementCtrl;
    private LoanTypeController loanTypeCtrl;
    
    private People customer;
    private Loan loan;
    private People endorsement;
    
    private String customerId;
    private String loanId;
    private String endorsementId;
    private String typeLoanId;
    private String customerTest;
    
    private List<Loan> loanEndorsement;
    private List<Loan> loanCustomer;
    private Loan selectedLoanCustomer;
    private List<LoanType> loanType;
    private List<People> customers;

    private List<LoanDetails> loanDetails;
    
    private Date createdOn;
    private String comments;
    
    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        customerCtrl = new CustomerController();
        loanCtrl = new LoanController();
        endorsementCtrl = new EndorsementController();
        loanTypeCtrl = new LoanTypeController();
        
        setLoanId(externalContext().getRequestParameterMap().get("form:dtLoanPending_selection"));
        loan = loanCtrl.getLoanById(getLoanId());
        customer = loan.getCustomer();
        endorsement = loan.getEndorsement();
        loanEndorsement = getLoanByEndorsement(loan.getEndorsement().getId());
        loanCustomer = getLoanByCustomer(loan.getCustomer().getId()); 
        loanType = loanTypeCtrl.fillLoanTypeDatatable(getLoggedUser().getOffice().getId());
        customers = customerCtrl.fillCustomersDatatable(getLoggedUser().getOffice().getId());
    }
    
}
