package main_package;

import group_head_package.DataBaseIncomeExpense;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

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 org.primefaces.context.RequestContext;

import report_package.DataBaseReportMethods;

@ManagedBean(name="myDashboard")
@ViewScoped
public class MyDashboardBean implements Serializable
{
	double cashBalance=0, bankTotalAmount, ddBalance, rdBalance, fdBalance, misBalance, loanReceive,loanReceiveMonthly, depositReceive,depositReceiveMonthly, loanDisburse,loanDisburseMonthly, returnMaturity,returnMaturityMonthly, ltd, mtd, ytd;
	ArrayList<SelectItem> bankList;
	DataBaseMethods dbObj=new DataBaseMethods();
	DataBaseReportMethods dbmr=new DataBaseReportMethods();
	ArrayList<UserInfo> loanList, depositList, memberBdayList;
	UserInfo selectedRow;
	String[] selectedCenter;
	ArrayList<SelectItem> centerList=new ArrayList<>();
	Date startDate, endDate;
	String msgBalance, searchType, strYtd, strMtd, strLtd;
	int totalProblems, apiBal;
	DecimalFormat df=new DecimalFormat("##.##");
	DataBaseMethodsRaviK dbmrj=new  DataBaseMethodsRaviK();
	
	public MyDashboardBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		//System.out.println("Time 9 :"+new Timestamp(new Date().getTime()));
		Connection conn=DataBaseConnection.JAVAConnection();
		
		startDate=new Date();	          
		endDate=new Date();
		Date monthStartDate = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(monthStartDate);
		cal.set(Calendar.DAY_OF_MONTH,1);
		
		monthStartDate =cal.getTime();
		Date todayDate = new Date();
		cashBalance=bankTotalAmount=ddBalance=rdBalance=fdBalance=misBalance=0;
		cashBalance=dbObj.remainingCompanyCapitalAmount(branchId,conn);
		/*double absCashTransferCr = dbObj.calculateCashTransferCr(branchId,"all",conn);
		double absCashTransferDr = dbObj.calculateCashTransferDr(branchId,"all",conn);
		cashBalance=Double.parseDouble(df.format(cashBalance-absCashTransferDr+absCashTransferCr));*/
		// Both upper 2 lines comments on 28 nov 2021 due to adding these method in existing code and now don't need these method 
		cashBalance=Double.parseDouble(df.format(cashBalance));
		
		bankList=new DataBaseIncomeExpense().allBankList(conn);
		
		for(SelectItem bb: bankList)
		{
			bankTotalAmount+=dbObj.currentBankBalance(String.valueOf(bb.getValue()), conn);
		}
		
		bankTotalAmount=Double.parseDouble(df.format(bankTotalAmount));
		memberBdayList=dbmr.memberBirthDayListByDate(branchId,startDate, conn);
		msgBalance=dbObj.messageBalanceMethod(); // comment due to change in api @10-03-25
		
		if(msgBalance.trim().equals(""))
		{
			msgBalance="0";
		}
		/*System.out.println("start: "+new Date());
		ddBalance=Double.parseDouble(df.format(dbObj.schemeWiseCurrentBalance("DD", conn)));
		rdBalance=Double.parseDouble(df.format(dbObj.schemeWiseCurrentBalance("RD", conn)));
		fdBalance=Double.parseDouble(df.format(dbObj.schemeWiseCurrentBalance("FD", conn)));
		misBalance=Double.parseDouble(df.format(dbObj.schemeWiseCurrentBalance("MIS", conn)));
		System.out.println("end: "+new Date());*/
		totalProblems=dbObj.totalRegProblem(branchId,conn);
			
		loanReceive=dbmr.dailyEmiCollectionBetweenDate(branchId,startDate, endDate, "both", "both",conn, "");
		loanReceiveMonthly=dbmrj.monthlyEmiCollectionBetweenDate(branchId, monthStartDate, todayDate, "both", "both", conn);
		depositReceive=dbmr.dailyDepositCollectionBetweenDate(branchId,startDate, endDate, conn);
		depositReceiveMonthly=dbmrj.monthlyDepositCollectionBetweenDate(branchId, monthStartDate, todayDate, conn);
		loanDisburse=dbmr.disburseLoanBetweenDate(branchId,startDate, endDate, "all", "");
		loanDisburseMonthly=dbmrj.monthlyDisburseLoanBetweenDate(branchId, monthStartDate, todayDate, "all");
		System.out.println("loan dis amount is:"+loanDisburse);
		returnMaturity=dbmr.depositMaturityBetweenDate(branchId,startDate, endDate, "all", "");
		returnMaturityMonthly = dbmrj.monthlyDepositMaturityBetweenDate(branchId, monthStartDate, todayDate, "all");
		loanReceive=Double.parseDouble(df.format(loanReceive));
		depositReceive=Double.parseDouble(df.format(depositReceive));
		loanDisburse=Double.parseDouble(df.format(loanDisburse));
		System.out.println("loan dis amount"+loanDisburse);
		returnMaturity=Double.parseDouble(df.format(returnMaturity));
		loanReceiveMonthly=Double.parseDouble(df.format(loanDisburseMonthly));
		depositReceiveMonthly=Double.parseDouble(df.format(depositReceiveMonthly));
		loanDisburseMonthly=Double.parseDouble(df.format(loanDisburseMonthly));
		returnMaturityMonthly= Double.parseDouble(df.format(returnMaturityMonthly));
		
