/*
 * 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.duallist;

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 DualList implements Serializable {

    private Map selectItemMap;
    private List leftList;
    private List rightList;
    private Object[] selectedOnLeft = new Object[0];
    private Object[] selectedOnRight = new Object[0];

    public DualList() {
        selectItemMap = new HashMap();
        leftList = new ArrayList();
        rightList = new ArrayList();
    }

    public void setAvailableItems(List list) {
        leftList.clear();
        leftList.addAll(list);

        SelectItem si;
        for (Object aList : list) {
            si = (SelectItem) aList;
            selectItemMap.put(si.getValue().toString(), si);
        }
    }

    public void addAvailableItem(SelectItem si) {
        leftList.add(si);
        selectItemMap.put(si.getValue().toString(), si);
    }

    public void addAvailableItemToRightList(SelectItem si) {
        rightList.add(si);
        selectItemMap.put(si.getValue().toString(), si);
    }

    public void addAll(ActionEvent evt) {
        rightList.addAll(leftList);
        leftList.clear();
    }

    public void removeAll(ActionEvent evt) {
        leftList.addAll(rightList);
        rightList.clear();
    }

    public void add(ActionEvent evt) {
        if (selectedOnLeft != null) {
            SelectItem si;
            for (Object aSelectedOnLeft : selectedOnLeft) {
                si = (SelectItem) selectItemMap.get(aSelectedOnLeft.toString());
                rightList.add(si);
                leftList.remove(si);
            }
        }
    }

    public void remove(ActionEvent evt) {
        if (selectedOnRight != null) {
            SelectItem si;
            for (Object aSelectedOnRight : selectedOnRight) {
                si = (SelectItem) selectItemMap.get(aSelectedOnRight.toString());
                leftList.add(si);
                rightList.remove(si);
            }
        }
    }

    public Object[] getSelectedOnLeft() {
        return selectedOnLeft;
    }

    public void setSelectedOnLeft(Object[] selectedOnLeft) {
        this.selectedOnLeft = selectedOnLeft;
    }

    public Object[] getSelectedOnRight() {
        return selectedOnRight;
    }

    public void setSelectedOnRight(Object[] selectedOnRight) {
        this.selectedOnRight = selectedOnRight;
    }

    public List getLeftList() {
        return leftList;
    }

    public List getRightList() {
        return rightList;
    }
}
