package info.bukova.isspst.ui.main.approved;

import info.bukova.isspst.data.JoinedItem;
import info.bukova.isspst.data.Order;
import info.bukova.isspst.data.User;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.filters.JoinedItemFilter;
import info.bukova.isspst.filters.TristateBoolean;
import info.bukova.isspst.services.orders.ApprovedService;
import info.bukova.isspst.services.orders.OrderService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import info.bukova.isspst.ui.ListViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.AccessDeniedException;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window;

public class ApprovedList extends ListViewModel<JoinedItem>
{
	@WireVariable
	protected ApprovedService approvedService;

	@WireVariable
	protected WorkgroupService workgroupService;

	@WireVariable
	protected UserService userService;

	@WireVariable
	protected OrderService orderService;

	protected List<JoinedItem> selectedItems;
	
	@Init(superclass = true)
	public void initApprovedList()
	{
		service = approvedService;
		dataClass = JoinedItem.class;
		// formZul = "form.zul";
		dataFilter = new JoinedItemFilter(getFilterTemplate());
		selectedItems = new ArrayList<JoinedItem>();
		this.itemMaterial = new TristateBoolean();
	}

	public List<JoinedItem> getItems()
	{
		return approvedService.getAll();
	}

	public List<JoinedItem> getSelectedItems()
	{
		return this.selectedItems;
	}

	public void setSelectedItems(List<JoinedItem> selectedItems)
	{
		this.selectedItems = selectedItems;
	}

	public List<Workgroup> getWorkgroups()
	{
		return this.workgroupService.getWorkgroups();
	}

	public List<Workgroup> getCenters()
	{
		return this.workgroupService.getCentres();
	}

	public List<User> getUsers()
	{
		return this.userService.getUsersForCombo();
	}

	@Command
	// @NotifyChange({ "selectedItems" })
	public void onCreateOrder()
	{
		if (this.selectedItems == null)
		{
			return;
		}

		if (this.selectedItems.size() == 0)
		{
			return;
		}

		Order order = orderService.createOrder(this.selectedItems);

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("selected", order);
		params.put("service", orderService);

		Window orderForm = (Window) Executions.createComponents("/main/orders/created/orderForm.zul", null, params);
		orderForm.doModal();
	}

	@Override
	protected List<JoinedItem> getListFromService() 
	{
		try 
		{
			return super.getListFromService();
		} catch (AccessDeniedException e) 
		{
			return new ArrayList<JoinedItem>();
		}
	}
	
	protected TristateBoolean itemMaterial;
	
	public TristateBoolean getItemMaterial()
	{
		return this.itemMaterial;
	}

	public void setItemMaterial(TristateBoolean tristate)
	{
		this.itemMaterial = tristate;
		this.getFilterTemplate().setItemMaterial(tristate.getBoolean());
	}
}
