package main_package;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

@ManagedBean(name="loanCalculator")
@ViewScoped
public class LoanCalculatorBean implements Serializable
{
	double loanAmount,interstRate,loanAmount2,interstRate2,duration, emiPayment, totalIntrest, monthlyIntrest;
	int frequency=12;
	double installment_amount,interest,maturity_value, totalDtAmount, totalDtPrincipal, totalDtInterest, discount;
	int terms;
	String label="Terms", calType="RD",loanType, calMode="emi";
	boolean showFd, showRd=true,tblRndr, disEmi, disIntr, showReducingBtn;
	ArrayList<LoanInfo> emiList;
	DataBaseMethods obj=new DataBaseMethods();
	DecimalFormat df=new DecimalFormat("##.##");
	
	public LoanCalculatorBean()
	{
		//System.out.println("Time 10 :"+new Timestamp(new Date().getTime()));
		checkCalMode();
	}
	
	public void calculateEMI()
	{
		totalIntrest=discount=0;
		loanType="flat";
		
		if(calMode.equals("emi"))
		{
			emiPayment=obj.calculateLoanEmiForCalculator(loanAmount,duration, interstRate, frequency, "both");  	
		}
		else
		{
			interstRate=obj.calculateLoanInterestForCalculator(loanAmount,duration, emiPayment, frequency);  	
		}
		
		if(loanAmount==0)
		{
			emiPayment=0;
		}
		
		if(emiPayment>0)
		{
			double totalPayment=emiPayment*duration;
			double falseInterest=totalPayment-loanAmount;
			double trueInterest=(loanAmount*interstRate*duration)/(100*frequency);
			trueInterest=Math.round(trueInterest);
			discount=(int) (falseInterest-trueInterest);
			discount=Math.round(discount);	
		}
		
		totalIntrest=Double.parseDouble(df.format((emiPayment*duration)-loanAmount));
		createEmiList();
		totalDtAmount=totalDtPrincipal=totalDtInterest=0;
		for(LoanInfo ff: emiList)
		{
			totalDtInterest+=ff.getInterest();
			totalDtPrincipal+=ff.getPrincipal();
			totalDtAmount+=ff.getPaidAmount();
		}
		System.out.println(totalDtPrincipal+"=="+discount);
		totalDtAmount=Math.round(totalDtAmount);
		totalDtPrincipal=Math.round(totalDtPrincipal);
		totalDtInterest=Math.round(totalDtInterest);
	}
	
	public void checkCalMode()
	{
		if(calMode.equals("emi"))
		{
			disEmi=true;
			disIntr=false;
			showReducingBtn=true;
			emiPayment=0;
		}
		else
		{
			disEmi=false;
			disIntr=true;
			showReducingBtn=false;
			interstRate=0;
		}
	}
	
	public void calculateEMIReducing()
	{
		totalIntrest=0;
		loanType="reducing";
		emiPayment=obj.calculateLoanEmi(loanAmount,duration, interstRate, frequency);
		//emiPayment=obj.calculateLoanEmiForCalculator(loanAmount,duration, interstRate, frequency);  	
		if(loanAmount==0)
		{
			emiPayment=0;
		}
		
		if(emiPayment>0)
		{
				double totalPayment=emiPayment*duration;
				double falseInterest=totalPayment-loanAmount;
				double trueInterest=new DataBaseMethods().totalInterestForDiscount(loanAmount,duration,interstRate,frequency,emiPayment);
				trueInterest=Math.round(trueInterest);
				discount=(int) (falseInterest-trueInterest);
				discount=Math.round(discount);
		}
		
		totalIntrest=new DataBaseMethods().totalInterestForDiscount(loanAmount,duration,interstRate,frequency,emiPayment);
		totalIntrest=Math.round(totalIntrest);
		createEmiList();
		totalDtAmount=totalDtPrincipal=totalDtInterest=0;
		for(LoanInfo ff: emiList)
		{
			totalDtInterest+=ff.getInterest();
			totalDtPrincipal+=ff.getPrincipal();
			totalDtAmount+=ff.getPaidAmount();
		}
		totalDtPrincipal-=discount;	
		totalDtAmount=Math.round(totalDtAmount);
		totalDtPrincipal=Math.round(totalDtPrincipal);
		totalDtInterest=Math.round(totalDtInterest);
	}
	
