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

import java.util.HashMap;
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.Paging;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.Pacote;
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.RenderConfiguracionPacote;

@Controller("busquedaConfiguracionPacoteController")
@Scope("prototype")
public class BusquedaConfiguracionPacoteController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private transient PagedListWrapper<Pacote> plwPacote;
	private MyListbox pacoteList;
	private Paging pagingPacote;
	private Textbox txtNombre;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		pacoteList.setItemRenderer(new RenderConfiguracionPacote());
		pacoteList.addEventListener("onDoubleClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				Pacote p = (Pacote) pacoteList.getSelected();
				selecionarPacote(p);
			}
		});

		refreshLista();

		txtNombre.focus();
	}

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

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

	public void onClick$btnNovo(Event ev) {
		selecionarPacote(new Pacote());
	}

	private void refreshLista() {
		HibernateSearchObject<Pacote> pacoteBusqueda =
				new HibernateSearchObject<Pacote>(Pacote.class, pagingPacote.getPageSize());

		pacoteBusqueda.addFilterLike("nompacote", "%" + txtNombre.getText().trim().concat("%"));
		pacoteBusqueda.addSortAsc("nompacote");

		plwPacote.init(pacoteBusqueda, pacoteList, pagingPacote);

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

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void selecionarPacote(Pacote p) {
		if (p == null) {
			return;
		}

		Map args = new HashMap();
		args.put("pacote", p);
		args.put("pacoteList", pacoteList);

		openWindow("/gui/configuraciones_comerciales/editarConfiguracionPacote.zul",
				Labels.getLabel("editarConfiguracionPacoteController.window.title"), args, MODAL);
	}

	public PagedListWrapper<Pacote> getPlwPacote() {
		return plwPacote;
	}

	public void setPlwPacote(PagedListWrapper<Pacote> plwPacote) {
		this.plwPacote = plwPacote;
	}

	public MyListbox getPacoteList() {
		return pacoteList;
	}

	public void setPacoteList(MyListbox pacoteList) {
		this.pacoteList = pacoteList;
	}

	public Paging getPagingPacote() {
		return pagingPacote;
	}

	public void setPagingPacote(Paging pagingPacote) {
		this.pagingPacote = pagingPacote;
	}

}
