package com.rjconsultores.ventaboletos.web.gui.controladores.esquemaoperacional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.resource.Labels;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Paging;

import com.rjconsultores.ventaboletos.entidad.AliasServico;
import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.service.RutaService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.paginacion.HibernateSearchObject;
import com.rjconsultores.ventaboletos.web.utilerias.paginacion.PagedListWrapper;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderAliasServico;

@Controller("busquedaAliasServicoController")
@Scope("prototype")
public class BusquedaAliasServicoController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient PagedListWrapper<AliasServico> plwAliasServico;
	@Autowired
	private RutaService rutaService;
	private List<Ruta> lsRutas;
	private MyListbox aliasServicoList;
	private Paging pagingAliasServico;
	private Combobox cmbOrigen;
	private Combobox cmbDestino;
	private Combobox cmbRuta;
	private Intbox txtCorrida;

	public List<Ruta> getLsRutas() {
		return lsRutas;
	}

	public void setLsRutas(List<Ruta> lsRutas) {
		this.lsRutas = lsRutas;
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsRutas = rutaService.obtenerTodos();
		super.doAfterCompose(comp);

		aliasServicoList.setItemRenderer(new RenderAliasServico());
		aliasServicoList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				AliasServico as = (AliasServico) aliasServicoList.getSelected();
				verAliasServico(as);
			}
		});

		refreshLista();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verAliasServico(AliasServico as) {
		if (as == null) {
			return;
		}

		Map args = new HashMap();
		args.put("aliasServico", as);
		args.put("aliasServicoList", aliasServicoList);

		openWindow("/gui/esquema_operacional/editarAliasServico.zul",
				Labels.getLabel("editarAliasServicoController.window.title"), args, MODAL);
	}

	private void refreshLista() {
		HibernateSearchObject<AliasServico> aliasServicoBusqueda =
				new HibernateSearchObject<AliasServico>(AliasServico.class, pagingAliasServico.getPageSize());

		aliasServicoBusqueda.addFilterEqual("activo", true);

		Comboitem cbiOrigem = cmbOrigen.getSelectedItem();
		if (cbiOrigem != null) {
			Parada origen = (Parada) cbiOrigem.getValue();
			aliasServicoBusqueda.addFilterEqual("origen", origen);
		}

		Comboitem cbiDestino = cmbDestino.getSelectedItem();
		if (cbiDestino != null) {
			Parada destino = (Parada) cbiDestino.getValue();
			aliasServicoBusqueda.addFilterEqual("destino", destino);
		}

		Comboitem cbiRuta = cmbRuta.getSelectedItem();
		if (cbiRuta != null) {
			Ruta ruta = (Ruta) cbiRuta.getValue();
			aliasServicoBusqueda.addFilterEqual("ruta", ruta);
		}

		Integer corridaId = txtCorrida.getValue();
		if (corridaId != null) {
			aliasServicoBusqueda.addFilterEqual("corridaId", corridaId);
		}

		aliasServicoBusqueda.addSortAsc("aliasServicoId");

		plwAliasServico.init(aliasServicoBusqueda, aliasServicoList, pagingAliasServico);

		if (aliasServicoList.getData().length == 0) {
			try {
				Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
						Labels.getLabel("busquedaAutobusController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
			} catch (InterruptedException ex) {
			}
		}
	}

	public void onClick$btnPesquisa(Event ev) throws InterruptedException {
		refreshLista();
	}

	public void onClick$btnRefresh(Event ev) {
		refreshLista();
	}

	public void onClick$btnNovo(Event ev) {
		verAliasServico(new AliasServico());
	}
}
