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

import java.util.HashMap;
import java.util.List;
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.FiscalImpressora;
import com.rjconsultores.ventaboletos.service.FiscalImpressoraService;
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.RenderFiscalImpressora;

@Controller("busquedaFiscalImpressoraController")
@Scope("prototype")
public class BusquedaFiscalImpressoraController extends MyGenericForwardComposer {

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

	@Autowired
	private transient PagedListWrapper<FiscalImpressora> plwFiscalImpressora;
	
	@Autowired
	private FiscalImpressoraService fiscalImpressoraService;
	
	private List<FiscalImpressora> lsFiscalImpressora;
	private MyListbox fiscalImpressoraList;
	private Paging pagingFiscalImpressora;
	private Textbox txtMarca;
	private Textbox txtModelo;
	private Textbox txtNumserie20;
	private Textbox txtVersionsb;
	private Textbox txtCniee;
	
	public List<FiscalImpressora> getLsFiscalImpressora() {
		return lsFiscalImpressora;
	}

	public void setLsFiscalImpressora(List<FiscalImpressora> lsFiscalImpressora) {
		this.lsFiscalImpressora = lsFiscalImpressora;
	}

	public MyListbox getFiscalImpressoraList() {
		return fiscalImpressoraList;
	}

	public void setFiscalImpressoraList(MyListbox fiscalImpressoraList) {
		this.fiscalImpressoraList = fiscalImpressoraList;
	}

	public Paging getPagingFiscalImpressora() {
		return pagingFiscalImpressora;
	}

	public void setPagingFiscalImpressora(Paging pagingFiscalImpressora) {
		this.pagingFiscalImpressora = pagingFiscalImpressora;
	}

	public Textbox getTxtMarca() {
		return txtMarca;
	}

	public void setTxtMarca(Textbox txtMarca) {
		this.txtMarca = txtMarca;
	}

	public Textbox getTxtModelo() {
		return txtModelo;
	}

	public void setTxtModelo(Textbox txtModelo) {
		this.txtModelo = txtModelo;
	}

	public Textbox getTxtVersionsb() {
		return txtVersionsb;
	}

	public void setTxtVersionsb(Textbox txtVersionsb) {
		this.txtVersionsb = txtVersionsb;
	}

	public Textbox getTxtCniee() {
		return txtCniee;
	}

	public void setTxtCniee(Textbox txtCniee) {
		this.txtCniee = txtCniee;
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
        fiscalImpressoraList.setItemRenderer(new RenderFiscalImpressora());
        fiscalImpressoraList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                FiscalImpressora s = (FiscalImpressora) fiscalImpressoraList.getSelected();
                verFiscalImpressora(s);
            }
        });

        refreshLista();

        txtModelo.focus();
	}
	
	private void verFiscalImpressora(FiscalImpressora fiscalImpressora) {
        if (fiscalImpressora == null) {
            return;
        }

        Map<String,Object> args = new HashMap<String, Object>();
        args.put("fiscalimpressoraId", fiscalImpressora.getFiscalimpressoraId());
        args.put("fiscalImpressoraList", fiscalImpressoraList);

        openWindow("/gui/seguridad/editarFiscalImpressora.zul",
                   Labels.getLabel("busquedaFiscalImpressoraController.window.title"), args, MODAL);
        
    }
	
	private void refreshLista() {
        HibernateSearchObject<FiscalImpressora> fiscalImpressoraBusqueda =
                new HibernateSearchObject<FiscalImpressora>(FiscalImpressora.class,
                pagingFiscalImpressora.getPageSize());

        String marca = txtMarca.getText();
        if (!marca.equals("")) {
            fiscalImpressoraBusqueda.addFilterLike("marca", "%" + marca.trim().concat("%"));
        }

        String modelo = txtModelo.getText();
        if (!modelo.equals("")) {
            fiscalImpressoraBusqueda.addFilterLike("modelo", "%" + modelo.trim().concat("%"));
        }
        
        String numserie20 = txtNumserie20.getText();
        if (!numserie20.equals("")) {
            fiscalImpressoraBusqueda.addFilterLike("numserie20", "%" + numserie20.trim().concat("%"));
        }

        String versionsb = txtVersionsb.getText();
        if (!versionsb.equals("")) {
            fiscalImpressoraBusqueda.addFilterLike("versionsb", "%" + versionsb.trim().concat("%"));
        }

        String cniee = txtCniee.getText();
        if (!cniee.equals("")) {
            fiscalImpressoraBusqueda.addFilterLike("cniee", "%" + cniee.trim().concat("%"));
        }

        fiscalImpressoraBusqueda.addSortAsc("marca");
        fiscalImpressoraBusqueda.addFilterEqual("activo", Boolean.TRUE);
        
        plwFiscalImpressora.init(fiscalImpressoraBusqueda, fiscalImpressoraList, pagingFiscalImpressora);

        if (fiscalImpressoraList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaFiscalImpressoraController.window.title"),
                        Messagebox.OK, Messagebox.INFORMATION);
            } catch (InterruptedException ex) {
            	log.error(ex.getMessage(),ex);
            }
        }
    }
	
    public void onClick$btnPesquisa(Event ev) {
        refreshLista();
    }

	public Textbox getTxtNumserie20() {
		return txtNumserie20;
	}

	public void setTxtNumserie20(Textbox txtNumserie20) {
		this.txtNumserie20 = txtNumserie20;
	}

}
