package report_package;

import java.io.Serializable;
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 main_package.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="loanIncomeExpenseReport")
@ViewScoped
public class LoanIncomeExpenseReportBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	Date startDate,endDate;
	String reportType,loanType,type;
	boolean show,showAmount,showOtherAmount;
	ArrayList<UserInfo> finalList,loanExpenseList,loanIncomeList,loanAssetList;
	double totalAmount,totalIncome,totalExpense,penalty,totalAsset;
	int totalEntry;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	public void printReport()
	{
		if(totalEntry>0)
		{
			String st="loan";
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("finalList", finalList);
			ss.setAttribute("reportType",reportType);
			ss.setAttribute("loanType", loanType);
			ss.setAttribute("identity", st);
			RequestContext.getCurrentInstance().execute("window.open('printIncomeExpenseReport.xhtml')");
		}
	}
	
	public void showReport()
	{
		totalIncome=totalExpense=totalAmount=0; 
		if(loanType.equals("personalloan"))
		{
			type="Personal Loan : ";
		}
		else if(loanType.equals("vechicleloan"))
		{
			type="Vehicle Loan : ";
		}
		else if(loanType.equals("propertyloan"))
		{
			type="Property Loan : ";
		}
		else if(loanType.equals("grouploan"))
		{
			type="Group Loan : ";
		}
		
		if((startDate==null && endDate==null) || ((startDate!=null && endDate!=null) && (endDate.after(startDate))))
		{
			show=true;showAmount=true;showOtherAmount=false;
			if(reportType.equals("Income"))
			{
				finalList=income(startDate,endDate,loanType);
			}
			else if(reportType.equals("Current Asset"))
			{
				finalList=currentAsset(startDate,endDate,loanType);
			}
			
			if(reportType.equals("Income_Expense"))
			{
				finalList=new ArrayList<>();
				showOtherAmount=true;showAmount=false;
				finalList=income(startDate,endDate,loanType);
				finalList.addAll(currentAsset(startDate,endDate,loanType));
				totalEntry=finalList.size();
				int i=1;penalty=0;totalAmount=0;
				for(UserInfo ll:finalList)
				{
					if(ll.getReportType().equals("Income"))
					{
						totalIncome+=ll.getAmount();
					}
					else if(ll.getReportType().equals("Current Asset"))
					{
						ll.setTotalAmount(ll.getAmount());
						ll.setAmount(0);
						totalAsset+=ll.getTotalAmount();
					}
					ll.setSno(i++);
					ll.setShowReportType(true);
					penalty=penalty+(ll.getPenalty());
				}
			}
			totalEntry=finalList.size();
		}
		else
		{
			show=false;
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Please enter the dates properly!"));
		}
	}
		
	public ArrayList<UserInfo> currentAsset(Date strtdt, Date enddt, String loanType)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		loanExpenseList=new ArrayList<>();
		loanExpenseList=obj1.ExpenseReportByLoanType(branchId,strtdt,enddt,loanType);
		int i=1;totalAmount=penalty=0;
		for(UserInfo ll:loanExpenseList)
		{
			ll.setSno(i++);
			ll.setReportType("Current Asset");
			penalty=penalty+(ll.getPenalty());
			totalAmount=totalAmount+(ll.getAmount());
		}
		return loanExpenseList;
	}
	
	public ArrayList<UserInfo> income(Date strtdt, Date enddt, String loanType)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		loanIncomeList=new ArrayList<>();
		loanIncomeList=obj1.incomeReportByLoanType(branchId,strtdt,enddt,loanType);
		int i=1;totalAmount=penalty=0;
		for(UserInfo ll:loanIncomeList)
		{
			ll.setSno(i++);
			ll.setReportType("Income");
			totalAmount=totalAmount+(ll.getAmount());
			penalty=penalty+(ll.getPenalty());
		}
		return loanIncomeList;
	}
	
	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 String getReportType() {
		return reportType;
	}
	public void setReportType(String reportType) {
		this.reportType = reportType;
	}
	public String getLoanType() {
		return loanType;
	}
	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public boolean isShow() {
		return show;
	}
	public void setShow(boolean show) {
		this.show = show;
	}
	public ArrayList<UserInfo> getFinalList() {
		return finalList;
	}
	public void setFinalList(ArrayList<UserInfo> finalList) {
		this.finalList = finalList;
	}
	public ArrayList<UserInfo> getLoanExpenseList() {
		return loanExpenseList;
	}
	public void setLoanExpenseList(ArrayList<UserInfo> loanExpenseList) {
		this.loanExpenseList = loanExpenseList;
	}
	public ArrayList<UserInfo> getLoanIncomeList() {
		return loanIncomeList;
	}
	public void setLoanIncomeList(ArrayList<UserInfo> loanIncomeList) {
		this.loanIncomeList = loanIncomeList;
	}
	public boolean isShowAmount() {
		return showAmount;
	}
	public void setShowAmount(boolean showAmount) {
		this.showAmount = showAmount;
	}
	public boolean isShowOtherAmount() {
		return showOtherAmount;
	}
	public void setShowOtherAmount(boolean showOtherAmount) {
		this.showOtherAmount = showOtherAmount;
	}
	public double getTotalAmount() {
		return totalAmount;
	}
	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}
	public double getTotalIncome() {
		return totalIncome;
	}
	public void setTotalIncome(double totalIncome) {
		this.totalIncome = totalIncome;
	}
	public double getTotalExpense() {
		return totalExpense;
	}
	public void setTotalExpense(double totalExpense) {
		this.totalExpense = totalExpense;
	}
	public double getPenalty() {
		return penalty;
	}
	public void setPenalty(double penalty) {
		this.penalty = penalty;
	}
	public int getTotalEntry() {
		return totalEntry;
	}
	public void setTotalEntry(int totalEntry) {
		this.totalEntry = totalEntry;
	}

	public double getTotalAsset() {
		return totalAsset;
	}

	public void setTotalAsset(double totalAsset) {
		this.totalAsset = totalAsset;
	}
	
}