	public void calculateMonthlyIntrest()
	{
		monthlyIntrest=(loanAmount2*interstRate2)/1200;
		monthlyIntrest=Double.parseDouble(df.format(monthlyIntrest));
	}
	
	

	public void printWhole()
	{
		System.out.println("welcome");
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("list", emiList);
		ss.setAttribute("loanAmount", loanAmount);
		ss.setAttribute("duration", duration);
		ss.setAttribute("interstRate", interstRate);
		ss.setAttribute("frequency", frequency);
		ss.setAttribute("emi", emiPayment);
		ss.setAttribute("totalInterest", totalIntrest);
		ss.setAttribute("discount", discount);
		ss.setAttribute("totalDtAmount", totalDtAmount);
		ss.setAttribute("totalDtInterest", totalDtInterest);
		ss.setAttribute("totalDtPrincipal", totalDtPrincipal);
		RequestContext.getCurrentInstance().execute("window.open('printLoanCalculator.xhtml')");
	}
	
	public void checkType()
	{
		if(calType.equals("FD"))
		{
			frequency=12;	
			label="Tenure (In Month)";
			showRd=false;
		}
		else
		{
			label="Terms";
			showRd=true;
		}
	}
	
	public void calculateInterest()
	{
		tblRndr=false;
		maturity_value=obj.findInterest(terms,installment_amount,interstRate,calType,frequency);
		//// System.out.println("mmmmmmmmmmm: "+maturity_value);
		if(calType.equals("RD") || calType.equals("DD"))
		{
			totalIntrest=maturity_value-(terms*installment_amount);
		}
		else
		{
			totalIntrest=maturity_value-installment_amount;
		}
		maturity_value=Double.parseDouble(df.format(maturity_value));
		totalIntrest=Double.parseDouble(df.format(totalIntrest));
	}
	
	
	
	
	
public void createEmiList()
{
	tblRndr=true;
	emiList= new ArrayList<>();
	int count=0;
	if(loanType.equals("flat"))
	{
		double pAmt=0, iAmt=0;
		for(int i=0;i<duration;i++)
		{
		     LoanInfo obj= new LoanInfo();
		     obj.setEmiNo(String.valueOf((++count)));
		     
		     if(count==duration)
		     {
		    	 obj.setPrincipal(Double.parseDouble(df.format(loanAmount-pAmt)));
				 obj.setInterest(Double.parseDouble(df.format((emiPayment*duration)-(loanAmount+iAmt))));
				 obj.setInterest(obj.getInterest()-discount);
				 obj.setPaidAmount(Double.parseDouble(df.format(emiPayment-discount)));
		     }
		     else
		     {
		    	 obj.setPrincipal(Double.parseDouble(df.format(loanAmount/duration)));
				 obj.setInterest(Double.parseDouble(df.format(totalIntrest/duration)));
				 obj.setPaidAmount(Double.parseDouble(df.format(emiPayment)));
		     }
		     pAmt+=obj.getPrincipal();
		     iAmt+=obj.getInterest();
		   
		     emiList.add(obj);
		   
		}		
	}
	else
	{
		double loanAmt=loanAmount;
		double fr=(double) frequency;
		//double interestMonthly =Double.parseDouble(new DecimalFormat("##.########").format(interstRate / (100.00 * frequency)));
		for(int i=0;i<duration;i++)
		{
			
		     LoanInfo obj= new LoanInfo();
		     obj.setEmiNo(String.valueOf((++count)));
		     System.out.println(loanAmt+"=="+interstRate+"=="+fr);
		     double frstIntrstAmnt=(loanAmt*interstRate)/(100.00 * fr);
		     double paidPrincipalAmnt=emiPayment-(frstIntrstAmnt);
		     obj.setPaidAmount(Double.parseDouble(df.format(emiPayment)));
		     obj.setPrincipal(Double.parseDouble(df.format(paidPrincipalAmnt)));
		    obj.setInterest(Double.parseDouble(df.format(frstIntrstAmnt)));
		    loanAmt=loanAmt-paidPrincipalAmnt;
		     emiList.add(obj);
		   
		}		
		
	}

}

