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 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.InstallmentInfo;
import main_package.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="parRD_FD")
@ViewScoped
public class ParticularRd_FDBean implements Serializable 
{
	private static final long serialVersionUID = 1L;
	String id,name,contact;
	ArrayList<UserInfo> list;
	ArrayList<InstallmentInfo> plist, rdPayList;
	double totalPaid, totalDue;
	UserInfo selectedUser;
	ArrayList<ReportInfo> dailyPayList;
	DataBaseMethods obj=new DataBaseMethods();
	public ParticularRd_FDBean()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		id=(String) ss.getAttribute("shareId");
		list=new DataBaseMethodsOther().allRd_FDList(id);

		Connection conn=DataBaseConnection.JAVAConnection();
		UserInfo info=new DataBaseMethods().userInfoByShareHolderNo(id, conn);
		name=info.getFirst_applicantName();
		contact=info.getContactNo();
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// this Method Shows the Complete Detail Of Deposit it May be DD, RD, FD etc.
	public void viewDetails() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularRD_FD.xhtml");
		FacesContext.getCurrentInstance().getExternalContext().redirect("viewUserInfo.xhtml"); ;
	}

	public void payment() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularRD_FD.xhtml");
		FacesContext.getCurrentInstance().getExternalContext().redirect("paymentReceipt.xhtml");
	}
	
	// This Method Shows Detail of Particular DD/RD/FD. 
	public void searchReport() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("selectedUser", selectedUser);
		ss.setAttribute("pageName","particularRD_FD.xhtml");
		FacesContext.getCurrentInstance().getExternalContext().redirect( "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')");
		
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public ArrayList<UserInfo> getList() {
		return list;
	}
	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}
	public UserInfo getSelectedUser() {
		return selectedUser;
	}
	public void setSelectedUser(UserInfo selectedUser) {
		this.selectedUser = selectedUser;
	}
	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 ArrayList<ReportInfo> getDailyPayList() {
		return dailyPayList;
	}

	public void setDailyPayList(ArrayList<ReportInfo> dailyPayList) {
		this.dailyPayList = dailyPayList;
	}
}
