package com.rjconsultores.ventaboletos.web.utilerias.render;

import org.zkoss.zk.ui.event.DropEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

import com.rjconsultores.ventaboletos.entidad.FormaPago;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

public class RenderEmpresaSicfeFormasPagamento implements ListitemRenderer {

	@Override
	public void render(Listitem lstm, Object selected) throws Exception {

		lstm.addEventListener("onDrop", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {

				Listitem droppedListitem = (Listitem) (((DropEvent) event).getTarget());
				Listitem draggedListitem = (Listitem) ((DropEvent) event).getDragged();

				MyListbox droppedListBox = (MyListbox) droppedListitem.getParent();
				MyListbox draggedListBox = (MyListbox) draggedListitem.getParent();

				if (draggedListitem instanceof Listitem) {

					if (!droppedListBox.equals(draggedListBox)) {
						if (draggedListitem.getIndex() > -1) {
							droppedListBox.addItemNovo(draggedListitem.getIndex(),
									draggedListBox.getListModel().getElementAt(draggedListitem.getIndex()));
							draggedListBox
									.removeItem(draggedListBox.getListModel().getElementAt(draggedListitem.getIndex()));
						}
					}
				} else {
					droppedListBox.insertBefore(draggedListitem, droppedListitem);
				}
			}
		});

		FormaPago entity = (FormaPago) selected;

		Listcell lc = new Listcell(entity.getDescpago());
		lc.setParent(lstm);

		lstm.setDroppable("true");
		lstm.setDraggable("true");
		lstm.setAttribute("data", entity);

	}
}