	public double getLoanAmount2() {
		return loanAmount2;
	}

	public void setLoanAmount2(double loanAmount2) {
		this.loanAmount2 = loanAmount2;
	}

	public double getInterstRate2() {
		return interstRate2;
	}

	public void setInterstRate2(double interstRate2) {
		this.interstRate2 = interstRate2;
	}

	public double getLoanAmount() {
		return loanAmount;
	}

	public void setLoanAmount(double loanAmount) {
		this.loanAmount = loanAmount;
	}

	public double getInterstRate() {
		return interstRate;
	}

	public void setInterstRate(double interstRate) {
		this.interstRate = interstRate;
	}

	public double getDuration() {
		return duration;
	}

	public void setDuration(double duration) {
		this.duration = duration;
	}

	public double getEmiPayment() {
		return emiPayment;
	}

	public void setEmiPayment(double emiPayment) {
		this.emiPayment = emiPayment;
	}

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int frequency) {
		this.frequency = frequency;
	}

	public double getTotalIntrest() {
		return totalIntrest;
	}

	public void setTotalIntrest(double totalIntrest) {
		this.totalIntrest = totalIntrest;
	}

	public double getMonthlyIntrest() {
		return monthlyIntrest;
	}

	public void setMonthlyIntrest(double monthlyIntrest) {
		this.monthlyIntrest = monthlyIntrest;
	}

	public double getInstallment_amount() {
		return installment_amount;
	}

	public void setInstallment_amount(double installment_amount) {
		this.installment_amount = installment_amount;
	}

	public double getInterest() {
		return interest;
	}

	public void setInterest(double interest) {
		this.interest = interest;
	}

	public double getMaturity_value() {
		return maturity_value;
	}

	public void setMaturity_value(double maturity_value) {
		this.maturity_value = maturity_value;
	}

	public int getTerms() {
		return terms;
	}

	public void setTerms(int terms) {
		this.terms = terms;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getCalType() {
		return calType;
	}

	public void setCalType(String calType) {
		this.calType = calType;
	}

	public boolean isShowFd() {
		return showFd;
	}

	public void setShowFd(boolean showFd) {
		this.showFd = showFd;
	}

	public boolean isShowRd() {
		return showRd;
	}

	public void setShowRd(boolean showRd) {
		this.showRd = showRd;
	}

	public ArrayList<LoanInfo> getEmiList() {
		return emiList;
	}

	public void setEmiList(ArrayList<LoanInfo> emiList) {
		this.emiList = emiList;
	}

	public boolean isTblRndr() {
		return tblRndr;
	}

	public void setTblRndr(boolean tblRndr) {
		this.tblRndr = tblRndr;
	}

	public double getTotalDtAmount() {
		return totalDtAmount;
	}

	public void setTotalDtAmount(double totalDtAmount) {
		this.totalDtAmount = totalDtAmount;
	}

	public double getTotalDtPrincipal() {
		return totalDtPrincipal;
	}

	public void setTotalDtPrincipal(double totalDtPrincipal) {
		this.totalDtPrincipal = totalDtPrincipal;
	}

	public double getTotalDtInterest() {
		return totalDtInterest;
	}

	public void setTotalDtInterest(double totalDtInterest) {
		this.totalDtInterest = totalDtInterest;
	}

	public String getLoanType() {
		return loanType;
	}

	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}

	public DataBaseMethods getObj() {
		return obj;
	}

	public void setObj(DataBaseMethods obj) {
		this.obj = obj;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public String getCalMode() {
		return calMode;
	}

	public void setCalMode(String calMode) {
		this.calMode = calMode;
	}

	public boolean isDisEmi() {
		return disEmi;
	}

	public void setDisEmi(boolean disEmi) {
		this.disEmi = disEmi;
	}

	public boolean isDisIntr() {
		return disIntr;
	}

	public void setDisIntr(boolean disIntr) {
		this.disIntr = disIntr;
	}

	public boolean isShowReducingBtn() {
		return showReducingBtn;
	}

	public void setShowReducingBtn(boolean showReducingBtn) {
		this.showReducingBtn = showReducingBtn;
	}

	


	
	
}