		DecimalFormat df = new DecimalFormat("#.###########");
		strMtd=df.format(dbmr.debitLoanAmountForDashboard(branchId, startDate, endDate, "mtd", conn));
		strYtd=df.format(dbmr.debitLoanAmountForDashboard(branchId, startDate, endDate, "ytd", conn));
		strLtd=df.format(dbmr.debitLoanAmountForDashboard(branchId, startDate, endDate, "ltd", conn));
		
		apiBal=new DataBaseMethods().checkApiBalance("Aadhaar Verification API (OTP)");
		 
		//System.out.println(strYtd+"=="+ytd);
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void refreshLoanList()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		loanList=new ArrayList<>();
		loanList=dbmr.allUpcomingEMI(branchId,startDate,endDate,"loan",conn,selectedCenter,centerList, false, false);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void refreshDepositList()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		depositList=new ArrayList<>();
		depositList=dbmr.allUpcomingEMI(branchId,startDate,endDate,"rd",conn,selectedCenter,centerList, false, false);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void editCash()
	{
		RequestContext.getCurrentInstance().execute("window.open('manageDashboardBalance.xhtml')");
	}
	
	public void editBank()
	{
		RequestContext.getCurrentInstance().execute("window.open('manageCompanyBank.xhtml')");
	}
	
