package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import main_package.DataBaseConnection;
import main_package.DataBaseMethodTaheer;
import main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.LoanInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="currentLoanReport")
@ViewScoped

public class CurrentLoanReportBean implements Serializable
{
	ArrayList<LoanInfo> list;
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();
	String loanType, searchType, frequency, dsaCode;
	double totalPrincipalAmount, totalPrincipalPaid, totalIntrestPaid, totalPrincipalBalance, totalPaid, totalLoanBalance, totalInterestBalance, totalInterestAmount, totalLoanAmt,totalAmount;
	boolean disAll;
	String userType,dealer;
	ArrayList<SelectItem> dealerList = new ArrayList<>();
	DataBaseMethodTaheer dbmt = new DataBaseMethodTaheer();
	String companyName;
	
	
	
	@ManagedBean(name = "currentLoanBean")
	@ViewScoped     
	
	public class CurrentLoanBean 	
	{
	    private List<LoanInfo> loanList;
	    
	    

	    public void setLoanList(List<LoanInfo> loanList) {
	        this.loanList = loanList;
	    }
	}
    	
	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

   
	int totalLoan;
	boolean show;
	ArrayList<SelectItem>  dsaList;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	DecimalFormat df=new DecimalFormat("##.##");
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	Date startDate=new Date(), endDate=new Date();
	
	
	
	public CurrentLoanReportBean()
	{
		Connection con = DataBaseConnection.JAVAConnection();
		
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
		branchId = (int) ss.getAttribute("branchId");
        companyName = dbmt.companyNameByBranchId(branchId);
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchList = new DataBaseMethods().branchList(branchUserId,con);
		dsaList=new DataBaseMethods().allDsaList(branchId,"Loan",con);
		String cc=(String) ss.getAttribute("companyName");
		
		userType= (String) ss.getAttribute("type");
		dealerList = dbmt.dealerList(con);
	    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);
	           
