/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.utilerias;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;

/**
 * 
 * @author gleimar
 */
public class MyListbox extends Listbox {
	private static final long serialVersionUID = 1L;
	private Object selected = null;
	private ListModelList modelList = new ListModelList();

	public void setData(List<? extends Object> data) {
		this.modelList.clear();
		this.modelList.addAll(data);

		super.setModel(modelList);
	}

	public MyListbox() {
		super.setModel(modelList);
		setVflex(Boolean.TRUE);
	}
	
	public void clear(){
		this.modelList.clear();
	}

	public boolean addItemNovo(Object item) {
		try {
			this.modelList.add(item);
			super.setModel(modelList);

			return Boolean.TRUE;
		} catch (Exception e) {
			return Boolean.FALSE;
		}
	}
	public boolean addItemNovo(Integer posicao , Object item) {
		try {
			this.modelList.add(posicao,item);
			super.setModel(modelList);
			
			return Boolean.TRUE;
		} catch (Exception e) {
			return Boolean.FALSE;
		}
	}

	/**
	 * Adiciona um item na lista caso ele não esteja presente na lista
	 * 
	 * 
	 * @param item
	 * @return - True se o item foi adicionado
	 * @deprecated - Use o método addItemNovo
	 * 
	 */
	@Deprecated
	public boolean addItem(Object item) {
		boolean ok = false;
		if (!this.modelList.contains(item)) {
			ok = this.modelList.add(item);
		}

		return ok;
	}

	/**
	 * Atualiza se o item está presente na lista. Caso não esteja presente, o item é adicionado.
	 * 
	 * @param item
	 */
	public void updateItem(Object item) {
		if (this.modelList.contains(item)) {
			this.modelList.set(modelList.indexOf(item), item);
		} else {
			this.addItem(item);
		}
	}

	public void removeItem(Object item) {
		this.modelList.remove(item);
	}

	public Object removeSelectedItem() {
		int index = this.getSelectedIndex();

		if (index != -1) {
			return this.modelList.remove(index);
		}

		return null;

	}

	public Object getSelected() {
		int index = this.getSelectedIndex();
		selected = null;
		if (index != -1) {
			selected = this.modelList.get(index);
		}

		return this.selected;
	}

	public List<Object> getSelectedsItens() {
		int index = this.getSelectedIndex();
		int qtd = this.getSelectedCount();
		List<Object> selecteds = new ArrayList<Object>();
		if (index != -1) {
			for (int i = 0; i < qtd; i++) {
				selecteds.add(this.modelList.get(index + i));
			}
		}

		return selecteds;
	}
	
	public List<Object> getItensSelecionados() {
		int qtd = this.getSelectedCount();
		List<Object> selecteds = new ArrayList<Object>();
		for (int i = 0; i < qtd; i++) {
			selecteds.add(modelList.get(((Listitem)getSelectedItems().toArray()[i]).getIndex()));
		}

		return selecteds;
	}
	
	public <E> List<E> getItensSelecionados(Class<E> classe) {
		int qtd = this.getSelectedCount();
		List<E> selecteds = new ArrayList<E>();
		for (int i = 0; i < qtd; i++) {
			Object obj = modelList.get(((Listitem)getSelectedItems().toArray()[i]).getIndex());
			if(classe.isInstance(obj)) {
				selecteds.add(classe.cast(obj));
			}
		}
		return selecteds;
	}
	
	public List<Object> getSelectedItemsByIndex() {
		List<Object> selecteds = new ArrayList<Object>();
		Set<Listitem> selList = this.getSelectedItems();
		for (Listitem sel : selList) {
			selecteds.add(this.modelList.get(sel.getIndex()));
		}
		return selecteds;
	}

	public int getSize() {
		return (modelList == null) ? 0 : modelList.getSize();
	}

	public Object[] getData() {
		return this.modelList.toArray();
	}

	public List getListData() {
		return this.modelList;
	}
	
	public void selectItems(List<? extends Object> data) {
		int idx = 0;
		
		Set<Listitem> selectedItems = new HashSet<Listitem>(data.size());
		
		for (Object modelItem : modelList) {
			for (Object selItem : data) {
				if (selItem.toString().equals(modelItem.toString())) {
					selectedItems.add(this.getItemAtIndex(idx));
					break;
				}
			}
			
			idx++;
		}
		
		this.setSelectedItems(selectedItems);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> getSelectedModelItems() {
		List<Object> list = new ArrayList<Object>(0);
		
		Set<Listitem> selList = this.getSelectedItems();
		
		for (Listitem sel : selList) {
			for (Object modelItem : modelList) {
				if (modelItem.toString().equals(sel.getLabel())) {
					list.add(modelItem);
					break;
				}
			}
		}
		
		return list;
	}
}
