package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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;

@ManagedBean(name = "employeePerformance")
@ViewScoped
public class EmployeePerformanceDataBean implements Serializable
{
	int branchId;
	
	String dsaCode;
	
	Date todayDate, startDate, endDate, lastMonthStartDate, lastMonthEndDate, yearStartDate, yearEndDate;
	
	boolean showManager, showEmployee;
	
	ArrayList<PerformanceDataInfo> list = new ArrayList<>();
	
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	
	ArrayList<SelectItem> dsaList = new ArrayList<>();
	
	DataBaseMethodTaheer dbt = new DataBaseMethodTaheer();
	
	DataBaseMethods db = new DataBaseMethods();
	
	
	
	public EmployeePerformanceDataBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		HttpSession ss =(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		branchId =(int) ss.getAttribute("branchId");
		String companyName = (String) ss.getAttribute("companyName");
		String branchUserId = (String) ss.getAttribute("branchUserId");
		System.out.println("cn:"+companyName);
		
		todayDate = new Date();
		startDate = new Date();
		endDate = new Date();
		startDate.setDate(1);

		branchList = new DataBaseMethods().branchList(branchUserId,conn);
		dsaList = db.allDsaList(branchId, "", conn);
	}
	
	
	
	public void searchData()
	{
		if (dsaCode.equalsIgnoreCase("all")) 
		{
			showManager = true;
			showEmployee = false;
		}
		else 
		{
			showEmployee = true;
			showManager = false;
		}
		
		lastMonthStartDate = getLastMonthStart(startDate);
		lastMonthEndDate = getLastMonthEnd(endDate);
		
		yearStartDate = getFinancialYearStart(startDate);
		yearEndDate = getFinancialYearEnd(endDate);
		
		list = dbt.employeePerformanceData(branchId, new java.sql.Date(startDate.getTime()),new java.sql.Date(endDate.getTime()), 
										   new java.sql.Date(yearStartDate.getTime()), new java.sql.Date(yearEndDate.getTime()), 
										   new java.sql.Date(lastMonthStartDate.getTime()), 
										   new java.sql.Date(lastMonthEndDate.getTime()), dsaCode, endDate);
	}
	
	
	
	public static Date getLastMonthStart(Date date) 
	{
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }


	
	public static Date getLastMonthEnd(Date date) 
	{
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);
	    cal.add(Calendar.MONTH, -1);
	    cal.set(Calendar.DAY_OF_MONTH,
	            cal.getActualMaximum(Calendar.DAY_OF_MONTH));
	    return cal.getTime();
	}
	
	
	
	public static Date getFinancialYearStart(Date date) 
	{
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);

	    int year = cal.get(Calendar.YEAR);
	    int month = cal.get(Calendar.MONTH);

	    if (month < Calendar.APRIL) 
	    {
	        year = year - 1;
	    }

	    cal.clear();
	    cal.set(Calendar.YEAR, year);
	    cal.set(Calendar.MONTH, Calendar.APRIL);
	    cal.set(Calendar.DAY_OF_MONTH, 1);

	    return cal.getTime();
	}
	
	
	
	public static Date getFinancialYearEnd(Date date) 
	{
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);

	    int year = cal.get(Calendar.YEAR);
	    int month = cal.get(Calendar.MONTH);

	    if (month >= Calendar.APRIL) 
	    {
	        year = year + 1;
	    }

	    cal.clear();
	    cal.set(Calendar.YEAR, year);
	    cal.set(Calendar.MONTH, Calendar.MARCH);
	    cal.set(Calendar.DAY_OF_MONTH, 31);

	    return cal.getTime();
	}

    

	public int getBranchId() {
		return branchId;
	}



	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}



	public Date getTodayDate() {
		return todayDate;
	}



	public void setTodayDate(Date todayDate) {
		this.todayDate = todayDate;
	}



	public ArrayList<PerformanceDataInfo> getList() {
		return list;
	}



	public void setList(ArrayList<PerformanceDataInfo> list) {
		this.list = list;
	}



	public List<SelectItem> getBranchList() {
		return branchList;
	}



	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}

	

	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 String getDsaCode() {
		return dsaCode;
	}



	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;
	}



	public ArrayList<SelectItem> getDsaList() {
		return dsaList;
	}



	public void setDsaList(ArrayList<SelectItem> dsaList) {
		this.dsaList = dsaList;
	}



	public boolean isShowManager() {
		return showManager;
	}



	public void setShowManager(boolean showManager) {
		this.showManager = showManager;
	}



	public boolean isShowEmployee() {
		return showEmployee;
	}



	public void setShowEmployee(boolean showEmployee) {
		this.showEmployee = showEmployee;
	}
	
}