/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.utilerias;

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

import org.apache.log4j.Logger;
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.Parada;
import com.rjconsultores.ventaboletos.service.ParadaService;

/**
 * 
 * @author Administrador
 */
public class MyComboboxParada extends Combobox {

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(MyComboboxParada.class);
	public final static int minLength = 2;
	private ParadaService paradaService;
	private List<Parada> lsParadas;
	private Parada initialValue;
	private Integer indiceSelected = null;
	private boolean sinTodos = false;
	private Integer ID_TODOS = -1;

	public MyComboboxParada() {
		super();

		paradaService = (ParadaService) SpringUtil.getBean("paradaService");
		lsParadas = new ArrayList<Parada>();

		this.setAutodrop(false);
		this.setAutocomplete(false);

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

			@Override
			public void onEvent(Event event) throws Exception {
				String strParada = MyComboboxParada.this.getText().toUpperCase();
				if (strParada.length() < MyComboboxParada.minLength) {
					return;
				}
				if (!strParada.isEmpty()) {
					lsParadas = paradaService.buscaLike(strParada);

					if (sinTodos) {
						log.debug("Sin todos");
						Parada p = paradaService.obtenerID(ID_TODOS);
						lsParadas.remove(p);
					}

					BindingListModel listModelParada = new BindingListModelList(lsParadas, true);
					MyComboboxParada.this.setModel(listModelParada);
					setIndiceSelected(null);
					if (!lsParadas.isEmpty()) {
						setIndiceSelected(0);
					}

					MyComboboxParada.this.open();
				} else {
					lsParadas.clear();

					BindingListModel listModelParada = new BindingListModelList(lsParadas, true);
					MyComboboxParada.this.setModel(listModelParada);
				}
			}
		});

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

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

					BindingListModel listModelParada = new BindingListModelList(lsParadas, true);
					MyComboboxParada.this.setModel(listModelParada);

					MyComboboxParada.this.close();
				}
			}
		});
	}

	public Parada getInitialValue() {
		return initialValue;
	}

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

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

	/**
	 * 
	 * @param checaBusqueda
	 * @throws WrongValueException
	 */
	public String getValue(boolean checaBusqueda) throws WrongValueException {
		if (checaBusqueda) {
			if (this.getSelectedItem() == null) {
				throw new WrongValueException(this, Labels.getLabel("MSG.Error.combobox.hacerBusqueda"));
			}
		}

		return super.getValue();
	}

	public boolean isSinTodos() {
		return sinTodos;
	}

	public void setSinTodos(boolean sinTodos) {
		this.sinTodos = sinTodos;
	}

	public void setComboItemByParada(Parada parada) {

		List<Parada> ls = new ArrayList<Parada>();
		ls.add(parada);

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

	public Integer getIndiceSelected() {
		return indiceSelected;
	}

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