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

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

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Hotel;
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.RenderHotel;

@Controller("busquedaHotelController")
@Scope("prototype")
public class BusquedaHotelController extends MyGenericForwardComposer {

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

	@Autowired
    private transient PagedListWrapper<Hotel> plwHotel;
    private MyListbox hotelList;
    private Paging pagingHotel;
    private Textbox txtNome;

    public Textbox getTxtNome() {
        return txtNome;
    }

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

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

        hotelList.setItemRenderer(new RenderHotel());
        hotelList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Hotel a = (Hotel) hotelList.getSelected();
                verHotel(a);
            }
        });

        refreshLista();

        txtNome.focus();
    }

    private void verHotel(Hotel t) {
        if (t == null) {
            return;
        }

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("hotel", t);
        args.put("hotelList", hotelList);

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

    private void refreshLista() {
        HibernateSearchObject<Hotel> hotelBusqueda = new HibernateSearchObject<Hotel>(Hotel.class, pagingHotel.getPageSize());

        if(StringUtils.isNotBlank(txtNome.getText())) {
        	hotelBusqueda.addFilterLike("deschotel", "%" + txtNome.getText().trim().concat("%"));
        }

        hotelBusqueda.addSortAsc("deschotel");
        hotelBusqueda.addFilterEqual("activo", Boolean.TRUE);
        plwHotel.init(hotelBusqueda, hotelList, pagingHotel);

        if (hotelList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        		Labels.getLabel("busquedaHotelController.window.title"),
                        		Messagebox.OK, Messagebox.INFORMATION);
            } catch (InterruptedException e) {
            	log.error(e.getMessage(), e.getCause());
            }
        }
    }

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

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

    public void onClick$btnNovo(Event ev) {
        verHotel(new Hotel());
    }

	public MyListbox getHotelList() {
		return hotelList;
	}

	public void setHotelList(MyListbox hotelList) {
		this.hotelList = hotelList;
	}

	public Paging getPagingHotel() {
		return pagingHotel;
	}

	public void setPagingHotel(Paging pagingHotel) {
		this.pagingHotel = pagingHotel;
	}
}
