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

import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.service.ParadaService;
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;

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

    private static Logger log = Logger.getLogger(MyComboboxParadaCve.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 MyComboboxParadaCve() {
        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 = MyComboboxParadaCve.this.getText().toUpperCase();
                if (strParada.length() < MyComboboxParadaCve.minLength) {
                    return;
                }
                if (!strParada.isEmpty()) {
                    lsParadas = paradaService.buscarCVE(strParada);

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

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

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

                    BindingListModel listModelParada = new BindingListModelList(lsParadas, true);
                    MyComboboxParadaCve.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);
                    MyComboboxParadaCve.this.setModel(listModelParada);

                    MyComboboxParadaCve.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.getCveparada());	
    }
    
}
