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

import info.bukova.isspst.data.Invoicing;
import info.bukova.isspst.data.InvoicingItem;
import info.bukova.isspst.services.invoicing.InvoicingService;
import info.bukova.isspst.ui.FormViewModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.BindUtils;
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.select.annotation.WireVariable;

public class InvoicingForm extends FormViewModel<Invoicing> {
	
	public final static String TAG = InvoicingForm.class.getSimpleName();
	private final static Logger log = LoggerFactory.getLogger(InvoicingForm.class.getSimpleName());

	private InvoicingItem selectedItem;
	private int selectedIndex;
	@WireVariable
	private InvoicingService invoicingService;
	
	protected Invoicing invoicingBeforeEdit;

	@Init(superclass = true)
	public void initInvoicingForm()
	{
		selectedIndex = -1;

		try
		{
			this.invoicingBeforeEdit = (Invoicing) this.getDataBean().clone();
		}
		catch (CloneNotSupportedException e)
		{
			log.error("Nelze provést hlubokou kopii fakturace!");
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doSave()
	{
		// Zjisti, zda se změnil příznak Vyřízeno
		boolean competedChanged = (this.invoicingBeforeEdit.isCompleted() != this.getDataBean().isCompleted());

		super.doSave();

		if (competedChanged)
		{
			BindUtils.postGlobalCommand(null, null, "refreshTabs", null);
		}
	}

	private void selectItem(InvoicingItem item) {
		if (item != null) {
			selectedItem = item;
			selectedIndex = getDataBean().getItems().indexOf(item);
		} else {
			selectedItem = null;
			selectedIndex = -1;
		}
	}
	
	@Command
	@NotifyChange({"dataBean", "selectedItem", "selectedIndex"})
	public void addItem() {
		InvoicingItem item = new InvoicingItem();
		getDataBean().getItems().add(item);
		selectItem(item);
	}
	
	@Command
	@NotifyChange({"dataBean", "selectedItem", "selectedIndex"})
	public void removeItem(@BindingParam("item") InvoicingItem item) {
		getDataBean().getItems().remove(item);
		selectItem(null);
		calculate();
	}
	
	@Command
	@NotifyChange({"selectedItem", "selectedIndex"})
	public void onFocus(@BindingParam("item") InvoicingItem item) {
		selectItem(item);
	}
	
	@Command
	@NotifyChange("dataBean")
	public void calculate() {
		invoicingService.calculate(getDataBean());
	}

	public InvoicingItem getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(InvoicingItem selectedItem) {
		this.selectedItem = selectedItem;
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}
	
	public Invoicing getInvoicingBeforeEdit()
	{
		return invoicingBeforeEdit;
	}

	public void setInvoicingBeforeEdit(Invoicing invoicingBeforeEdit)
	{
		this.invoicingBeforeEdit = invoicingBeforeEdit;
	}
}
