package other_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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 main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.DebitInfo;
import main_package.InstallmentInfo;
import main_package.LoanInfo;
import main_package.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="personalDetailDashboard")
@ViewScoped
public class PersonalDetailDashboardBean implements Serializable
{
	ArrayList<LoanInfo> allLoanList;
	LoanInfo selectedLoan;
	boolean showPenaltyField;
	String id,name,contact;
	ArrayList<InstallmentInfo> plist, rdPayList;
	double totalPaid, totalDue;
	ArrayList<ReportInfo> dailyPayList;
	DataBaseMethods obj=new DataBaseMethods();
	String shareId;
	ArrayList<UserInfo> list, savingList;
	UserInfo selectedUser, info;
	ArrayList<DebitInfo> transactionHistory;
	
	public PersonalDetailDashboardBean()
	{    
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		shareId=(String) ss.getAttribute("shareId");
		System.out.println("value of shareID :"+shareId);
		allLoanList = new DataBaseMethodsOther().allLoanList(shareId);
		
		Connection conn=DataBaseConnection.JAVAConnection();
		info=obj.userInfoByShareHolderNo(shareId, conn);
		
		list=new DataBaseMethodsOther().allRd_FDList(shareId);
		savingList=new DataBaseMethodsOther().allSavingAccountBean(shareId);
		
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void viewLoanDisburseDetail()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		if(selectedLoan.getTableName().equalsIgnoreCase("grouploan"))
		{
			transactionHistory=obj.viewDebitLoanHistory(selectedLoan.getTableName(),selectedLoan.getSubLoanNo(),selectedLoan.getShareHolderNo(),conn);
		}
		else
		{
			transactionHistory=obj.viewDebitLoanHistory(selectedLoan.getTableName(),selectedLoan.getLoanNo(),selectedLoan.getShareHolderNo(),conn);
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// This Method Show Complete Detail Of Loan. 
	public void viewLoanDetails() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedLoan",selectedLoan);
		ss.setAttribute("pageName","particularLoan.xhtml");
		if(selectedLoan.getLoanType().equals("Personal Loan"))
		{
			RequestContext.getCurrentInstance().execute("window.open('viewPersonalLoan.xhtml')");
		}
		else if(selectedLoan.getLoanType().equals("Vehicle Loan"))
		{
			RequestContext.getCurrentInstance().execute("window.open('viewVehicleLoan.xhtml')");
		}
		else if(selectedLoan.getLoanType().equals("Property Loan"))
		{
			RequestContext.getCurrentInstance().execute("window.open('viewPropertyLoan.xhtml')");
		}
		else if(selectedLoan.getLoanType().equals("Group Loan"))
		{
			RequestContext.getCurrentInstance().execute("window.open('viewGroupLoan.xhtml')");
		}
		else if(selectedLoan.getLoanType().equals("Secure Loan"))
		{
			RequestContext.getCurrentInstance().execute("window.open('viewSecureLoan.xhtml')");
		}
	}
	
	public void searchLoanReport() 
	{
		if(!selectedLoan.getStatus().equalsIgnoreCase("Approve"))
		{
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Please Approve Loan First"));
		}
		else
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("selectedLoan",selectedLoan);
			ss.setAttribute("showPen",showPenaltyField);
			ss.setAttribute("pageName","particularLoan.xhtml");
			
			RequestContext.getCurrentInstance().execute("window.open('loanReportParticular.xhtml')");
		}
	}
	
