package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
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;

@ManagedBean(name="headerBean")
@ViewScoped
public class HeaderBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	String name,address1,address2,address3,address4,logo, mobileNo, sentBy,branchName, branchCode,companyName,company_type,designation;
	String selectedSession, startFinYear, endFinYear, currentSession, msg="";
	ArrayList<SelectItem> loanTypeList,bankNameList, cityNameList,payModeList;
	DataBaseMethods db=new DataBaseMethods();
	UserInfo selected;
	boolean sessionFlag, disableEntryDate, showSoftwareType;
	Date minFinYearDate, maxFinYearDate;
	int branchId,offerLatter3;
    private GlGuarantorDetails guarantor = new GlGuarantorDetails();
    
	public HeaderBean()
	{	
		
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		loanTypeList=(ArrayList<SelectItem>) ss.getAttribute("loanTypeList");
		bankNameList=(ArrayList<SelectItem>) ss.getAttribute("bankNameList");
		cityNameList=(ArrayList<SelectItem>) ss.getAttribute("cityNameList");
		payModeList =(ArrayList<SelectItem>) ss.getAttribute("payModeList");

		selected=(UserInfo) ss.getAttribute("selectedDataForHeaderBean");
		sessionFlag=(boolean) ss.getAttribute("sessionFlag");
		selectedSession=(String) ss.getAttribute("session");
		currentSession=(String) ss.getAttribute("currentSession");
		startFinYear=(String)ss.getAttribute("startFinYear");
		endFinYear=(String)ss.getAttribute("endFinYear");
		minFinYearDate=(Date) ss.getAttribute("minFinYearDate");
		maxFinYearDate=(Date) ss.getAttribute("maxFinYearDate");
		disableEntryDate=(boolean) ss.getAttribute("disableEntryDate");
		showSoftwareType=(boolean) ss.getAttribute("showSoftwareType");
		branchId = (int) ss.getAttribute("branchId");
		name=selected.getFirm_name();
		
		companyName=(String) ss.getAttribute("companyName");
        		
		address1=selected.getAddress1();
		address2=selected.getAddress2();
		address3=selected.getAddress3();
		address4=selected.getAddress4();
		logo=selected.getLogo();
		mobileNo=selected.getMobile();
		sentBy=selected.getSentBy();
		branchName=selected.getBranch();
		branchCode=selected.getBranchCode();

		company_type =selected.getCompanytype();
		
	   		ss.setAttribute("companyType", company_type);
	   		
		//	companyName = (String) ss.getAttribute("companyName"); 
	   		
		if(selected.firm_name!=null)
		{
			
			if (!selected.firm_name.equalsIgnoreCase("null") &&
				    selected.firm_name.equalsIgnoreCase("FINAZONE MICRO SERVICES FOUNDATION")) {

				    // Safely fetch from session and cast to int
				    Object offerLatterObj = ss.getAttribute("offerLatter3");
				    
				    if (offerLatterObj != null) 
				    {
				        offerLatter3 = (int) offerLatterObj;
				        address1= "Mustatil No-,Kila No-4/1(2-2),4/2(1-2),5Min(4/16),Third Floor" ;
				    	address2= "Ghitorni Mehrauli Village,Delhi,Delhi-110030";
				    	address3= ".";
						address4=".";
				    } 
				    else  
				    {
				        offerLatter3 = 0; // or handle it some other way
				    }
				}
	
		}


		
		Date currDate=new Date();
		
		int days=0;
		SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
		String eDate="31-03-2018";
		Date endDate = null;
		try {
			endDate = f.parse(eDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		endDate.setHours(currDate.getHours());
		endDate.setMinutes(currDate.getMinutes());
		endDate.setSeconds(currDate.getSeconds());
		
		
		days = (int) ((endDate.getTime() - currDate.getTime()) / (1000 * 60 * 60 * 24));
		days+=1;
		
		String arr[]=selectedSession.split("-");
		int sy=Integer.parseInt(arr[0]);
		int ey=Integer.parseInt(arr[1]);
		String fy=String.valueOf(sy-1)+"-"+String.valueOf(ey-1);
		
		if(currDate.before(endDate))
		{
			if(days<=20)
			{
				msg=days+" Days Left... Please Update All Entry For The Current Financial Year "+selectedSession;
			}
		}
		else if(days==1)
		{
			msg="Finalize Balance Sheet For The Financial Year "+selectedSession;
		}
		else if(currDate.after(endDate))
		{
			msg="Please Update All Entry And Finalize Balance Sheet For The Financial Year "+fy;
		}
		
	
		
	}
	
	public String getSelectedSession()
	{
		return selectedSession;
	}

	public void setSelectedSession(String selectedSession) {
		this.selectedSession = selectedSession;
	}

	public String getStartFinYear() {
		return startFinYear;
	}

	public void setStartFinYear(String startFinYear) {
		this.startFinYear = startFinYear;
	}

	public String getEndFinYear() {
		return endFinYear;
	}

	public void setEndFinYear(String endFinYear) {
		this.endFinYear = endFinYear;
	}

	public String getLogo() {
		return logo;
	}
	public void setLogo(String logo) {
		this.logo = logo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	}
	public String getAddress3() {
		return address3;
	}
	public void setAddress3(String address3) {
		this.address3 = address3;
	}
	public String getAddress4() {
		return address4;
	}
	public void setAddress4(String address4) {
		this.address4 = address4;
	}
	
	
	public ArrayList<SelectItem> getLoanTypeList() {
		return loanTypeList;
	}
	public void setLoanTypeList(ArrayList<SelectItem> loanTypeList) {
		this.loanTypeList = loanTypeList;
	}

	public ArrayList<SelectItem> getBankNameList() {
		return bankNameList;
	}

	public void setBankNameList(ArrayList<SelectItem> bankNameList) {
		this.bankNameList = bankNameList;
	}

	public String getMobileNo() {
		return mobileNo;
	}

	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}

	public String getCurrentSession() {
		return currentSession;
	}

	public void setCurrentSession(String currentSession) {
		this.currentSession = currentSession;
	}

	public Date getMinFinYearDate() {
		return minFinYearDate;
	}

	public void setMinFinYearDate(Date minFinYearDate) {
		this.minFinYearDate = minFinYearDate;
	}

	public Date getMaxFinYearDate() {
		return maxFinYearDate;
	}

	public void setMaxFinYearDate(Date maxFinYearDate) {
		this.maxFinYearDate = maxFinYearDate;
	}

	public boolean isDisableEntryDate() {
		return disableEntryDate;
	}

	public void setDisableEntryDate(boolean disableEntryDate) {
		this.disableEntryDate = disableEntryDate;
	}

	public ArrayList<SelectItem> getCityNameList() {
		return cityNameList;
	}

	public void setCityNameList(ArrayList<SelectItem> cityNameList) {
		this.cityNameList = cityNameList;
	}

	public boolean isShowSoftwareType() {
		return showSoftwareType;
	}

	public void setShowSoftwareType(boolean showSoftwareType) {
		this.showSoftwareType = showSoftwareType;
	}

	public ArrayList<SelectItem> getPayModeList() {
		return payModeList;
	}

	public void setPayModeList(ArrayList<SelectItem> payModeList) {
		this.payModeList = payModeList;
	}

	public String getSentBy() {
		return sentBy;
	}

	public void setSentBy(String sentBy) {
		this.sentBy = sentBy;
	}

	public String getBranchName() {
		return branchName;
	}

	public void setBranchName(String branchName) {
		this.branchName = branchName;
	}

	public String getBranchCode() {
		return branchCode;
	}

	public void setBranchCode(String branchCode) {
		this.branchCode = branchCode;
	}

	public String getCompany_type() {
		return company_type;
	}

	public void setCompany_type(String company_type) {
		this.company_type = company_type;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	

	
}
