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.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import main_package.DataBaseMethods;
import main_package.DataBaseConnection;
import main_package.LoanInfo;

@ManagedBean(name="loanPaymentReport")
@ViewScoped

public class LoanPaymentReportBean implements Serializable
{
	ArrayList<LoanInfo> list;
	Date startdate,enddate;
	boolean show;
	int totalEntries=0;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	double totAmountPersonal,totAmountProperty,totAmountVehicle,totAmountGroup,totAssets,balance;
	double paidAmountPersonal,paidAmountProperty,paidAmountVehicle,paidAmountGroup,dueAmount,totalInterest=0,totalPrincipal=0;
	DecimalFormat df=new DecimalFormat("##.##");
	
	public LoanPaymentReportBean() 
	{
		startdate=new Date(); enddate=new Date();
		startdate.setMonth(startdate.getMonth()-1);
		list=new ArrayList<>();
		/*list=obj1.showLoanPaymentReport();
		totalEntries=list.size();*/
		/*general(list);
		Collections.sort(list);
			*/
	}
	public void general(ArrayList<LoanInfo> list2) 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		totalInterest=totalPrincipal=totAssets=balance=0;
		//double tamnt=0;
		int i=0;
		for(LoanInfo ll: list2)
		{
			if(ll.getLoanType().equals("personalloan"))
			{
				totAmountPersonal=obj1.obtainTotalAmountWithInterest(ll.getLoanType(),ll.getLoanNo());
				if(i!=0)
				{
					if(!(ll.getLoanNo().equals(list2.get(i-1).getLoanNo())))
					{
						paidAmountPersonal=0;
					}
				}
				paidAmountPersonal+=ll.getPrincipal();
				if(ll.getLoanNo().equals("PL-000106"))
				{
					// System.out.println("loan NO...."+ll.getLoanNo()+".......Tamount"+totAmountPersonal+"........installment......."+paidAmountPersonal+".....Paydate"+ll.getStrRequestDate());
						
				}
				
				ll.setDueAmount(Double.parseDouble(df.format(totAmountPersonal-paidAmountPersonal)));
			}
			else if(ll.getLoanType().equals("propertyloan"))
			{
				if(i!=0)
				{
					if(!(ll.getLoanNo().equals(list2.get(i-1).getLoanNo())))
					{
						paidAmountProperty=0;
					}
				}
				
				totAmountProperty=obj1.obtainTotalAmountWithInterest(ll.getLoanType(),ll.getLoanNo());
				paidAmountProperty+=ll.getPrincipal();
				ll.setDueAmount(totAmountProperty-paidAmountProperty);
			}
			else if(ll.getLoanType().equals("vechicleloan"))
			{
				
				if(i!=0)
				{
					if(!(ll.getLoanNo().equals(list2.get(i-1).getLoanNo())))
					{
						paidAmountVehicle=0;
					}
				}
				totAmountVehicle=obj1.obtainTotalAmountWithInterest(ll.getLoanType(),ll.getLoanNo());
				paidAmountVehicle+=ll.getPrincipal();
				ll.setDueAmount(totAmountVehicle-paidAmountVehicle);
			}
			else if(ll.getLoanType().equals("grouploan"))
			{
				if(i!=0)
				{
					if(!(ll.getLoanNo().equals(list2.get(i-1).getLoanNo())))
					{
						paidAmountGroup=0;
					}
				}
				
				totAmountGroup=obj1.obtainTotalAmountWithInterest(ll.getLoanType(),ll.getLoanNo());
				paidAmountGroup+=ll.getPrincipal();
				// System.out.println("emi : "+ll.getInstallAmount());
				ll.setDueAmount(totAmountGroup-paidAmountGroup);
			}
			
			totalInterest+=ll.getInterest();
			totalPrincipal+=ll.getPrincipal();
			
			i++;
		}
		totAssets+=obj1.totalCurrentAssetLoan(branchId);
		balance=totAssets-totalPrincipal;
		
		
		totalInterest=Double.parseDouble(df.format(totalInterest));
		totalPrincipal=Double.parseDouble(df.format(totalPrincipal));
		balance=Double.parseDouble(df.format(balance));
		
		/*String loanNo="";
		boolean chk=false;
		for(int k=0; k<list.size();k++)
		{
			if(chk==false)
			{
				loanNo=list.get(k).getLoanNo();
				chk=true;
			}
			
			if(list.get(k).getLoanNo().equals(loanNo))
			{
				double due=list.get(k).getDueAmount();
				double totPaid=new DataBase().loanPaidAmountByUser(list.get(k).getLoanType(), loanNo);
				double diff=totPaid-due;
				// System.out.println("tot paid : "+totPaid+" now : "+due);
				list.get(k).setDueAmount(Double.parseDouble(df.format(due-diff)));
				break;
			}
			else
			{
				loanNo=list.get(k).getLoanNo();
			}
			
		}
		*/
		
		//Collections.sort(list2);
		
		ArrayList<LoanInfo> tempList=new ArrayList<>();
		
		for(LoanInfo ll: list)
		{
			if((ll.getRequestDate().equals(startdate) || ll.getRequestDate().after(startdate)) && (ll.getRequestDate().equals(enddate) || ll.getRequestDate().before(enddate)))
			{
				tempList.add(ll);
			}
		}
		
		list=new ArrayList<>();
		list=tempList;
		tempList=new ArrayList<>();
		// System.out.println("final Entry: "+list.size());
	}
	
	public void view()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		show=true;
		//list=new ArrayList<>();
		Connection conn=DataBaseConnection.JAVAConnection();
		list=obj1.showLoanPaymentReportByDate(branchId,startdate,enddate,conn);
		totalEntries=list.size();
		general(list);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		//Collections.sort(list);

	}
	
	public String print()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("interest", totalInterest);
		ss.setAttribute("principal", totalPrincipal);
		ss.setAttribute("entries", totalEntries);
		ss.setAttribute("amount", totalInterest+totalPrincipal);
		ss.setAttribute("list", list);
		return "printLoanPaymentReport";
	}
	public ArrayList<LoanInfo> getList() {
		return list;
	}
	public void setList(ArrayList<LoanInfo> list) {
		this.list = list;
	}
	public Date getStartdate() {
		return startdate;
	}
	public void setStartdate(Date startdate) {
		this.startdate = startdate;
	}
	
	public double getTotAssets() {
		return totAssets;
	}
	public void setTotAssets(double totAssets) {
		this.totAssets = totAssets;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public Date getEnddate() {
		return enddate;
	}
	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}
	public int getTotalEntries() {
		return totalEntries;
	}
	public void setTotalEntries(int totalEntries) {
		this.totalEntries = totalEntries;
	}
	public double getTotalInterest() {
		return totalInterest;
	}
	public void setTotalInterest(double totalInterest) {
		this.totalInterest = totalInterest;
	}
	public double getTotalPrincipal() {
		return totalPrincipal;
	}
	public void setTotalPrincipal(double totalPrincipal) {
		this.totalPrincipal = totalPrincipal;
	}
	public boolean isShow() {
		return show;
	}
	public void setShow(boolean show) {
		this.show = show;
	}
	
	
}
