package main_package;

import java.io.Serializable;
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.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import main_package.DataBaseMethods;
import main_package.FixedAssetsInfo;

@ManagedBean(name="dep")
@ViewScoped
public class DepreciationBean implements Serializable
{
	ArrayList<FixedAssetsInfo> list;
	ArrayList<SelectItem> yearList;
	String year, dt, depType;
	boolean show, showDep;
	DataBaseMethods obj=new DataBaseMethods();
	public DepreciationBean() 
	{
		yearList=new ArrayList<>();
		for(int i=2015; i<=2025; i++)
		{
			SelectItem tt=new SelectItem();
			
			tt.setLabel(String.valueOf(i));
			tt.setValue(String.valueOf(i));
			
			yearList.add(tt);
		}
	}
	
	public void search()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		show=true;
		dt=year+"-03-31";
		/*boolean check=obj.checkDepreciationDuplicacy(year);
		if(check==true)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Depreciation Already Applied for Selected year."));
		}
		else
		{
			list=obj.fixedAssetsList(dt, year);
		}*/
		
		list=obj.fixedAssetsList(branchId,dt, year);
		for(FixedAssetsInfo ff: list)
		{
			if(ff.getDepDeduct()>0)
			{
				showDep=true;
			}
		}
	}
	
	public void calculateDep()
	{
		FacesContext context= FacesContext.getCurrentInstance();
		int k=(int) UIComponent.getCurrentComponent(context).getAttributes().get("serialNo");
		
		for(FixedAssetsInfo ll: list)
		{
			if(k==ll.getSno())
			{
				double dep=0;
				if(depType.equals("percent"))
				{
					dep=(ll.getPresentValue()*ll.getDepPercent())/100;
				}
				else
				{
					dep=ll.getDepPercent();
				}
				ll.setDepValue(dep);
				ll.setAfterDepValue(ll.getPresentValue()-ll.getDepValue());
			}
		}	
	}

	public String submitDep()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		int i=obj.addDepreciation(branchId,list, year, depType);
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Depreciation Add Sucessfully"));
			search();
			//return "userBlockDashboard.xhtml";
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error in add Depreciation. Plz check..."));
		}
		return null;
	}
	
	
	public ArrayList<FixedAssetsInfo> getList() {
		return list;
	}

	public void setList(ArrayList<FixedAssetsInfo> list) {
		this.list = list;
	}

	public ArrayList<SelectItem> getYearList() {
		return yearList;
	}

	public void setYearList(ArrayList<SelectItem> yearList) {
		this.yearList = yearList;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public String getDt() {
		return dt;
	}

	public void setDt(String dt) {
		this.dt = dt;
	}

	public String getDepType() {
		return depType;
	}

	public void setDepType(String depType) {
		this.depType = depType;
	}

	public boolean isShowDep() {
		return showDep;
	}

	public void setShowDep(boolean showDep) {
		this.showDep = showDep;
	}
	
	
}
