package info.bukova.isspst.ui.signeddocs;

import info.bukova.isspst.data.SignedDocument;
import info.bukova.isspst.data.SignedDocumentItem;
import info.bukova.isspst.filters.SignedDocumentFilter;
import info.bukova.isspst.reporting.ReportDefinition;
import info.bukova.isspst.services.signeddocs.SignedDocumentService;
import info.bukova.isspst.ui.ListViewModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.BindingParam;
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.Listbox;
import org.zkoss.zul.Window;

import java.util.ArrayList;
import java.util.List;

public class SignedDocsList extends ListViewModel<SignedDocument>
{
	@SuppressWarnings("unused")
	private final static Logger log = LoggerFactory.getLogger(SignedDocsList.class.getName());

	@WireVariable
	protected SignedDocumentService signedDocumentService;

	protected SignedDocumentItem signedDocumentItem;

	protected List<SignedDocumentItem> signedDocumentItems;

	@WireVariable
	private ReportDefinition reportDefinition;

	@Init(superclass = true)
	public void initSignedDocsList() {
		service = signedDocumentService;
		dataClass = SignedDocument.class;
		formZul = "form.zul";
		dataFilter = new SignedDocumentFilter(getFilterTemplate());
		this.signedDocumentItem = null;
		this.signedDocumentItems = new ArrayList<SignedDocumentItem>();
	}

	public SignedDocumentItem getSignedDocumentItem() {
		return signedDocumentItem;
	}

	public void setSignedDocumentItem(SignedDocumentItem signedDocumentItem) {
		this.signedDocumentItem = signedDocumentItem;
	}

	public List<SignedDocumentItem> getSignedDocumentItems() {
		return signedDocumentItems;
	}

	public void setSignedDocumentItems(List<SignedDocumentItem> signedDocumentItems) {
		this.signedDocumentItems = signedDocumentItems;
	}

	@Command
	@NotifyChange("signedDocumentItems")
	public void onChangeSelectSignedDocs(@BindingParam("ctrl") Listbox lb) {
		if (lb == null) {
			return;
		}

		if (lb.getSelectedIndex() > -1) {
			this.signedDocumentItems = this.getDataBean().getItems();
		}
		else {
			this.signedDocumentItems = new ArrayList<SignedDocumentItem>();
		}
	}

	@Command
	public void onOpen(@BindingParam("item") SignedDocumentItem item) {
		reportDefinition.clear();
		reportDefinition.setSignedDocItem(item);
		Window reportWin = (Window) Executions.createComponents("/app/reporting/report.zul", null, null);
		reportWin.doModal();
	}
}
