/*
 * 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.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.Estacion;
import com.rjconsultores.ventaboletos.entidad.Usuario;
import com.rjconsultores.ventaboletos.service.EstacionService;

/**
 *
 * @author Wilian Domingues
 */
public class MyComboboxEstacion extends Combobox {

	private static final long serialVersionUID = 1L;

	private EstacionService estacionService;
    private List<Estacion> lsEstacion;
    private Usuario initialValue;
    private Integer indiceSelected = null;

    public MyComboboxEstacion() {

        super();

        estacionService = (EstacionService) SpringUtil.getBean("estacionService");
        lsEstacion = new ArrayList<Estacion>();

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

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

            @Override
            public void onEvent(Event event) throws Exception {
                String dscEstacion = MyComboboxEstacion.this.getText().toUpperCase();

                if (dscEstacion.length() < MyComboboxParada.minLength) {
                    return;
                }
                if (!dscEstacion.isEmpty()) {
                    lsEstacion = estacionService.buscar(dscEstacion, null, null, null);
                    lsEstacion.add(new Estacion(-1, "TODAS"));

                    BindingListModel listModelUsuario = new BindingListModelList(lsEstacion, true);
                    MyComboboxEstacion.this.setModel(listModelUsuario);
                    indiceSelected = null;
                    if (!lsEstacion.isEmpty()) {
                        indiceSelected = 0;
                    }

                    MyComboboxEstacion.this.open();
                } else {
                    lsEstacion.clear();

                    BindingListModel listModelEstacion = new BindingListModelList(lsEstacion, true);
                    MyComboboxEstacion.this.setModel(listModelEstacion);
                }
            }
        });

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

            public void onEvent(Event event) throws Exception {
                InputEvent ev = (InputEvent) event;
                String strUsuario = ev.getValue();
                if (strUsuario.length() < 2) {
                    lsEstacion.clear();

                    BindingListModel listModelEstacion = new BindingListModelList(lsEstacion, true);
                    MyComboboxEstacion.this.setModel(listModelEstacion);

                    MyComboboxEstacion.this.close();
                }
            }
        });
    }

    public Usuario getInitialValue() {
        return initialValue;
    }

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

        this.setModel(new BindingListModelList(ls, false));
    }
    
	/**
	 * 
	 * @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();
	}
}