		try
		{
			con.close();
		} 
		catch (Exception e) {}
	}
	
	public void searchReport()
	{	
		if(branchId != 0)
		{   
			totalPrincipalAmount=totalPrincipalPaid=totalIntrestPaid=totalPrincipalBalance=totalPaid=totalLoanBalance=totalInterestBalance=totalInterestAmount=0;
			Connection conn=DataBaseConnection.JAVAConnection();
			
			if(companyName.equalsIgnoreCase("DELIGHTS MICRO CREDIT INDIA FOUNDATION"))
			{
				list = dbmt.currentLoanStatusList(branchId,loanType,conn, searchType, frequency, dsaCode, startDate, endDate, dealer);
                
			}
			else
			{
				list = obj1.currentLoanStatusList(branchId,loanType,conn, searchType, frequency, dsaCode, startDate, endDate);

			}
			
			if(list.size()>0)
			{
				show=true; 
				totalLoan=list.size();
				int sno=1;
				for(LoanInfo info:list)
				{
					info.setSno(sno++);
					totalPrincipalAmount = totalPrincipalAmount + Double.valueOf(info.getLoanAmount());
					totalIntrestPaid+=info.getIntrestPaid();
					totalPrincipalPaid+=info.getPrincipalPaid();
					totalPrincipalBalance+=info.getPrincipalBalance();
					totalInterestAmount+=Double.valueOf(info.getInterestAmount());
					
			        double manualCharge = info.getManualChargeAmount();
			        //System.out.println("manual charge is:"+manualCharge);
			        double principal= info.getPrincipalBalance();
			        System.out.println("principal is "+principal);
			        // Total amount = principal balance + manual charge
			        double totalAmount = principal + manualCharge;
			        //System.out.println("total amuont is :"+totalAmount);
			        info.setTotalAmount(totalAmount); // set in object				
				}
				
			/*	totalamount=Double.parseDouble(df.format(totalamount));
				totalIntrestPaid=Double.parseDouble(df.format(totalIntrestPaid));
				totalPrincipalPaid=Double.parseDouble(df.format(totalPrincipalPaid));
				totalPrincipalBalance=Double.parseDouble(df.format(totalPrincipalBalance));
				totalPaid=Double.parseDouble(df.format(totalPrincipalPaid+totalIntrestPaid));
				totalLoanBalance=Double.parseDouble(df.format(totalamount-totalPaid));
				totalInterestBalance=Double.parseDouble(df.format(totalLoanBalance-totalPrincipalBalance));
				totalInterestAmount=Double.parseDouble(df.format(totalInterestAmount));
				totalLoanAmt=Double.parseDouble(df.format(totalInterestAmount+totalamount));*/
				
				totalLoanAmt=Double.parseDouble(df.format(totalInterestAmount+totalPrincipalAmount));
				totalPrincipalAmount=Double.parseDouble(df.format(totalPrincipalAmount));
				totalInterestAmount=Double.parseDouble(df.format(totalInterestAmount));
				
				totalPaid=Double.parseDouble(df.format(totalPrincipalPaid+totalIntrestPaid));
				totalPrincipalPaid=Double.parseDouble(df.format(totalPrincipalPaid));
				totalIntrestPaid=Double.parseDouble(df.format(totalIntrestPaid));
				
				totalLoanBalance=Double.parseDouble(df.format(totalLoanAmt-totalPaid));
				totalPrincipalBalance=Double.parseDouble(df.format(totalPrincipalBalance));
				totalInterestBalance=Double.parseDouble(df.format(totalLoanBalance-totalPrincipalBalance));
				
				Collections.sort(list, Comparator.comparing(LoanInfo::getStrApprovalDate));
			}
			else
			{
				show = false;
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
			}
			
			try
			{
				conn.close();
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}	
	}
	
	
	
	public void print()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("list", list);		
		RequestContext.getCurrentInstance().execute("window.open('printCurrentLoanReport.xhtml')");
	}
	
	
	
	public void changeDSAListByBranch()
	{
		Connection conn = DataBaseConnection.JAVAConnection();
		dsaList = new DataBaseMethods().allDsaList(branchId,"Loan",conn);
		
		try 
		{
			conn.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
		
	
	
	public ArrayList<LoanInfo> getList() {
		return list;
	}

	public void setList(ArrayList<LoanInfo> list) {
		this.list = list;
	}



	public double getTotalPrincipalPaid() {
		return totalPrincipalPaid;
	}

	public void setTotalPrincipalPaid(double totalPrincipalPaid) {
		this.totalPrincipalPaid = totalPrincipalPaid;
	}

	public double getTotalIntrestPaid() {
		return totalIntrestPaid;
	}

	public void setTotalIntrestPaid(double totalIntrestPaid) {
		this.totalIntrestPaid = totalIntrestPaid;
	}

	public double getTotalPrincipalBalance() {
		return totalPrincipalBalance;
	}

	public void setTotalPrincipalBalance(double totalPrincipalBalance) {
		this.totalPrincipalBalance = totalPrincipalBalance;
	}

	public int getTotalLoan() {
		return totalLoan;
	}

	public void setTotalLoan(int totalLoan) {
		this.totalLoan = totalLoan;
	}

	public double getTotalPaid() {
		return totalPaid;
	}

	public void setTotalPaid(double totalPaid) {
		this.totalPaid = totalPaid;
	}


	public boolean isShow() {
		return show;
	}


	public void setShow(boolean show) {
		this.show = show;
	}


	public String getLoanType() {
		return loanType;
	}


	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}


	public String getSearchType() {
		return searchType;
	}


	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}


	public double getTotalLoanBalance() {
		return totalLoanBalance;
	}


	public void setTotalLoanBalance(double totalLoanBalance) {
		this.totalLoanBalance = totalLoanBalance;
	}


	public double getTotalInterestBalance() {
		return totalInterestBalance;
	}


	public void setTotalInterestBalance(double totalInterestBalance) {
		this.totalInterestBalance = totalInterestBalance;
	}


	public DataBaseReportMethods getObj1() {
		return obj1;
	}


	public void setObj1(DataBaseReportMethods obj1) {
		this.obj1 = obj1;
	}


	public DecimalFormat getDf() {
		return df;
	}


	public void setDf(DecimalFormat df) {
		this.df = df;
	}


	public double getTotalInterestAmount() {
		return totalInterestAmount;
	}


	public void setTotalInterestAmount(double totalInterestAmount) {
		this.totalInterestAmount = totalInterestAmount;
	}


	public double getTotalLoanAmt() {
		return totalLoanAmt;
	}


	public void setTotalLoanAmt(double totalLoanAmt) {
		this.totalLoanAmt = totalLoanAmt;
	}


	public String getFrequency() {
		return frequency;
	}


	public void setFrequency(String frequency) {
		this.frequency = frequency;
	}


	public int getBranchId() {
		return branchId;
	}


	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}


	public List<SelectItem> getBranchList() {
		return branchList;
	}


	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}


	public double getTotalPrincipalAmount() {
		return totalPrincipalAmount;
	}


	public void setTotalPrincipalAmount(double totalPrincipalAmount) {
		this.totalPrincipalAmount = totalPrincipalAmount;
	}


	public String getDsaCode() {
		return dsaCode;
	}


	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}


	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}


	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}


	public Date getStartDate() {
		return startDate;
	}


	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}


	public Date getEndDate() {
		return endDate;
	}


	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public boolean isDisAll() {
		return disAll;
	}

	public void setDisAll(boolean disAll) {
		this.disAll = disAll;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getDealer() {
		return dealer;
	}

	public void setDealer(String dealer) {
		this.dealer = dealer;
	}

	public ArrayList<SelectItem> getDealerList() {
		return dealerList;
	}

	public void setDealerList(ArrayList<SelectItem> dealerList) {
		this.dealerList = dealerList;
	}
	
	
	
}
