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.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.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="savingAccountIncomeExpenseReport")
@ViewScoped

public class SavingAccountIncomeExpenseReportBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	Date startDate=new Date(),endDate=new Date();
	String reportType, accountNo;
	boolean show,showAmount,showOtherAmount,showIntr;
	ArrayList<UserInfo> finalList,expenseList,incomeList,interestList,liabilityList;
	double totalAmount,totalIncome,totalExpense,totalCr,totalDr,totalintr;
	int totalEntry;
	ArrayList<SelectItem> savingAccList;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	
	
	
	public SavingAccountIncomeExpenseReportBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		startDate.setMonth(startDate.getMonth()-1);
		savingAccList=obj1.savingActiveAccountNoList(branchId);
	}
	
	public void printReport()
	{
		if(totalEntry>0)
		{
			String st="Saving's Account";
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("finalList", finalList);
			ss.setAttribute("reportType",reportType);
			ss.setAttribute("cr", totalCr);
			ss.setAttribute("dr", totalDr);
			ss.setAttribute("intr", totalintr);
			RequestContext.getCurrentInstance().execute("window.open('printSavingsIncomeExpenseReport.xhtml')");
		}
	}
	
	public void showReport()
	{
		
		showOtherAmount=false;showAmount=true;
		if((startDate==null && endDate==null) || ((startDate!=null && endDate!=null) && (endDate.after(startDate))))
		{
			show=true;totalExpense=totalIncome=0;
			if(reportType.equals("Liabilities"))
			{
				finalList=liabilities(startDate,endDate);
				showIntr=false;
			}
			else if(reportType.equals("Expense"))
			{
				showIntr=true;
				finalList=expense(startDate,endDate);
			}
			if(reportType.equals("Income_Expense"))
			{
				showOtherAmount=true;showAmount=false;
				finalList=liabilities(startDate,endDate);
				finalList.addAll(expense(startDate,endDate));
				int i=1;
				for(UserInfo ll:finalList)
				{
					if(ll.getReportType().equals("Income"))
					{
						totalIncome+=ll.getAmount();
					}
					else if(ll.getReportType().equals("Expense"))
					{
						ll.setTotalAmount(ll.getAmount());
						ll.setAmount(0);
						totalExpense+=ll.getTotalAmount();
					}
					ll.setSno(i++);
					ll.setShowReportType(true);
				}
			}
			totalEntry=finalList.size();
			Collections.sort(finalList);
		
			/*if(reportType.equals("Liabilities"))
			{
				// System.out.println("list size : "+finalList.size());
				ArrayList<UserInfo> tempList=new ArrayList<>();
				for(int k=0; k<finalList.size(); k++)
				{
					//// System.out.println(k+" hiiiii "+finalList.get(k).getAmount()+"========="+finalList.get(k).getDrAmount());
					
					if(finalList.get(k).getAmount()>0 || finalList.get(k).getDrAmount()>0)
					{
						// System.out.println("ifff");
						tempList.add(finalList.get(k));
					}
				}
				// System.out.println("temp size: "+tempList.size());
				finalList=new ArrayList<>();
				finalList=tempList;
				tempList=new ArrayList<>();
			}*/
			
			for(int i=0; i<finalList.size(); i++)
			{
				finalList.get(i).setSno(i+1);
			}
			totalCr=Double.parseDouble(new DecimalFormat("##.##").format(totalCr));
			totalDr=Double.parseDouble(new DecimalFormat("##.##").format(totalDr));
			totalintr=Double.parseDouble(new DecimalFormat("##.##").format(totalintr));
		}
		else
		{
			show=false;
			FacesContext fc=FacesContext.getCurrentInstance();
			fc.addMessage(null, new FacesMessage("Please enter the dates properly!"));
		}
		
		
	}
	
	private ArrayList<UserInfo> expense(Date strtdt, Date enddt)
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		//expenseList=new ArrayList<>();
		interestList=new ArrayList<>();
		interestList=obj1.interestOnSavingsList(branchId,strtdt,enddt,conn, accountNo);
		//expenseList=obj1.withdrawlListForExpenseReport(strtdt,enddt);
		int i=1;
		totalDr=totalCr=totalintr=0;
		for(UserInfo ll:interestList)
		{
			ll.setSno(i++);
			totalintr=totalintr+(ll.getIntrAmount());
			ll.setReportType("Expense");
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return interestList;
	}
	private ArrayList<UserInfo> liabilities(Date strtdt, Date enddt) 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		incomeList=new ArrayList<>();
		expenseList=new ArrayList<>();
		interestList=new ArrayList<>();
		liabilityList=new ArrayList<>();
		incomeList=obj1.depositPaymentListForIncomeReport(branchId,strtdt,enddt,conn, accountNo);
		expenseList=obj1.withdrawlListForExpenseReport(branchId,strtdt,enddt,conn, accountNo);
		interestList=obj1.interestOnSavingsList(branchId,strtdt,enddt,conn, accountNo);
		int i=1;
		totalDr=totalCr=totalintr=0;
		for(UserInfo ll:incomeList)
		{
			ll.setSno(i++);
			totalCr=totalCr+(ll.getAmount());
			ll.setReportType("Liabilities");
		}
		
		for(UserInfo ll:expenseList)
		{
			ll.setSno(i++);
			totalDr=totalDr+(ll.getDrAmount());
			ll.setReportType("Liabilities");
		}
		
		for(UserInfo ll:interestList)
		{
			ll.setSno(i++);
			totalintr=totalintr+(ll.getIntrAmount());
			ll.setReportType("Liabilities");
		}
		
		liabilityList.addAll(incomeList);
		liabilityList.addAll(expenseList);
		liabilityList.addAll(interestList);
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return liabilityList;
	}
	
	public Date getStartDate() {
		return startDate;
	}
	
	public ArrayList<UserInfo> getInterestList() {
		return interestList;
	}

	public void setInterestList(ArrayList<UserInfo> interestList) {
		this.interestList = interestList;
	}

	public ArrayList<UserInfo> getLiabilityList() {
		return liabilityList;
	}

	public void setLiabilityList(ArrayList<UserInfo> liabilityList) {
		this.liabilityList = liabilityList;
	}

	public double getTotalCr() {
		return totalCr;
	}

	public void setTotalCr(double totalCr) {
		this.totalCr = totalCr;
	}

	public double getTotalDr() {
		return totalDr;
	}

	public void setTotalDr(double totalDr) {
		this.totalDr = totalDr;
	}

	public double getTotalintr() {
		return totalintr;
	}

	public void setTotalintr(double totalintr) {
		this.totalintr = totalintr;
	}

	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 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> getExpenseList() {
		return expenseList;
	}
	public void setExpenseList(ArrayList<UserInfo> expenseList) {
		this.expenseList = expenseList;
	}
	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 int getTotalEntry() {
		return totalEntry;
	}
	public void setTotalEntry(int totalEntry) {
		this.totalEntry = totalEntry;
	}
	public ArrayList<UserInfo> getIncomeList() {
		return incomeList;
	}
	public void setIncomeList(ArrayList<UserInfo> incomeList) {
		this.incomeList = incomeList;
	}

	public boolean isShowIntr() {
		return showIntr;
	}

	public void setShowIntr(boolean showIntr) {
		this.showIntr = showIntr;
	}

	public String getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	public ArrayList<SelectItem> getSavingAccList() {
		return savingAccList;
	}

	public void setSavingAccList(ArrayList<SelectItem> savingAccList) {
		this.savingAccList = savingAccList;
	}
	
}
