/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.component.editablelist;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EditableList implements Serializable {

    private List list;
    private String newItem;
    private String[] selectedItems;
    private boolean enforceUnique;

    public EditableList() {
        list = new ArrayList();
    }

    public EditableList(boolean enforceUnique) {
        this();
        this.enforceUnique = enforceUnique;
    }

    public void addNewItem(ActionEvent evt) {
        if (newItem != null && newItem.length() > 0) {
            if (enforceUnique && getFirstSelectItemWithValue(newItem) != null)
                return;
            SelectItem si = new SelectItem(newItem);
            list.add(si);
            newItem = null;
        }
    }
    
    private SelectItem getFirstSelectItemWithValue(String val){
    	SelectItem target = null;
    	for( Object obj : list ){
    		SelectItem si = (SelectItem)obj;
    		if( si.getValue() != null && si.getValue().equals(val) ){
    			target = si;
    			break;
    		}
    	}
    	return target;
    }

    public void remove(ActionEvent evt) {
        if (selectedItems != null) {
            for (String selectedItem : selectedItems) {
                SelectItem si = getFirstSelectItemWithValue(selectedItem);
                if( si != null ){
                	list.remove(si);
                }
            }
            selectedItems = null;
        }
    }

    public void setList(List newItems) {
        list.clear();
        if (newItems != null) {
            for (Object newItem1 : newItems) {
                String str = (String) newItem1;
                SelectItem si = new SelectItem(str);
                list.add(si);
            }
        }
    }

    public String getNewItem() {
        return newItem;
    }

    public void setNewItem(String newItem) {
        this.newItem = newItem == null ? null : newItem.trim();
    }

    public String[] getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(String[] selectedItems) {
        this.selectedItems = selectedItems;
    }

    public List getList() {
        List strings = new ArrayList();
        for (Object aList : list) {
            strings.add(((SelectItem) aList).getValue());
        }
        return strings;
    }

    public List getListItems() {
        return list;
    }

}
