package info.bukova.isspst.ui.main;

import info.bukova.isspst.Constants;
import info.bukova.isspst.SessionData;
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.signapi.SignData;
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.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.WireVariable;
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;
	@WireVariable
	private SessionData sessionData;
	private boolean signed;
	private boolean signRequired;
	private boolean timer;
	private byte[] signedPdf;

	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();

		if (service.getNextWorkflow(requirement).getSignature() != null
				&& service.getNextWorkflow(requirement).getSignature()) {
			this.signed = false;
			this.signRequired = true;
		} else {
			this.signRequired = false;
		}

		this.timer = false;
	}

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

	@Command
	@NotifyChange("timer")
	public void signPdf() {
		if (service.prepareSignData(requirement, approveDate)) {
			timer = true;
			Executions.getCurrent().sendRedirect("/api/sign/signer.jnlp", "signer");
		}
	}

	@Command
	@NotifyChange({"signed", "timer"})
	public void onTimer() {
		String guid = (String) sessionData.getProperty(Constants.KEY_SIGN_GUID);
		SignData data = (SignData) sessionData.getProperty(Constants.KEY_SIGN_DATA);

		if (guid.equals(data.getSignGuid()) && data.isSignSuccess()) {
			signed = true;
			timer = false;
			signedPdf = data.getPdfData();
		} else {
			signed = false;
		}
	}

	public boolean isSigned() {
		return signed;
	}

	public boolean isTimer() {
		return timer;
	}

	public boolean isSignRequired() {
		return signRequired;
	}
}
