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

import info.bukova.isspst.data.ServiceItem;
import info.bukova.isspst.filters.ServiceItemFilter;
import info.bukova.isspst.services.reqsubjects.ServiceItemService;
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 SelectServicesItems extends SelectItems
{
	@WireVariable
	private ServiceItemService serviceItemService;

	private List<ServiceItem> serviceItemList;

	private List<ServiceItem> fullServiceItemList;

	
	private boolean activeFilterService;

	private ServiceItem filterTmpService;

	private ServiceItemFilter dataFilterService;


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

		this.setFullServiceItemList(serviceItemService.getAll());
		this.setServiceItemList(this.getFullServiceItemList());

		this.setActiveFilterService(false);
		this.setFilterTmpService(new ServiceItem());
		this.setDataFilterService(new ServiceItemFilter(this.getFilterTmpService()));
	}

	public List<ServiceItem> getServiceItemList()
	{
		return serviceItemList;
	}

	public void setServiceItemList(List<ServiceItem> serviceItemList)
	{
		this.serviceItemList = serviceItemList;
	}

	public List<ServiceItem> getFullServiceItemList()
	{
		return fullServiceItemList;
	}

	public void setFullServiceItemList(List<ServiceItem> fullServiceItemList)
	{
		this.fullServiceItemList = fullServiceItemList;
	}

	public boolean isActiveFilterService()
	{
		return activeFilterService;
	}

	public void setActiveFilterService(boolean activeFilterService)
	{
		this.activeFilterService = activeFilterService;
	}

	public ServiceItem getFilterTmpService()
	{
		return this.filterTmpService;
	}

	private void setFilterTmpService(ServiceItem serviceItem)
	{
		this.filterTmpService = serviceItem;
	}

	public ServiceItemFilter getDataFilterService()
	{
		return dataFilterService;
	}

	public void setDataFilterService(ServiceItemFilter dataFilterService)
	{
		this.dataFilterService = dataFilterService;
	}

	@Command
	@NotifyChange({ "activeFilterService", "serviceItemList", "selectedItem" })
	public void onFilterService()
	{
		this.setSelectedItem(null);

		this.setActiveFilterService(!this.isActiveFilterService());

		if (this.isActiveFilterService())
		{
			this.doFilterService();
		}
		else
		{
			this.setServiceItemList(this.getFullServiceItemList());
		}
	}

	@Command
	@NotifyChange("serviceItemList")
	public void doFilterService()
	{
		this.setSelectedItem(null);
		List<ServiceItem> result = this.serviceItemService.filterList(this.getFullServiceItemList(), this.getDataFilterService());
		this.setServiceItemList(result);
	}
}
