/*
 * 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.Secretaria;
import com.rjconsultores.ventaboletos.service.SecretariaService;

/**
 *
 * @author Lucas
 */
public class MyComboboxSecretaria extends Combobox {

    private SecretariaService secretariaService;
    private List<Secretaria> lsSecretaria;
    private Secretaria initialValue;
    private Integer indiceSelected = null;
    private boolean sinTodos = false;

    public MyComboboxSecretaria() {
        super();

        secretariaService = (SecretariaService) SpringUtil.getBean("secretariaService");
        lsSecretaria = new ArrayList<Secretaria>();

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

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

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

                    BindingListModel listModelSecretaria = new BindingListModelList(lsSecretaria, true);
                    MyComboboxSecretaria.this.setModel(listModelSecretaria);
                    indiceSelected = null;
                    if (!lsSecretaria.isEmpty()) {
                        indiceSelected = 0;
                    }
                    MyComboboxSecretaria.this.open();
                } else {
                	lsSecretaria.clear();

                    BindingListModel listModelPuntoVenta = new BindingListModelList(lsSecretaria, true);
                    MyComboboxSecretaria.this.setModel(listModelPuntoVenta);
                }
            }
        });

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

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

                    BindingListModel listModelSecretaria = new BindingListModelList(lsSecretaria, true);
                    MyComboboxSecretaria.this.setModel(listModelSecretaria);

                    MyComboboxSecretaria.this.close();
                }
            }
        });
    }

    public Secretaria getInitialValue() {
        return initialValue;
    }

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

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

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

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

	public boolean isSinTodos() {
		return sinTodos;
	}

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