package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
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 main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.DataBaseConnection;
import main_package.DataBaseMethodTaheer;
import main_package.UserInfo;

import org.primefaces.context.RequestContext;

@ManagedBean(name="allDueEmiReport")
@ViewScoped
public class AllDueEmiReportBean implements Serializable
{
	Date startDate=new Date(),endDate=new Date();
	ArrayList<UserInfo> list,multipleRows=new ArrayList<>();
	double totalAmount;
	String category, userId, frequency, dsaCode;
	DataBaseReportMethods obj1=new DataBaseReportMethods();
	DataBaseMethods obj=new DataBaseMethods();
	boolean show,disDsa;
	ArrayList<SelectItem> dsaList;
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();
	boolean disAll;
	String userType;
	DataBaseMethodTaheer dbmt= new DataBaseMethodTaheer();
    
	
	
	public AllDueEmiReportBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		
		String companyName = dbmt.companyNameByBranchId(branchId);
		Connection conn=DataBaseConnection.JAVAConnection();
		dsaList=obj.allDsaList(branchId,"",conn);
		String userId=obj.userId();
		String empDataStatus=obj.checkSMessageApiDetailFromTable("employee_wise_data", "",conn);
		
		if(obj.userType().equals("employee") && empDataStatus.equalsIgnoreCase("active"))
		{
			dsaCode=obj.dsaCodeByEmpUserId(userId, conn);
			
			if(companyName.equalsIgnoreCase("SUDERSHANA FINANCE"))
			{
				disDsa=false;
			}
			else 
			{
				disDsa=true;
			}
		}
		
		branchUserId = (String) ss.getAttribute("branchUserId");
		branchList = new DataBaseMethods().branchList(branchUserId,conn);
		
		String cc=(String) ss.getAttribute("companyName");

		userType= (String) ss.getAttribute("type");

		
	    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);

		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public void checkList()
	{
		if(branchId != 0)
		{
			show=true;
			Connection conn=DataBaseConnection.JAVAConnection();
			ArrayList<UserInfo> templist=obj1.allDueEMI(branchId,category, frequency, dsaCode, conn);
			list=new ArrayList<>();
			for(int i=0;i<templist.size();i++)
			{
				if(!templist.get(i).getEntryDateStr().equalsIgnoreCase(new SimpleDateFormat("dd/MM/yyyy").format(new Date())))
				{
					list.add(templist.get(i));
				}
			}
			Collections.sort(list);
			int i=1;
			totalAmount=0;
			for(UserInfo ll:list)
			{
				ll.setSno(i++);
				totalAmount+=ll.getAmount();
			}
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}
		
	}

	public String sendMsg()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	    int branchId = (int) ss.getAttribute("branchId");
		if(multipleRows.size()>0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			String check=new DataBaseMethods().checkStatusInMessageSettingTable("Due Emi");
			if(check.equals("active"))
			{
				for(UserInfo ll: multipleRows)
				{
					String typeMessage="Dear "+ll.getLoanNo()+" - "+ll.getFirst_applicantName()+", Your EMI Rs. "+ll.getAmount() +" is Due On "+ll.getEntryDateStr();
					obj.runMessageUrl(branchId,ll.getContactNo(), typeMessage, userId,"pending", conn);
					typeMessage="";
				}
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(multipleRows.size()+" Message Send Sucessfully"));
				return "mainDashBoard.xhtml";	
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Message For This Module is not Active. Please Go to In User Block and Active Message For This Module."));
			}
			
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Select Atleast One Contact no from List."));
		}
		return "";
	}
	
	public void printReport()
	{
		if(list.size()>0)
		{
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("printDueList", list);
			ss.setAttribute("printDueTotal", totalAmount);
			RequestContext.getCurrentInstance().execute("window.open('printDueEmiReport.xhtml')");
		}
	}
	
	public void printDateWise()
	{
		if(list.size()>0)
		{
			ArrayList<UserInfo> tempList=new ArrayList<>();
			double dueAmount = 0;
			int count=1;
			for(int i=0;i<list.size();i++)
			{
				if((list.get(i).getEntryDate().after(startDate) || list.get(i).getEntryDate().equals(startDate) ) && 
						(list.get(i).getEntryDate().before(endDate) || list.get(i).getEntryDate().equals(endDate)))
				{
					dueAmount+=list.get(i).getAmount();
					list.get(i).setSno(count++);
					tempList.add(list.get(i));
				}
			}
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			ss.setAttribute("printDueList", tempList);
			ss.setAttribute("printDueTotal", dueAmount);
			RequestContext.getCurrentInstance().execute("window.open('printDueEmiReport.xhtml')");
		}
	}
	
	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> getList() {
		return list;
	}
	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}
	public double getTotalAmount() {
		return totalAmount;
	}
	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public ArrayList<UserInfo> getMultipleRows() {
		return multipleRows;
	}

	public void setMultipleRows(ArrayList<UserInfo> multipleRows) {
		this.multipleRows = multipleRows;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getFrequency() {
		return frequency;
	}

	public void setFrequency(String frequency) {
		this.frequency = frequency;
	}

	public String getDsaCode() {
		return dsaCode;
	}

	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}

	public boolean isDisDsa() {
		return disDsa;
	}

	public void setDisDsa(boolean disDsa) {
		this.disDsa = disDsa;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public List<SelectItem> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}

	public boolean isDisAll() {
		return disAll;
	}

	public void setDisAll(boolean disAll) {
		this.disAll = disAll;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}
}
