/*
 * 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.Ciudad;
import com.rjconsultores.ventaboletos.entidad.Colonia;
import com.rjconsultores.ventaboletos.service.ColoniaService;

public class MyComboboxColonia extends Combobox {

	private static final long serialVersionUID = 1L;

	private ColoniaService coloniaService;
    private List<Colonia> lsColonia;
    private Ciudad initialValue;
    private Integer indiceSelected = null;

    public MyComboboxColonia() {

        super();

        coloniaService = (ColoniaService) SpringUtil.getBean("coloniaService");
        lsColonia = new ArrayList<Colonia>();

        this.setAutodrop(false);
        this.setAutocomplete(false);
       
        this.addEventListener("onOK", new EventListener() {

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

                if (strColonia.length() < MyComboboxParada.minLength) {
                    return;
                }
                if (!strColonia.isEmpty()) {
                    lsColonia = coloniaService.buscaLike(strColonia);

                    BindingListModel listModelColonia = new BindingListModelList(lsColonia, true);
                    MyComboboxColonia.this.setModel(listModelColonia);
                    indiceSelected = null;
                    if (!lsColonia.isEmpty()) {
                        indiceSelected = 0;
                    }

                    MyComboboxColonia.this.open();
                } else {
                    lsColonia.clear();

                    BindingListModel listModelColonia = new BindingListModelList(lsColonia, true);
                    MyComboboxColonia.this.setModel(listModelColonia);
                }
            }
        });

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

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

                    BindingListModel listModelColonia = new BindingListModelList(lsColonia, true);
                    MyComboboxColonia.this.setModel(listModelColonia);

                    MyComboboxColonia.this.close();
                }
            }
        });
    }

    public Ciudad getInitialValue() {
        return initialValue;
    }

    public void setInitialValue(Colonia initialValue) {
        if (initialValue == null) {
            return;
        }
        List<Colonia> ls = new ArrayList<Colonia>();
        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();
	}
}