	public void printDetailsNoc()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selected", selectedLoan);
		ss.setAttribute("loanType", selectedLoan.getTableName());
		RequestContext.getCurrentInstance().execute("window.open('printNoc.xhtml')");
		RequestContext.getCurrentInstance().execute("window.open('printNocFormat.xhtml')");
	}	
	

	// Method for Loan EMI Payment. This Method Firstly check Loan is Approved than after it return on Pay Loan EMI Page.
	public void paymentLoan() 
	{
		if(!selectedLoan.getStatus().equalsIgnoreCase("Approve"))
		{
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Please Approve Loan First"));
		}
		else
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("selectedLoan",selectedLoan);
			ss.setAttribute("pageName","particularLoan");
			
			RequestContext.getCurrentInstance().execute("window.open('payLoanEMI.xhtml')");
		}
	}
	
	public String printCloserReport()
    {
        HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ss.setAttribute("selectedUser",selectedUser);
        
        RequestContext.getCurrentInstance().execute("window.open('printRdFdClosureReport.xhtml')");
        
        return "";
    }
	
	// this Method Shows the Complete Detail Of Deposit it May be DD, RD, FD etc.
	public void viewDepositDetails() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularRD_FD.xhtml");
		
		RequestContext.getCurrentInstance().execute("window.open('viewUserInfo.xhtml')");
	}

	public void paymentDeposit() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularRD_FD");
		
		RequestContext.getCurrentInstance().execute("window.open('paymentReceipt.xhtml')");
	}
	
	// This Method Shows Detail of Particular DD/RD/FD. 
	public void searchDepositReport() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularRD_FD.xhtml");
		
		RequestContext.getCurrentInstance().execute("window.open('rd_fd_ReportParticular.xhtml')");
	}
	
	public void printDailyPay()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		if(selectedUser.getScheme_type().equalsIgnoreCase("FD"))
		{
			plist=new ArrayList<>();
			InstallmentInfo ii = new InstallmentInfo();
			double paidAmount = obj.paidAmountByUser(selectedUser.getUser_no(),conn);
			
			ii.setInstallment_amount(selectedUser.getInstallment_amount());
			ii.setPaidAmount(paidAmount);
			ii.setUser_no(selectedUser.getUser_no());
			ii.setSno(1);
			ii.setIns_date(new SimpleDateFormat("dd/MM/yyyy").format(selectedUser.getOffice_date()));
			ii.setInstlmntDate(selectedUser.getOffice_date());
		
			if(paidAmount==selectedUser.getInstallment_amount())
			{
				ii.setStatus("Paid");
			}
			else
			{
				ii.setStatus("Due");
			}
			ii.setTerms(selectedUser.getTerms());
			ii.setCalc_interest(selectedUser.getMaturity_value() - selectedUser.getInstallment_amount());
			ii.setTot_sum(Double.parseDouble(new DecimalFormat("#.##").format(selectedUser.getMaturity_value())));
			
			plist.add(ii);
		}
		else
		{
			plist=obj.showInstallmentForDepositDetail(selectedUser.getUser_no(), conn);
		}
		totalDue=totalPaid=0;
		System.out.println("dksldkskdlskdlskd"+plist.size());
		for(InstallmentInfo ff: plist)
		{
		
			if(ff.getStatus().equalsIgnoreCase("paid"))
			{
				totalPaid+=ff.getInstallment_amount();
			}
			else
			{
				totalDue+=ff.getInstallment_amount();
			}
		}
		
		totalDue=Double.parseDouble(new DecimalFormat("##.##").format(0));
		totalPaid=Double.parseDouble(new DecimalFormat("##.##").format(totalPaid));
		
		dailyPayList= new ArrayList<>();
		// System.out.println(selectedUser.getUser_no());
		// System.out.println(selectedUser.getScheme_type());
		dailyPayList= obj.dailyRDFDReport(selectedUser.getUser_no(),selectedUser.getScheme_type(),conn);
		try 
		{
			conn.close();
		} 
		catch (Exception e) 
		{
			// TODO: handle exception
		}
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("RDFDailyPayList", dailyPayList);
		ss.setAttribute("totalSchDue", totalDue);
		ss.setAttribute("totalSchPaid", totalPaid);
		ss.setAttribute("selectedUserInfo", selectedUser);
		
		RequestContext.getCurrentInstance().execute("window.open('printPerticularDateSchemePayment.xhtml')");
		
	}
	
	
	// This Method Shows the Detail of Selected Saving Account No.
	public void viewSavingDetails() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount.xhtml");
		
		RequestContext.getCurrentInstance().execute("window.open('viewSavingAccount.xhtml')");
	}
	
	// This Method Return on Deposit Page For Deposit Amount in Selected Saving Account No.
	public void depositSavingAmount() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount");
		
		RequestContext.getCurrentInstance().execute("window.open('userDeposit.xhtml')");
	}
	
	// This Method Return on Withdraw Page For Withdraw Amount in Selected Saving Account No.
	public void withdrawalSavingAmount() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUserWithdraw", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount");
		
		RequestContext.getCurrentInstance().execute("window.open('userWithdrawl.xhtml')");
	}
	
	// This Method Return on Mini-Statement Page where We Can Check Saving Account Statement.
	public void miniStatement() 
	{
		Date startDate=new Date();
		Date endDate=new Date();
		startDate.setDate(1);
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularSaveAccount");
		ss.setAttribute("svStartDate", startDate);
		ss.setAttribute("svEndDate", endDate);
		
		RequestContext.getCurrentInstance().execute("window.open('miniStatement.xhtml')");
	}
	
	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}
	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}
	public ArrayList<LoanInfo> getAllLoanList() {
		return allLoanList;
	}
	public void setAllLoanList(ArrayList<LoanInfo> allLoanList) {
		this.allLoanList = allLoanList;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContact() {
		return contact;
	}
	public void setContact(String contact) {
		this.contact = contact;
	}


	public String getShareId() {
		return shareId;
	}


	public void setShareId(String shareId) {
		this.shareId = shareId;
	}


	public boolean isShowPenaltyField() {
		return showPenaltyField;
	}


	public void setShowPenaltyField(boolean showPenaltyField) {
		this.showPenaltyField = showPenaltyField;
	}


	public ArrayList<InstallmentInfo> getPlist() {
		return plist;
	}


	public void setPlist(ArrayList<InstallmentInfo> plist) {
		this.plist = plist;
	}


	public ArrayList<InstallmentInfo> getRdPayList() {
		return rdPayList;
	}


	public void setRdPayList(ArrayList<InstallmentInfo> rdPayList) {
		this.rdPayList = rdPayList;
	}


	public double getTotalPaid() {
		return totalPaid;
	}


	public void setTotalPaid(double totalPaid) {
		this.totalPaid = totalPaid;
	}


	public double getTotalDue() {
		return totalDue;
	}


	public void setTotalDue(double totalDue) {
		this.totalDue = totalDue;
	}


	public ArrayList<UserInfo> getList() {
		return list;
	}


	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}


	public ArrayList<UserInfo> getSavingList() {
		return savingList;
	}


	public void setSavingList(ArrayList<UserInfo> savingList) {
		this.savingList = savingList;
	}


	public UserInfo getSelectedUser() {
		return selectedUser;
	}


	public void setSelectedUser(UserInfo selectedUser) {
		this.selectedUser = selectedUser;
	}


	public UserInfo getInfo() {
		return info;
	}


	public void setInfo(UserInfo info) {
		this.info = info;
	}


	public ArrayList<DebitInfo> getTransactionHistory() {
		return transactionHistory;
	}


	public void setTransactionHistory(ArrayList<DebitInfo> transactionHistory) {
		this.transactionHistory = transactionHistory;
	}
	
}
