package info.bukova.isspst.ui.requirement;

import info.bukova.isspst.data.RequirementBase;
import info.bukova.isspst.data.User;
import info.bukova.isspst.services.requirement.RequirementBaseService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.ui.BigDecimalConverter;
import info.bukova.isspst.ui.ListViewModel;
import info.bukova.isspst.ui.renderers.RequirementsItemRenderer;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window;

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

public class RequirementSubpage<T extends RequirementBase> extends ListViewModel<T> {
	
	@WireVariable
	protected UserService userService;

	protected RequirementsItemRenderer requirementsItemRenderer;

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

	private BigDecimalConverter bigDecimalConverter;

	public BigDecimalConverter getBigDecimalConverter()
	{
		return bigDecimalConverter;
	}

	public void setBigDecimalConverter(BigDecimalConverter bigDecimalConverter)
	{
		this.bigDecimalConverter = bigDecimalConverter;
	}

	@Init(superclass = true)
	public void initRequirementSubpage()
	{
		this.bigDecimalConverter = new BigDecimalConverter();
		this.requirementsItemRenderer = new RequirementsItemRenderer();
	}

	protected RequirementBaseService<T> getReqService()
	{
		return (RequirementBaseService<T>) service;
	}
	
	@Command
	@NotifyChange({"dataBean", "canApprove"})
	public void approve() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("service", getReqService());
		params.put("requirement", getDataBean());
		params.put("grid", this);
		Window window = (Window) Executions.createComponents("/main/approveDialog.zul", null, params);
		window.doModal();
	}

	@Override
	@NotifyChange({ "dataBean", "ableToDelete", "canApprove" })
	public void setDataBean(T data) {
		super.setDataBean(data);
	}
	
	public boolean isCanApprove() {
		if (getDataBean() != null) {
			return this.getReqService().canApprove(getDataBean());
		}
		
		return false;
	}

	public RequirementsItemRenderer getRequirementsItemRenderer()
	{
		return requirementsItemRenderer;
	}

	public void setRequirementsItemRenderer(RequirementsItemRenderer requirementsItemRenderer)
	{
		this.requirementsItemRenderer = requirementsItemRenderer;
	}

	@Override
	protected void afterSelect() {
		BindUtils.postNotifyChange(null, null, this, "canApprove");
	}
}
