package info.bukova.isspst.ui.main.orders.material;

import info.bukova.isspst.data.Material;
import info.bukova.isspst.filters.MaterialFilter;
import info.bukova.isspst.services.reqsubjects.MaterialService;
import info.bukova.isspst.ui.main.orders.requirements.SelectItems;

import java.util.List;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;

public class SelectMaterialItems extends SelectItems
{
	@WireVariable
	private MaterialService materialService;

	private List<Material> materialList;

	private List<Material> fullMaterialList;
	

	private boolean activeFilterMaterial;

	private Material filterTmpMaterial;

	private MaterialFilter dataFilterMaterial;


	@Init(superclass = true)
	public void initSelectMaterialItems()
	{
		// super.init();

		this.setFullMaterialList(materialService.getAll());
		this.setMaterialList(this.getFullMaterialList());

		this.setActiveFilterMaterial(false);
		this.setFilterTmpMaterial(new Material());
		this.setDataFilterMaterial(new MaterialFilter(this.getFilterTmpMaterial()));
	}

	public List<Material> getMaterialList()
	{
		return materialList;
	}

	public void setMaterialList(List<Material> materialList)
	{
		this.materialList = materialList;
	}

	public List<Material> getFullMaterialList()
	{
		return fullMaterialList;
	}

	public void setFullMaterialList(List<Material> fullMaterialList)
	{
		this.fullMaterialList = fullMaterialList;
	}

	public boolean isActiveFilterMaterial()
	{
		return activeFilterMaterial;
	}

	public void setActiveFilterMaterial(boolean activeFilterMaterial)
	{
		this.activeFilterMaterial = activeFilterMaterial;
	}

	public Material getFilterTmpMaterial()
	{
		return this.filterTmpMaterial;
	}

	private void setFilterTmpMaterial(Material material)
	{
		this.filterTmpMaterial = material;
	}

	public MaterialFilter getDataFilterMaterial()
	{
		return dataFilterMaterial;
	}

	public void setDataFilterMaterial(MaterialFilter dataFilterMaterial)
	{
		this.dataFilterMaterial = dataFilterMaterial;
	}

	@Command
	@NotifyChange({ "activeFilterMaterial", "materialList", "selectedItem" })
	public void onFilterMaterial()
	{
		this.setSelectedItem(null);

		this.setActiveFilterMaterial(!this.isActiveFilterMaterial());

		if (this.isActiveFilterMaterial())
		{
			this.doFilterMaterial();
		}
		else
		{
			this.setMaterialList(this.getFullMaterialList());
		}
	}

	@Command
	@NotifyChange("materialList")
	public void doFilterMaterial()
	{
		this.setSelectedItem(null);
		List<Material> result = this.materialService.filterList(this.getFullMaterialList(), this.getDataFilterMaterial());
		this.setMaterialList(result);
	}
}
