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

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.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.CasetaPeaje;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.menu.item.tarifas.constantes.ConstantesGenericas;
import com.rjconsultores.ventaboletos.web.utilerias.paginacion.HibernateSearchObject;
import com.rjconsultores.ventaboletos.web.utilerias.paginacion.PagedListWrapper;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderCasetaPeaje;

@SuppressWarnings("serial")
@Controller("busquedaCasetaPeajeController")
@Scope("prototype")
public class BusquedaCasetaPeajeController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<CasetaPeaje> plwPracaPedagio;
	private Paging pagingPracaPedagio;
	private MyListbox pracaPedagioList;
	private Textbox txtDescPracaPedagio;
	public Paging getPagingPracaPedagio() {
		return pagingPracaPedagio;
	}
	public void setPagingPracaPedagio(Paging pagingPracaPedagio) {
		this.pagingPracaPedagio = pagingPracaPedagio;
	}
	public MyListbox getPracaPedagioList() {
		return pracaPedagioList;
	}
	public void setPracaPedagioList(MyListbox pracaPedagioList) {
		this.pracaPedagioList = pracaPedagioList;
	}	
	
    public Textbox getTxtDescPracaPedagio() {
		return txtDescPracaPedagio;
	}
	public void setTxtDescPracaPedagio(Textbox txtDescPracaPedagio) {
		this.txtDescPracaPedagio = txtDescPracaPedagio;
	}
	public void onClick$btnPesquisa(Event ev) throws InterruptedException {
        refreshLista();
    }

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

    public void onClick$btnNovo(Event ev) {
        verPracaPedagio(new CasetaPeaje());
    }
    
    private void verPracaPedagio(CasetaPeaje casetaPeaje) {
		if(casetaPeaje == null) {
			return;
		}
		
		Map<String, Object> argsOpenWindow = new HashMap<String, Object>();
		argsOpenWindow.put("casetaPeaje", casetaPeaje);
		argsOpenWindow.put("casetaPeajeList", pracaPedagioList);
		
		openWindow("/gui/tarifas/editarPracaPedagio.zul", Labels.getLabel("editarPracaPedagioController.window.title"), argsOpenWindow, MODAL);		
	}
    
	private void refreshLista() {		
		HibernateSearchObject<CasetaPeaje> pesquisaPracaPedagio = new HibernateSearchObject<CasetaPeaje>(CasetaPeaje.class, pagingPracaPedagio.getPageSize());
		pesquisaPracaPedagio.addFilterEqual("activo", Boolean.TRUE);
		pesquisaPracaPedagio.addFilterILike("desccaseta", ConstantesGenericas.OPERADOR_PORCENTO + txtDescPracaPedagio.getValue().trim().concat(ConstantesGenericas.OPERADOR_PORCENTO));
		pesquisaPracaPedagio.addSortAsc("desccaseta");
		plwPracaPedagio.init(pesquisaPracaPedagio, pracaPedagioList, pagingPracaPedagio);
		
		if (pracaPedagioList.getData().length == 0) {
			try {
				Messagebox.show(Labels.getLabel(ConstantesGenericas.MSG_NENHUM_REGISTRO), Labels.getLabel("busquedaPracaPedagioController.window.title"), Messagebox.OK, Messagebox.INFORMATION);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		pracaPedagioList.setItemRenderer(new RenderCasetaPeaje());
		pracaPedagioList.addEventListener(ConstantesGenericas.EVENT_ON_DOUBLE_CLICK, new EventListener() {
			
			@Override
			public void onEvent(Event arg0) throws Exception {
				CasetaPeaje pracaPedagio = (CasetaPeaje) pracaPedagioList.getSelected();
				verPracaPedagio(pracaPedagio);				
			}
		});
		
		refreshLista();
	}
}