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.Date;

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 main_package.CenterInfo;
import main_package.DataBaseConnection;
import main_package.DataBaseMethodRaviJ;
import main_package.DataBaseMethods;
import main_package.UserInfo;

@ManagedBean(name="combineCdsReport")
@ViewScoped

public class CombineCdsReportBean implements Serializable
{
	int branchId;
	
	
	double totalLoan, totalRd, totalAmount;
	
	
	String frequency, center, centerName, strSearchDate, strFromSearchDate, dayOfWeek, dsaCode = "all", loanType = "grouploan";
	
	String selectedFrequencyText;
	
	String[] multiCenter, multiFrequency;
	
	
	boolean show, showCenter;
	
	
	Date searchDate = new Date(), fromsSearchDate;
	
	
	ArrayList<UserInfo> list;
	
	ArrayList<SelectItem> dsaList, centerList;
	
	
	CenterInfo selectedCenter;
	
	
	DataBaseMethods dbm = new DataBaseMethods();
	
	DataBaseMethodRaviJ dbmr = new DataBaseMethodRaviJ();
	
	
	
	public CombineCdsReportBean() 
	{
		Connection conn = DataBaseConnection.JAVAConnection();
		HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId = (int) ss.getAttribute("branchId");
		
		dsaList = dbm.allDsaList(branchId, "Loan", conn);
		
		if(fromsSearchDate == null)
		{
			fromsSearchDate = new Date();
		}
		
		checkCenterMethod();
		
		try 
		{
			conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	
	
	public void searchData()
	{
		HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn = DataBaseConnection.JAVAConnection();
//		list = new DataBaseReportMethods().combineCdsReportMethod(branchId, searchDate, fromsSearchDate, frequency, multiCenter, 
//																  dsaCode, loanType, conn);
		
		list = dbmr.combineCdsReportMethod(branchId, searchDate, fromsSearchDate, multiFrequency, multiCenter, dsaCode, loanType, 
										   conn);
		
		if(list.size() > 0)
		{
			show = true;
			totalLoan = totalRd = totalAmount = 0;
			
			for(UserInfo uu: list)
			{
				totalAmount += uu.getTotalLoanRdEmiAmtDue();
				totalLoan += uu.getLoanEmiAmtDue();
				totalRd += uu.getRdEmiAmtDue();
			}
			
			if(multiCenter.length == 1)
			{
				center = multiCenter[0];
				showCenter = false;
				centerName = new DataBaseMethods().centerNameById(center, conn);
				selectedCenter = new DataBaseMethods().selectedCenterInfo(center);
			}
			else
			{
				showCenter = true;
				center = "ALL";
				centerName = "ALL";
				selectedCenter = new CenterInfo();
			}
			
			strSearchDate = new SimpleDateFormat("dd/MMM/yyyy").format(searchDate);
			strFromSearchDate = new SimpleDateFormat("dd/MMM/yyyy").format(fromsSearchDate);
			dayOfWeek = new SimpleDateFormat("EE").format(searchDate);
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("No Record Found!!!"));
		}
		
		try 
		{
			conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	

	public void checkCenterMethod()
	{
		centerList = dbm.centerListOfParticularDate(branchId, searchDate, fromsSearchDate, loanType, dsaCode);
		//System.out.println(searchDate+"Method==="+centerList.size());
	}
	
	
	
	public void printData()
	{
		HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("cdsList", list);
		ss.setAttribute("cdsCenter", center);
		ss.setAttribute("cdsFrequency", frequency);
		ss.setAttribute("cdsSearchDate", searchDate);
		
		RequestContext.getCurrentInstance().execute("window.open('printCombineCdsReport.xhtml')");
		RequestContext.getCurrentInstance().execute("window.open('printCombineCdsReport2.xhtml')");
	}
	
	
	
	public String getSelectedFrequencyText()
	{
	    if(multiFrequency == null || multiFrequency.length == 0)
	    {
	        return "";
	    }

	    String text = "";

	    for(String f : multiFrequency)
	    {
	        if(text.length() > 0)
	        {
	            text += ", ";
	        }

	        if(f.equals("365")) text += "Daily";
	        else if(f.equals("52")) text += "1 Week";
	        else if(f.equals("36")) text += "10 Days";
	        else if(f.equals("26")) text += "2 Week";
	        else if(f.equals("24")) text += "15 Days";
	        else if(f.equals("13")) text += "4 Week";
	        else if(f.equals("12")) text += "Monthly";
	        else text += f;
	    }

	    return text;
	}
	
	
	
	public Date getSearchDate() {
		return searchDate;
	}

	public void setSearchDate(Date searchDate) {
		this.searchDate = searchDate;
	}

	public String getFrequency() {
		return frequency;
	}

	public void setFrequency(String frequency) {
		this.frequency = frequency;
	}

	public ArrayList<UserInfo> getList() {
		return list;
	}

	public void setList(ArrayList<UserInfo> list) {
		this.list = list;
	}

	public double getTotalLoan() {
		return totalLoan;
	}

	public void setTotalLoan(double totalLoan) {
		this.totalLoan = totalLoan;
	}

	public double getTotalRd() {
		return totalRd;
	}

	public void setTotalRd(double totalRd) {
		this.totalRd = totalRd;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getCenter() {
		return center;
	}

	public void setCenter(String center) {
		this.center = center;
	}

	public String getCenterName() {
		return centerName;
	}

	public void setCenterName(String centerName) {
		this.centerName = centerName;
	}

	public boolean isShowCenter() {
		return showCenter;
	}

	public void setShowCenter(boolean showCenter) {
		this.showCenter = showCenter;
	}

	public String getStrSearchDate() {
		return strSearchDate;
	}

	public void setStrSearchDate(String strSearchDate) {
		this.strSearchDate = strSearchDate;
	}

	public String getDayOfWeek() {
		return dayOfWeek;
	}

	public void setDayOfWeek(String dayOfWeek) {
		this.dayOfWeek = dayOfWeek;
	}

	public CenterInfo getSelectedCenter() {
		return selectedCenter;
	}

	public void setSelectedCenter(CenterInfo selectedCenter) {
		this.selectedCenter = selectedCenter;
	}

	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}

	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}

	public String getDsaCode() {
		return dsaCode;
	}

	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}

	public ArrayList<SelectItem> getCenterList() {
		return centerList;
	}

	public void setCenterList(ArrayList<SelectItem> centerList) {
		this.centerList = centerList;
	}

	public String[] getMultiCenter() {
		return multiCenter;
	}

	public void setMultiCenter(String[] multiCenter) {
		this.multiCenter = multiCenter;
	}

	public String getLoanType() {
		return loanType;
	}

	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}

	public Date getFromsSearchDate() {
		return fromsSearchDate;
	}

	public void setFromsSearchDate(Date fromsSearchDate) {
		this.fromsSearchDate = fromsSearchDate;
	}

	public String getStrFromSearchDate() {
		return strFromSearchDate;
	}

	public void setStrFromSearchDate(String strFromSearchDate) {
		this.strFromSearchDate = strFromSearchDate;
	}



	public String[] getMultiFrequency() {
		return multiFrequency;
	}



	public void setMultiFrequency(String[] multiFrequency) {
		this.multiFrequency = multiFrequency;
	}



	public void setSelectedFrequencyText(String selectedFrequencyText) {
		this.selectedFrequencyText = selectedFrequencyText;
	}
	
	
}
