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

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

import org.apache.log4j.Logger;
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.PricingClasseTarifaria;
import com.rjconsultores.ventaboletos.service.PricingClasseTarifariaService;
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.RenderPricingClasseTarifaria;

@Controller("busquedaPricingClasseTarifariaController")
@Scope("prototype")
public class BusquedaPricingClasseTarifariaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(BusquedaPricingClasseTarifariaController.class);

	@Autowired
	private transient PagedListWrapper<PricingClasseTarifaria> plwPricingClasseTarifaria;

	@Autowired
	private PricingClasseTarifariaService pricingClasseTarifariaService;

	private MyListbox pricingClasseTarifariaList;
	private Paging pagingPricing;

	private Textbox desc;
	private Textbox sigla;

	public void onClick$btnApagar(Event ev) {
		try {
			PricingClasseTarifaria p = (PricingClasseTarifaria) pricingClasseTarifariaList.getSelected();
			if (p != null) {
				int resp = Messagebox.show(Labels.getLabel("editarPricingController.MSG.borrarPergunta"),
						Labels.getLabel("busquedaPricingController.window.title"),
						Messagebox.YES | Messagebox.NO,
						Messagebox.QUESTION);

				if (resp == Messagebox.YES) {
					pricingClasseTarifariaService.borrar(p);
					Messagebox.show(Labels.getLabel("editarPricingController.MSG.borrarOK"),
							Labels.getLabel("busquedaPricingController.window.title"),
							Messagebox.OK, Messagebox.INFORMATION);
					pricingClasseTarifariaList.removeItem(p);
				}
			} else {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("busquedaPricingController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error("Erro ao eliminar Pricing Classe Tarifaria: ", ex);
		}
	}

	private void refreshLista() {

		HibernateSearchObject<PricingClasseTarifaria> pricingSearch = new HibernateSearchObject<PricingClasseTarifaria>(
				PricingClasseTarifaria.class, pagingPricing.getPageSize());

		if (!desc.getValue().isEmpty()) {
			pricingSearch.addFilterILike("descClasseTarifaria", desc.getValue() + "%");
		}

		if (!sigla.getValue().isEmpty()) {
			pricingSearch.addFilterILike("sigla", sigla.getValue());
		}

		pricingSearch.addFilterEqual("activo", Boolean.TRUE);
		pricingSearch.addSortAsc("descClasseTarifaria");

		plwPricingClasseTarifaria.init(pricingSearch, pricingClasseTarifariaList, pagingPricing);

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

	public void onClick$btnNovo(Event ev) {
		verPricingClasseTarifaria(new PricingClasseTarifaria());
	}

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

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

	@SuppressWarnings("unchecked")
	private void verPricingClasseTarifaria(PricingClasseTarifaria pricingClasseTarifaria) {
		if (pricingClasseTarifaria == null) {
			return;
		}

		@SuppressWarnings("rawtypes")
		Map args = new HashMap();
		args.put("pricingClasseTarifaria", pricingClasseTarifaria);
		args.put("pricingClasseTarifariaList", pricingClasseTarifariaList);

		openWindow("/gui/pricing/editarPricingClasseTarifaria.zul",
				Labels.getLabel("editarPricingClasseTarifariaController.window.title"), args, MODAL);
	}

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

		super.doAfterCompose(comp);

		pricingClasseTarifariaList.setItemRenderer(new RenderPricingClasseTarifaria());
		pricingClasseTarifariaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingClasseTarifaria pricing = (PricingClasseTarifaria) pricingClasseTarifariaList.getSelected();
				verPricingClasseTarifaria(pricing);
			}
		});

		refreshLista();
	}

}