	public void messageBalanceMethodByURL()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		
		String 	url=dbObj.checkSMessageApiDetailFromTable("balance_url", "",conn);
		//System.out.println(url);
		RequestContext.getCurrentInstance().execute("window.open('"+url+"')");
		
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendBirthDayMsg()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		String typeMsg="Happy birthday to you, Dear "+selectedRow.getShareHolderNo()+" "+selectedRow.getFirst_applicantName()+"! We wish you fulfillment and happiness on this special day! "
				+ "May you be blessed with sound health and enormous wealth in the coming years! Happy birthday!";
		new DataBaseMethods().runMessageUrl(branchId,selectedRow.getContactNo(), typeMsg, "System","1207161897468855526", conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void downloadApp() {
		try {
			String appLink = new DataBaseMethodsRavi().appLink();
			
			if (appLink != null && !appLink.trim().equals("")) {
				FacesContext.getCurrentInstance().getExternalContext().redirect(appLink);
			}
			
		}catch (Exception e) {
           e.printStackTrace();
		}
	}
	
	

	public void refreshCashBalance()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		
		Connection conn=DataBaseConnection.JAVAConnection();
		
		String sDate=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		double totalCashIn=dbObj.closingCashInBalance(branchId,sDate,conn, "tb");
		double totalCashOut=dbObj.closingCashOutBalance(branchId,sDate,conn, "tb");
		cashBalance=totalCashIn-totalCashOut;
		cashBalance=Double.parseDouble(df.format(cashBalance));
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendBirthDayMsgList()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId =(int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		
		for(UserInfo uu: memberBdayList)
		{
			String typeMsg="Happy birthday to you, Dear "+uu.getFirst_applicantName()+"! We wish you fulfillment and happiness on this special day! "
					+ "May you be blessed with sound health and enormous wealth in the coming years! Happy birthday!";
			new DataBaseMethods().runMessageUrl(branchId,uu.getContactNo(), typeMsg, "System","1207161897468855526", conn);
			typeMsg="";
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void traningVideo()
	{
		RequestContext.getCurrentInstance().execute("window.open('https://www.youtube.com/results?search_query=garg+web+infotech')");
	}
	
	public void demoSoft()
	{
		RequestContext.getCurrentInstance().execute("window.open('http://gargwebinfotech.in/')");
	}
	
	public void demoApp()
	{
		RequestContext.getCurrentInstance().execute("window.open('https://drive.google.com/file/d/1qZtvp-CY5Rcf0mQHPq5D7ArOWPCjxy9M/view?usp=drive_link')");
	}
	
	public void companyWebsite()
	{
		RequestContext.getCurrentInstance().execute("window.open('https://gargwebinfotech.com/')");
	}
	
	public void gwitBank()
	{
		RequestContext.getCurrentInstance().execute("window.open('gwitBankDetail.xhtml')");
	}
	
	public void companyProgram()
	{
		RequestContext.getCurrentInstance().execute("window.open('https://gargwebinfotech.com/gallery.html')");
	}
	
	
	public double getCashBalance() {
		return cashBalance;
	}


	public void setCashBalance(double cashBalance) {
		this.cashBalance = cashBalance;
	}


	public double getBankTotalAmount() {
		return bankTotalAmount;
	}


	public void setBankTotalAmount(double bankTotalAmount) {
		this.bankTotalAmount = bankTotalAmount;
	}


	public ArrayList<SelectItem> getBankList() {
		return bankList;
	}


	public void setBankList(ArrayList<SelectItem> bankList) {
		this.bankList = bankList;
	}


	public ArrayList<UserInfo> getLoanList() {
		return loanList;
	}


	public void setLoanList(ArrayList<UserInfo> loanList) {
		this.loanList = loanList;
	}


	public String[] getSelectedCenter() {
		return selectedCenter;
	}


	public void setSelectedCenter(String[] selectedCenter) {
		this.selectedCenter = selectedCenter;
	}


	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}


	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}


	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 ArrayList<UserInfo> getDepositList() {
		return depositList;
	}


	public void setDepositList(ArrayList<UserInfo> depositList) {
		this.depositList = depositList;
	}


	public ArrayList<UserInfo> getMemberBdayList() {
		return memberBdayList;
	}


	public void setMemberBdayList(ArrayList<UserInfo> memberBdayList) {
		this.memberBdayList = memberBdayList;
	}


	public UserInfo getSelectedRow() {
		return selectedRow;
	}


	public void setSelectedRow(UserInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

	public String getMsgBalance() {
		return msgBalance;
	}

	public void setMsgBalance(String msgBalance) {
		this.msgBalance = msgBalance;
	}

	public int getTotalProblems() {
		return totalProblems;
	}

	public void setTotalProblems(int totalProblems) {
		this.totalProblems = totalProblems;
	}

	public double getDdBalance() {
		return ddBalance;
	}

	public void setDdBalance(double ddBalance) {
		this.ddBalance = ddBalance;
	}

	public double getRdBalance() {
		return rdBalance;
	}

	public void setRdBalance(double rdBalance) {
		this.rdBalance = rdBalance;
	}

	public double getFdBalance() {
		return fdBalance;
	}

	public void setFdBalance(double fdBalance) {
		this.fdBalance = fdBalance;
	}

	public double getMisBalance() {
		return misBalance;
	}

	public void setMisBalance(double misBalance) {
		this.misBalance = misBalance;
	}

	public double getLoanReceive() {
		return loanReceive;
	}

	public void setLoanReceive(double loanReceive) {
		this.loanReceive = loanReceive;
	}

	public double getDepositReceive() {
		return depositReceive;
	}

	public void setDepositReceive(double depositReceive) {
		this.depositReceive = depositReceive;
	}

	public double getLoanDisburse() {
		return loanDisburse;
	}

	public void setLoanDisburse(double loanDisburse) {
		this.loanDisburse = loanDisburse;
	}

	public double getReturnMaturity() {
		return returnMaturity;
	}

	public void setReturnMaturity(double returnMaturity) {
		this.returnMaturity = returnMaturity;
	}

	public double getLtd() {
		return ltd;
	}

	public void setLtd(double ltd) {
		this.ltd = ltd;
	}

	public double getMtd() {
		return mtd;
	}

	public void setMtd(double mtd) {
		this.mtd = mtd;
	}

	public double getYtd() {
		return ytd;
	}

	public void setYtd(double ytd) {
		this.ytd = ytd;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getStrYtd() {
		return strYtd;
	}

	public void setStrYtd(String strYtd) {
		this.strYtd = strYtd;
	}

	public String getStrMtd() {
		return strMtd;
	}

	public void setStrMtd(String strMtd) {
		this.strMtd = strMtd;
	}

	public String getStrLtd() {
		return strLtd;
	}

	public void setStrLtd(String strLtd) {
		this.strLtd = strLtd;
	}

	public int getApiBal() {
		return apiBal;
	}

	public void setApiBal(int apiBal) {
		this.apiBal = apiBal;
	}

	public double getLoanReceiveMonthly() {
		return loanReceiveMonthly;
	}

	public void setLoanReceiveMonthly(double loanReceiveMonthly) {
		this.loanReceiveMonthly = loanReceiveMonthly;
	}

	public double getDepositReceiveMonthly() {
		return depositReceiveMonthly;
	}

	public void setDepositReceiveMonthly(double depositReceiveMonthly) {
		this.depositReceiveMonthly = depositReceiveMonthly;
	}

	public double getLoanDisburseMonthly() {
		return loanDisburseMonthly;
	}

	public void setLoanDisburseMonthly(double loanDisburseMonthly) {
		this.loanDisburseMonthly = loanDisburseMonthly;
	}

	public double getReturnMaturityMonthly() {
		return returnMaturityMonthly;
	}

	public void setReturnMaturityMonthly(double returnMaturityMonthly) {
		this.returnMaturityMonthly = returnMaturityMonthly;
	}

	


	
	
	
}
