package com.rjconsultores.ventaboletos.web.utilerias;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.Ruta;
import com.rjconsultores.ventaboletos.service.RutaService;

public class MyComboboxRuta extends Combobox {

	private static final String SEPARADOR = " - ";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final static int minLength = 2;

	@Autowired
	private RutaService rutaService;
	private List<Ruta> rutas;
	private Integer indiceSelected;
	private Ruta initialValue;

	public MyComboboxRuta() {
		super();

		rutas = new ArrayList<Ruta>();

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

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

			@Override
			public void onEvent(Event event) throws Exception {
				rutaService = (RutaService) SpringUtil.getBean("rutaService");
				String stringConsulta = MyComboboxRuta.this.getText().toUpperCase();
				if (stringConsulta.length() < MyComboboxRuta.minLength) {
					return;
				}
				if (!stringConsulta.isEmpty()) {
					rutas = rutaService.buscaLikeComboBox(stringConsulta);

					setIndiceSelected(null);
					BindingListModel listModelRutas = new BindingListModelList(rutas, true);
					MyComboboxRuta.this.setModel(listModelRutas);
					if (!rutas.isEmpty()) {
						setIndiceSelected(0);
					}

					MyComboboxRuta.this.open();
				} else {
					rutas.clear();

					BindingListModel listModelParada = new BindingListModelList(rutas, true);
					MyComboboxRuta.this.setModel(listModelParada);
				}
			}
		});

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

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

					BindingListModel listModelRutas = new BindingListModelList(rutas, true);
					MyComboboxRuta.this.setModel(listModelRutas);

					MyComboboxRuta.this.close();
				}
			}
		});
	}

	public Ruta getInitialValue() {
		return initialValue;
	}

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

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

	private String retornaDescricaoRuta(Ruta ruta) {
		StringBuilder descricao = new StringBuilder();
		
		if(StringUtils.isNotBlank(ruta.getNumRuta())) {
			descricao.append(ruta.getNumRuta());
		}
		if(descricao.length() > 0) {
			descricao.append(SEPARADOR);
		}
		descricao.append(ruta.getDescruta());
		if(ruta.getClaseServicio() != null) {
			descricao.append(SEPARADOR)
					 .append(ruta.getClaseServicio().getDescclase());
		}
		
		return descricao.toString();
	}
	
	public void setComboItemByRuta(Ruta ruta) {

		List<Ruta> ls = new ArrayList<Ruta>();
		ls.add(ruta);

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

	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 Integer getIndiceSelected() {
		return indiceSelected;
	}

	public void setIndiceSelected(Integer indiceSelected) {
		this.indiceSelected = indiceSelected;
	}
	
	public <T> T getSelecteObject(Class<T> cType) {
		if (this.getSelectedItem() != null) {
			return cType.cast(this.getSelectedItem().getValue());
		}
		return null;
	}

}
