package main_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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 org.primefaces.context.RequestContext;

import other_package.DataBaseMethodsOther;

@ManagedBean(name="mySideMenu")
@ViewScoped
public class SideMenuBean implements Serializable 
{
	private static final long serialVersionUID = 1L;
	String shareHolderNo;
	int count=0;
	List<String> list=new ArrayList<>();
	boolean showCredit,showDebit;
	String placeHolder="";
	ArrayList<SelectItem> loanTypeList,bankNameList, cityNameList,sessionList,payModeList;
	ArrayList<UserInfo> depositList,rdPayList,loanPayList,incomeList,fdrIncomeList,returnInstantLoanList,instantBorrowList,promotorsCapitalList, shareChqList;
	ArrayList<DueDebitInfo> debitLoanList,expenseList,fdrExpenseList,returnDSAList,salPayList,tdsSubList,rdFdList,finalList,withDrawList, instantLoanList, 
	returnInstantBorrowList, returnPromotorCapitalList;
	DataBaseMethods db=new DataBaseMethods();
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	UserInfo selected;
	boolean showAdmin,showEmployee, showTdsModule, showMsgModule, showUserApp, showAdminApp,showPersonalLoan, showPropertyLoan, showVehicleLoan, showGroupLoan, showOdLoan;
	boolean showMemberOdReport, showSoftwareType, showSecureLoan, showBusinessLoan,checkBranch=false;
	int branchId;
	
