/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.dialogs.actionsmanager;

import com.dmdirc.actions.Action;
import com.dmdirc.actions.ActionGroup;
import com.dmdirc.interfaces.actions.ActionType;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

/**
 * Action table model.
 */
public class ActionTableModel extends AbstractTableModel {

    /** A version number for this class. */
    private static final long serialVersionUID = 1;
    /** Action list. */
    private List<Action> actions;

    /**
     * Instantiates a new table model.
     */
    public ActionTableModel() {
        this(new ArrayList<Action>());
    }

    /**
     * Instantiates a new table model.
     *
     * @param actions Actions to show
     */
    public ActionTableModel(final List<Action> actions) {
        super();

        if (actions == null) {
            this.actions = new ArrayList<>();
        } else {
            this.actions = actions;
        }
    }

    @Override
    public int getRowCount() {
        synchronized (actions) {
            return actions.size();
        }
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(final int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Name";
            case 1:
                return "Trigger";
            case 2:
                return "Response";
            default:
                throw new IllegalArgumentException("Unknown column: " + columnIndex);
        }
    }

    @Override
    public Class<?> getColumnClass(final int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return ActionType.class;
            case 2:
                return String[].class;
            default:
                throw new IllegalArgumentException("Unknown column: " + columnIndex);
        }
    }

    @Override
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        synchronized (actions) {
            switch (columnIndex) {
                case 0:
                    return actions.get(rowIndex).getName();
                case 1:
                    return actions.get(rowIndex).getTriggers()[0];
                case 2:
                    return actions.get(rowIndex).getResponse();
                default:
                    throw new IllegalArgumentException("Unknown column: " + columnIndex);
            }
        }
    }

    /**
     * Returns the action at the specified row.
     *
     * @param rowIndex Row index
     *
     * @return Action
     */
    public Action getAction(final int rowIndex) {
        synchronized (actions) {
            return actions.get(rowIndex);
        }
    }

    /**
     * Returns the row index of the specified index.
     *
     * @param action Action to get
     *
     * @return Action row index or -1 if not found.
     */
    public int getAction(final Action action) {
        synchronized (actions) {
            return actions.indexOf(action);
        }
    }

    /**
     * Replaces the model data with the specified action group.
     *
     * @param group New Action group
     */
    public void setActionGroup(final ActionGroup group) {
        synchronized (actions) {
            if (group == null) {
                actions = new ArrayList<>();
            } else {
                actions = group.getActions();
            }
            fireTableDataChanged();
        }
    }

    /**
     * Adds an action to the model.
     *
     * @param action Action to add
     */
    public void add(final Action action) {
        synchronized (actions) {
            if (action == null) {
                return;
            }
            actions.add(action);
            fireTableRowsInserted(actions.size() - 1, actions.size() - 1);
        }
    }

    /**
     * Removes an action from the model.
     *
     * @param action Action to remove
     */
    public void remove(final Action action) {
        if (action == null) {
            return;
        }
        remove(actions.indexOf(action));
    }

    /**
     * Removes an action from the model.
     *
     * @param index Index of the action to remove
     */
    public void remove(final int index) {
        synchronized (actions) {
            if (index != -1) {
                actions.remove(index);
                fireTableRowsDeleted(index, index);
            }
        }
    }

    /**
     * Checks if this model contains the specified action.
     *
     * @param action Action to check for
     *
     * @return true if the action exists
     */
    public boolean contains(final Action action) {
        synchronized (actions) {
            return actions.contains(action);
        }
    }

    public int findAction(final String name) {
        int location = -1;
        synchronized (actions) {
            for (Action action : actions) {
                if (action.getName().equals(name)) {
                    location = actions.indexOf(action);
                }
            }
        }
        return location;
    }

}

@@ -1332,16 +1332,45 @@
 ayList;%0A
+import java.util.Collection;%0A
 import j
@@ -1654,16 +1654,22 @@
 private 
+final 
 List%3CAct
@@ -1687,141 +1687,8 @@
 s;%0A%0A
-    /**%0A     * Instantiates a new table model.%0A     */%0A    public ActionTableModel() %7B%0A        this(new ArrayList%3CAction%3E());%0A    %7D%0A%0A
     
@@ -1821,85 +1821,38 @@
 nal 
-List%3CA
+Colle
 ction
-%3E a
+%3CA
 ction
-s) %7B%0A        super();%0A%0A        if (actions == null) %7B%0A    
+%3E actions) %7B%0A
     
@@ -1889,38 +1889,52 @@
 st%3C%3E();%0A        
-%7D else
+if (actions != null)
  %7B%0A            t
@@ -1936,30 +1936,31 @@
     
-this.
 actions
- = 
+.addAll(
 actions
+)
 ;%0A  
@@ -4352,92 +4352,55 @@
     
-if (group == null) %7B%0A                actions = new ArrayList%3C%3E();%0A            %7D else
+actions.clear();%0A            if (group != null)
  %7B%0A 
@@ -4421,19 +4421,24 @@
  actions
- = 
+.addAll(
 group.ge
@@ -4447,16 +4447,17 @@
 ctions()
+)
 ;%0A      

Tidy up ActionTableModel.