package info.bukova.isspst.ui.main;

import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.RequirementBase;
import info.bukova.isspst.services.requirement.ApproveException;
import info.bukova.isspst.services.requirement.RequirementBaseService;
import info.bukova.isspst.ui.ListViewModel;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import java.util.Date;

/**
 * @author Pepa Rokos
 */
public class ApproveDialogVM {

	private RequirementBaseService service;
	private RequirementBase requirement;
	private ListViewModel grid;
	private Date approveDate;

	public Date getApproveDate() {
		return approveDate;
	}

	public void setApproveDate(Date approveDate) {
		this.approveDate = approveDate;
	}

	@Init
	public void init(@ExecutionArgParam("service") RequirementBaseService service,
					 @ExecutionArgParam("requirement") RequirementBase requirement,
					 @ExecutionArgParam("grid") ListViewModel grid) {
		this.service = service;
		this.requirement = requirement;
		this.grid = grid;
		this.approveDate = new Date();
	}

	@Command
	public void approve(@BindingParam("window") Window window) {
		try {
			service.approve(requirement, approveDate);
			BindUtils.postNotifyChange(null, null, grid, "dataBean");
			BindUtils.postNotifyChange(null, null, grid, "canApprove");
			BindUtils.postGlobalCommand(null, null, "reload", null);
			window.detach();
		} catch (ApproveException ex) {
			Messagebox.show(StringUtils.localize(ex.getReason()), StringUtils.localize("Error"), Messagebox.OK,
					Messagebox.ERROR);
		}
	}

}