	public List<String> completeText(String name)
    {
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
        //System.out.println(placeHolder);
        if(placeHolder.equals("Enter Loan No"))
        {
            list=new DataBaseAutoCompAndDuplicate().loanList(branchId,name);
            return list;
        }
        else if(placeHolder.equals("Enter Deposit No"))
        {
            list=new DataBaseAutoCompAndDuplicate().depositList(branchId,name);
            return list;
        }
        else
        {
            list=new DataBaseAutoCompAndDuplicate().shareHolderAllNo(branchId,name);
             return list;
        }
       
    }
    
	
	public void demo() throws IOException
	{
		if(placeHolder.equals("Enter Loan No"))
		{
			if(shareHolderNo.equals("") || shareHolderNo==null)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Enter Loan No"));
			}
			else
			{
				String loanNo="";
				shareHolderNo=shareHolderNo.substring(0,shareHolderNo.indexOf("/"));
				loanNo=db.shareHolderNoByLoanNo(shareHolderNo);
				// System.out.println(loanNo);
				HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
				ss.setAttribute("shareId",loanNo);
				
				FacesContext.getCurrentInstance().getExternalContext().redirect("particularLoan.xhtml");
				
			}
		}
		else
		{
			if(shareHolderNo.equals("") || shareHolderNo==null)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Enter Share Holder No"));
				System.out.println("if share");
			}
			else
			{
				shareHolderNo=shareHolderNo.substring(0,shareHolderNo.indexOf("/"));
				HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
				ss.setAttribute("shareId",shareHolderNo);
				
				FacesContext.getCurrentInstance().getExternalContext().redirect("personalDetailDashboard.xhtml");
			}
	  }
	}
	
	public SideMenuBean() 
	{
		Connection conn = DataBaseConnection.JAVAConnection();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		branchId = (int) ss.getAttribute("branchId");
		String branchUserId= (String) ss.getAttribute("branchUserId");
		branchList = db.branchList(branchUserId,conn);
		branchId = (int) ss.getAttribute("branchId");
		
	    placeHolder="Name Or Member No";
	    	
		
		showDebit=false;
		showCredit=false;
		debitLoanList=db.pendingDebitLoanList(branchId);
		if((debitLoanList.size()>0))
		{
			showDebit=true;
		}
		else
		{
			showDebit=false;
			expenseList=db.pendingExpenseList(branchId);
			if((expenseList.size()>0))
			{
				showDebit=true;
			}
			else
			{
				showDebit=false;
				withDrawList=db.pendingWithdrawList(branchId);
				if((withDrawList.size()>0))
				{
					showDebit=true;
				}
				else
				{
					showDebit=false;
					fdrExpenseList=db.pendingFdrExpenseList(branchId);
					if((fdrExpenseList.size()>0))
					{
						showDebit=true;
					}
					else
					{
						showDebit=false;
						rdFdList=db.pendingReturnAmountRdFdList(branchId);
						if((rdFdList.size()>0))
						{
							showDebit=true;
						}
						else
						{
							showDebit=false;
							instantLoanList=db.pendingInstantLoanList(branchId);
							if((instantLoanList.size()>0))
							{
								showDebit=true;
							}
							else
							{
								showDebit=false;
								returnInstantBorrowList=db.pendingReturnInstantBorrowList(branchId);
								if((returnInstantBorrowList.size()>0))
								{
									showDebit=true;
								}
								else
								{
									showDebit=false;
									returnDSAList=db.pendingReturnDSAList(branchId);
									if((returnDSAList.size()>0))
									{
										showDebit=true;
									}
									else
									{
										showDebit=false;
										salPayList=db.pendingSalPayoutList(branchId);
										if((salPayList.size()>0))
										{
											showDebit=true;
										}
										else
										{
											showDebit=false;
											tdsSubList=db.pendingTdsSubList(branchId);
											if((tdsSubList.size()>0))
											{
												showDebit=true;
											}
											else
											{
												showDebit=false;
												returnPromotorCapitalList=db.pendingReturnPromotorsCapitalList(branchId);
												if((returnPromotorCapitalList.size()>0))
												{
													showDebit=true;
												}
												else
												{
													showDebit=false;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		//**********************************************************************
		
		depositList=db.depositPaymentList(branchId);
		if((depositList.size()>0))
		{
			showCredit=true;
		}
		else
		{
			showCredit=false;
			incomeList=db.pendingIncomeList(branchId);
			if((incomeList.size()>0))
			{
				showCredit=true;
			}
			else
			{
				showCredit=false;
				fdrIncomeList=db.pendingFdrIncomeList(branchId);
				if((fdrIncomeList.size()>0))
				{
					showCredit=true;
				}
				else
				{
					showCredit=false;
					rdPayList=db.allPayReceipt(branchId);
					if((rdPayList.size()>0))
					{
						showCredit=true;
					}
					else
					{
						showCredit=false;
						loanPayList=db.allLoanPayReceipt(branchId);
						if((loanPayList.size()>0))
						{
							showCredit=true;
						}
						else
						{
							showCredit=false;
							returnInstantLoanList=db.pendingReturnInstantLoanList(branchId);
							if((returnInstantLoanList.size()>0))
							{
								showCredit=true;
							}
							else
							{
								showCredit=false;
								instantBorrowList=db.pendingInstantBorrowList(branchId);
								if((instantBorrowList.size()>0))
								{
									showCredit=true;
								}
								else
								{
									showCredit=false;
									promotorsCapitalList=db.pendingPromotorsCapitalList(branchId);
									if((promotorsCapitalList.size()>0))
									{
										showCredit=true;
									}
									else
									{
										showCredit=false;
										shareChqList= db.pendingCheque(branchId);
										if((shareChqList.size()>0))
										{
											showCredit=true;
										}
										else
										{
											showCredit=false;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	public void changeMode()
    {
        shareHolderNo="";
        if(count==0)
        {
            placeHolder="Enter Loan No";
            count=1;
        }
        else if(count==1)
        {
            placeHolder="Enter Deposit No";
            count=2;
        }
        else
        {
            placeHolder="Name Or Share Holder No";
            count=0;
        }
       
    }

	public void changeBranch() throws IOException
	{
		Connection conn = DataBaseConnection.JAVAConnection();
        HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.setAttribute("branchId", branchId);
        String type=(String) ss.getAttribute("type");
        selected=db.firmDetails(branchId);
	    ss.setAttribute("branchId", branchId);
         loanTypeList=db.loanTypeList(conn);
         bankNameList=db.bankNameSelectItemList(conn);
         cityNameList=db.cityNameList(branchId,conn);
         payModeList=db.paymentModeOptionList(conn);
         ss.setAttribute("selectedDataForHeaderBean", selected);
 		ss.setAttribute("loanTypeList", loanTypeList);
 		ss.setAttribute("bankNameList", bankNameList);
		ss.setAttribute("cityNameList", cityNameList);
		ss.setAttribute("payModeList", payModeList);
         		
		String tds=db.checkTdsBasis(branchId,conn);
		if(tds.equals(""))
		{
			showTdsModule=false;
		}
		else
		{
			showTdsModule=true;
		}
//        		
		if(type.equalsIgnoreCase("admin")||type.equalsIgnoreCase("masterAdmin"))
		{
			showAdmin=true;
			
		}
		else
		{
			showAdmin=false;
		}

		
		String 	softType=db.checkSMessageApiDetailFromTable("software_type","", conn);	
		if(softType.equalsIgnoreCase("customized"))
		{
			showSoftwareType=true;
		}
		
		ss.setAttribute("showWelcomeAdmin", showAdmin);
		ss.setAttribute("showWelcomeEmployee", showEmployee);
		ss.setAttribute("showSoftwareType", showSoftwareType);
		ss.setAttribute("sessionFlag", true);
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FacesContext.getCurrentInstance().getExternalContext().redirect( "mainDashBoard.xhtml");
	}
	
	
	  public void loanStatement() throws IOException
	    {
	        boolean showPenaltyField=true;

	        if(placeHolder.equals("Enter Loan No"))
	        {
	            System.out.println("Welcome");
	            if(shareHolderNo.equals("") || shareHolderNo==null)
	            {
	                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Enter Loan No"));
	            }
	            else
	            {
	                String loanNo="";
	                shareHolderNo=shareHolderNo.substring(0,shareHolderNo.indexOf("/"));
	            
	                loanNo=db.shareHolderNoByLoanNo(shareHolderNo);
	                LoanInfo ll = new LoanInfo();
	                ll= new DataBaseMethodsOther().allLoanInfo(loanNo,shareHolderNo,ll);
	                //System.out.println(ll.getLoanType());
	                HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	                ss.setAttribute("selectedLoan",ll);
	                ss.setAttribute("shareId",loanNo);
	                ss.setAttribute("showPen",showPenaltyField);
	                ss.setAttribute("pageName","particularLoan.xhtml");
	                
	                FacesContext.getCurrentInstance().getExternalContext().redirect("loanReportParticular.xhtml");
	            }
	        }
	        else
	        {
	            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Enter Loan No."));
	        }
	        
	    }
	    
	    
	  public void depositStatement() throws IOException
	    {

	        if(placeHolder.equals("Enter Deposit No"))
	        {
	        if(shareHolderNo.equals("") || shareHolderNo==null)
	        {
	            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Enter Share Holder No"));
	        }
	        else
	        {
	            shareHolderNo=shareHolderNo.substring(0,shareHolderNo.indexOf("/"));
	            String aa=new DataBaseMethods().shareHolderNoByRdFdNo(shareHolderNo);
	            System.out.println(aa);
	            if(aa.equalsIgnoreCase(""))
	            {
	                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found"));
	            }
	            else
	            {
	            UserInfo nn=new UserInfo();
	           
	            nn=new DataBaseMethodsOther().allRd_FDInfo(shareHolderNo,nn);
	            HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	            ss.setAttribute("selectedUser", nn);
	            ss.setAttribute("pageName","particularRD_FD.xhtml");
	            FacesContext.getCurrentInstance().getExternalContext().redirect( "rd_fd_ReportParticular.xhtml");
	        //    ss.setAttribute("pageName","particularSaveAccount");
	        //    FacesContext.getCurrentInstance().getExternalContext().redirect("miniStatement.xhtml");
	            }
	        }
	        }
	        else
	        {
	            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Enter Deposit No."));
	        }
	     
	    }
	            
	public String getShareHolderNo() {
		return shareHolderNo;
	}
	public void setShareHolderNo(String shareHolderNo) {
		this.shareHolderNo = shareHolderNo;
	}

	public boolean isShowCredit() {
		return showCredit;
	}

	public String getPlaceHolder() {
		return placeHolder;
	}

	public void setPlaceHolder(String placeHolder) {
		this.placeHolder = placeHolder;
	}

	public void setShowCredit(boolean showCredit) {
		this.showCredit = showCredit;
	}

	public boolean isShowDebit() {
		return showDebit;
	}

	public void setShowDebit(boolean showDebit) {
		this.showDebit = showDebit;
	}


	public List<SelectItem> getBranchList() {
		return branchList;
	}


	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}


	public int getBranchId() {
		return branchId;
	}


	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}
	
	
}
