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

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.ImpresionLayoutConfig;
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.RenderImpresionLayoutConfig;

@SuppressWarnings("serial")
@Controller("busquedaImpresionLayoutConfigController")
@Scope("prototype")
public class BusquedaImpresionLayoutConfigController extends MyGenericForwardComposer {

	@Autowired
	private transient PagedListWrapper<ImpresionLayoutConfig> plwImpresionLayoutConfig;
	private MyListbox impresionLayoutConfigList;
	private Paging pagingImpresionLayoutConfig;
	private Textbox txtNome;

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

		impresionLayoutConfigList.setItemRenderer(new RenderImpresionLayoutConfig());
		impresionLayoutConfigList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				ImpresionLayoutConfig entity = (ImpresionLayoutConfig) impresionLayoutConfigList.getSelected();
				verImpresionLayoutConfig(entity);
			}
		});

		refreshLista();

		txtNome.focus();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void verImpresionLayoutConfig(ImpresionLayoutConfig entity) {
		if (entity == null) {
			return;
		}

		Map args = new HashMap();
		args.put("impresionLayoutConfig", entity);
		args.put("impresionLayoutConfigList", impresionLayoutConfigList);

		openWindow("/gui/catalogos/editarImpresionLayoutConfig.zul",
				Labels.getLabel("busquedaImpresionLayoutConfigController.window.title"), args, MODAL);
	}

	private void refreshLista() {
		HibernateSearchObject<ImpresionLayoutConfig> impresionLayoutConfigBusqueda = new HibernateSearchObject<ImpresionLayoutConfig>(
				ImpresionLayoutConfig.class, pagingImpresionLayoutConfig.getPageSize());

		impresionLayoutConfigBusqueda.addFilterLike("descricao", "%" + txtNome.getText().trim().concat("%"));

		impresionLayoutConfigBusqueda.addSortAsc("descricao");

		impresionLayoutConfigBusqueda.addFilterEqual("activo", Boolean.TRUE);

		plwImpresionLayoutConfig.init(impresionLayoutConfigBusqueda, impresionLayoutConfigList,
				pagingImpresionLayoutConfig);

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

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

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

	public void onClick$btnNovo(Event ev) {
		verImpresionLayoutConfig(new ImpresionLayoutConfig());
	}

	public PagedListWrapper<ImpresionLayoutConfig> getPlwImpresionLayoutConfig() {
		return plwImpresionLayoutConfig;
	}

	public void setPlwImpresionLayoutConfig(PagedListWrapper<ImpresionLayoutConfig> plwImpresionLayoutConfig) {
		this.plwImpresionLayoutConfig = plwImpresionLayoutConfig;
	}

	public MyListbox getImpresionLayoutConfigList() {
		return impresionLayoutConfigList;
	}

	public void setImpresionLayoutConfigList(MyListbox impresionLayoutConfigList) {
		this.impresionLayoutConfigList = impresionLayoutConfigList;
	}

	public Paging getPagingImpresionLayoutConfig() {
		return pagingImpresionLayoutConfig;
	}

	public void setPagingImpresionLayoutConfig(Paging pagingImpresionLayoutConfig) {
		this.pagingImpresionLayoutConfig = pagingImpresionLayoutConfig;
	}

	public Textbox getTxtNome() {
		return txtNome;
	}

	public void setTxtNome(Textbox txtNome) {
		this.txtNome = txtNome;
	}

}
