
package com.rjconsultores.ventaboletos.web.utilerias;

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

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zkplus.databind.BindingListModel;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Combobox;

import com.rjconsultores.ventaboletos.entidad.ImpresionLayoutConfig;
import com.rjconsultores.ventaboletos.service.ImpresionLayoutConfigService;

public class MyComboboxImpresionLayoutConfig extends Combobox {

	private ImpresionLayoutConfigService impresionLayoutConfigService;
	private List<ImpresionLayoutConfig> lsImpresionLayoutConfig;
	private ImpresionLayoutConfig initialValue;
	private Integer indiceSelected = null;

	public MyComboboxImpresionLayoutConfig() {
		super();

		impresionLayoutConfigService = (ImpresionLayoutConfigService) SpringUtil
				.getBean("impresionLayoutConfigService");
		lsImpresionLayoutConfig = new ArrayList<ImpresionLayoutConfig>();

		this.setAutodrop(true);
		this.setAutocomplete(true);

		this.addEventListener("onOK", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				String strImpresionLayoutConfig = MyComboboxImpresionLayoutConfig.this.getText().toUpperCase();
				if (strImpresionLayoutConfig.length() < MyComboboxParada.minLength) {
					return;
				}
				if (!strImpresionLayoutConfig.isEmpty()) {
					lsImpresionLayoutConfig = impresionLayoutConfigService.buscarLike(strImpresionLayoutConfig);

					BindingListModel listModelImpresionLayoutConfig = new BindingListModelList(lsImpresionLayoutConfig,
							true);
					MyComboboxImpresionLayoutConfig.this.setModel(listModelImpresionLayoutConfig);
					indiceSelected = null;
					if (!lsImpresionLayoutConfig.isEmpty()) {
						indiceSelected = 0;
					}
					MyComboboxImpresionLayoutConfig.this.open();
				} else {
					lsImpresionLayoutConfig.clear();

					BindingListModel listModelImpresionLayoutConfig = new BindingListModelList(lsImpresionLayoutConfig,
							true);
					MyComboboxImpresionLayoutConfig.this.setModel(listModelImpresionLayoutConfig);
				}
			}
		});

		this.addEventListener("onChanging", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				InputEvent ev = (InputEvent) event;
				String strImpresionLayoutConfig = ev.getValue();
				if (strImpresionLayoutConfig.length() < 2) {
					lsImpresionLayoutConfig.clear();

					BindingListModel listModelImpresionLayoutConfig = new BindingListModelList(lsImpresionLayoutConfig,
							true);
					MyComboboxImpresionLayoutConfig.this.setModel(listModelImpresionLayoutConfig);

					MyComboboxImpresionLayoutConfig.this.close();
				}
			}
		});
	}

	public ImpresionLayoutConfig getInitialValue() {
		return initialValue;
	}

	public void setInitialValue(ImpresionLayoutConfig initialValue) {
		if (initialValue == null) {
			return;
		}
		List<ImpresionLayoutConfig> ls = new ArrayList<ImpresionLayoutConfig>();
		ls.add(initialValue);

		this.setModel(new BindingListModelList(ls, false));
	}

	public void getValue(boolean checaBusqueda) throws WrongValueException {
		if (checaBusqueda) {
			ImpresionLayoutConfig impresionLayoutConfig = (ImpresionLayoutConfig) (this.getSelectedItem() == null ? null
					: this.getSelectedItem().getValue());

			if (impresionLayoutConfig == null) {
				throw new WrongValueException(this, Labels.getLabel("MSG.Error.combobox.hacerBusqueda"));
			}
		} else {
			super.getValue();
		}
	}

	public <T> T getSelecteObject(Class<T> cType) {
		if (this.getSelectedItem() != null) {
			return cType.cast(this.getSelectedItem().getValue());
		}
		return null;
	}

	public Integer getIndiceSelected() {
		return indiceSelected;
	}

	public void setIndiceSelected(Integer indiceSelected) {
		this.indiceSelected = indiceSelected;
	}

}
