package info.bukova.isspst.ui;

import info.bukova.isspst.Constants;
import info.bukova.isspst.StringUtils;

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

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.KeyEvent;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Messagebox;

public class DocumentViewModel
{
	protected BigDecimalConverter standardBigDecimalConverter;

	protected BoolConverter standardBoolConverter;

	public BigDecimalConverter getStandardBigDecimalConverter()
	{
		return standardBigDecimalConverter;
	}

	public void setStandardBigDecimalConverter(BigDecimalConverter standardBigDecimalConverter)
	{
		this.standardBigDecimalConverter = standardBigDecimalConverter;
	}

	public BoolConverter getStandardBoolConverter()
	{
		return standardBoolConverter;
	}

	public void setStandardBoolConverter(BoolConverter standardBoolConverter)
	{
		this.standardBoolConverter = standardBoolConverter;
	}

	final public int getLengthText()
	{
		return Constants.LEN_TEXT;
	}

	final public int getLengthDescription()
	{
		return Constants.LEN_DESCRIPTION;
	}

	@Init
	public void initDocumentViewModel()
	{
		this.standardBigDecimalConverter = new BigDecimalConverter();
		this.standardBoolConverter = new BoolConverter();
	}

	@Command
	public void handleComboKeyFilter(@BindingParam("ctrl") Combobox cb, @BindingParam("keyEvent") KeyEvent keyEvent)
	{
		if (cb == null)
		{
			return;
		}

		int keyCode = keyEvent.getKeyCode();

		switch (keyCode)
		{
		case 46: // del
			// Zrušit jakoukoli vybranou položku
			cb.setSelectedIndex(-1);
			// Zavřít listbox
			cb.close();
			// Vyvolat událost změny
			Events.postEvent("onChange", cb, null);
			break;
		}
	}

	@Command
	public void handleComboKey(@BindingParam("ctrl") Combobox cb, @BindingParam("keyEvent") KeyEvent keyEvent)
	{
		if (cb == null)
		{
			return;
		}

		int keyCode = keyEvent.getKeyCode();
		boolean isShiftKey = keyEvent.isShiftKey();

		switch (keyCode)
		{
		case 46: // del

			if (isShiftKey)
			{
				// Zrušit jakoukoli vybranou položku
				cb.setSelectedIndex(-1);
				// Zavřít listbox
				cb.close();
				// Vyvolat událost změny
				Events.postEvent("onChange", cb, null);
			}
			break;
		}
	}

	@Command
	public void testPressKey(@BindingParam("keyEvent") KeyEvent keyEvent)
	{
		int keyCode = keyEvent.getKeyCode();
		boolean isCtrlKey = keyEvent.isCtrlKey();
		boolean isAltKey = keyEvent.isAltKey();
		boolean isShiftKey = keyEvent.isShiftKey();

		List<String> list = new ArrayList<String>();
		list.add(isCtrlKey ? "Ctrl" : "");
		list.add(isAltKey ? "Alt" : "");
		list.add(isShiftKey ? "Shift" : "");
		list.add(Integer.toString(keyCode));
		String sMsg = StringUtils.joinNotEmpty(list, "+");
		
		Messagebox.show(sMsg + " is pressed", "testPressKey", Messagebox.OK, Messagebox.EXCLAMATION);
	}
}
