package main_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;

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="changeFinYear")
@ViewScoped
public class ChangeFinYearBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	ArrayList<SelectItem> sessionList;
	String selectedSession;
	String selectedUnit;
	DataBaseMethods obj=new DataBaseMethods();
	Date minFinYearDate, maxFinYearDate;
	
	public ChangeFinYearBean()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		sessionList=obj.financialYearList(branchId,conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		selectedSession=(String) ss.getAttribute("session");
		
	}
	
	public void goToNext() throws IOException
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		HeaderBean hh=new HeaderBean();
		String arr[]=selectedSession.split("-");
		String startFinYear=arr[0];
		String endFinYear=arr[1];
		ss.setAttribute("session", selectedSession);
		//ss.setAttribute("currentSession", selectedSession);
		ss.setAttribute("startFinYear", startFinYear);
		ss.setAttribute("endFinYear", endFinYear);
		
		SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
		String sDate="01-04-"+startFinYear;
		String eDate="";
		
		if(hh.getCurrentSession().equals(selectedSession))
		{
			
			LocalDate dt = LocalDate.now();

			String day = String.valueOf(dt.getDayOfMonth()); // 
			String month = String.valueOf(dt.getMonthValue()); // 
			String year = String.valueOf(dt.getYear()); // 
			eDate=day+"-"+month+"-"+year;
		}
		else
		{
			eDate="31-03-"+endFinYear;
		}
		
		try 
		{
			minFinYearDate=f.parse(sDate);
			maxFinYearDate=f.parse(eDate);
		} 
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		ss.setAttribute("minFinYearDate", minFinYearDate);
		ss.setAttribute("maxFinYearDate", maxFinYearDate);
		
		Connection conn=DataBaseConnection.JAVAConnection();
		boolean disableEntryDate=obj.checkDataFreezing(branchId,selectedSession,conn);
		ss.setAttribute("disableEntryDate", disableEntryDate);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		FacesContext.getCurrentInstance().getExternalContext().redirect("mainDashBoard.xhtml");
	}

	public String getSelectedUnit() {
		return selectedUnit;
	}
	public void setSelectedUnit(String selectedUnit) {
		this.selectedUnit = selectedUnit;
	}
	public ArrayList<SelectItem> getSessionList() 
	{
		return sessionList;
	}
	
	public void setSessionList(ArrayList<SelectItem> sessionList) 
	{
		this.sessionList = sessionList;
	}
	public String getSelectedSession() {
		return selectedSession;
	}
	public void setSelectedSession(String selectedSession) {
		this.selectedSession = selectedSession;
	}
}
