/**
 * CS1101 Lab 04 Ex 1: MyArrayListDriver.java
 *
 * <fill in your discussion group here>
 *
 * This is a driver class to test MyArrayList.
 **/

import java.util.*;

public class MyArrayListDriver
{
    public static void main(String[] args)
    {
        MyArrayList list = new MyArrayList(1);
        Scanner stdIn = new Scanner(System.in);
        String[] line = {};
        do
        {
            // We split up the input into several tokens by the spaces
            // The first item of the resultant array would be command 
            // and subsequent items are the parameters.

            line = stdIn.nextLine().split(" ");
            if (line[0].equalsIgnoreCase("new"))
            {
                // If no parameters are specified, we create a new list 
                // of default capacity.
                if (line.length == 1)
                {
                    list = new MyArrayList();
                }
                // Otherwise we create a list based on the capacity specified.
                else
                {
                    int param = Integer.parseInt(line[1]);
                    list = new MyArrayList(param);
                }
            }
            else if (line[0].equalsIgnoreCase("add"))    // Append ONE item
            {
                String item = line[1];

                //Construct the item and append it to the list
                ListItem element = new ListItem(item);
                list.add(element);
            }
            else if (line[0].equalsIgnoreCase("addi"))    //Add ONE item at index
            {
                //Obtain the index which is the first parameter
                int index = Integer.parseInt(line[1]);

                //The item name is the second parameter
                String item = line[2];
                ListItem element = new ListItem(item);
                list.add(index, element);
            }
            else if (line[0].equalsIgnoreCase("addm"))   //Append MANY items
            {
                int numItems = line.length - 1;

                //Construct a temporary MyArrayList with specified capacity and
                //append items into this list one at a time
                MyArrayList temp = new MyArrayList(numItems);
                for (int i = 1; i < line.length; i++)
                {
                    temp.add(new ListItem(line[i]));
                }

                //Append the temporary list into the original list.
                list.addAll(temp);
            }
            else if (line[0].equalsIgnoreCase("addmi"))   //Append MANY items at index
            {
                //Obtain the insertion index
                int index = Integer.parseInt(line[1]);

                int numItems = line.length - 2;
                //Construct a temporary list again
                MyArrayList temp = new MyArrayList(numItems);
                for (int i = 2; i < line.length; i++)
                {
                    temp.add(new ListItem(line[i]));
                }

                //Insert the entire list at the specified index
                list.addAll(index, temp);
            }
            else if (line[0].equalsIgnoreCase("remove"))   //Remove item at index
            {
                int index = Integer.parseInt(line[1]);
                ListItem item = list.remove(index);
                System.out.print(item + " ");
            }
            else if (line[0].equalsIgnoreCase("set"))      //Set item at index
            {
                int index = Integer.parseInt(line[1]);
                ListItem item = list.set(index, new ListItem(line[2]));
                System.out.print(item + " ");
            }
            else if (line[0].equalsIgnoreCase("clear"))    //Clear the list
            {
                list.clear();
            }
            else if (line[0].equalsIgnoreCase("clone"))    //Make a clone of the list
            {
                MyArrayList clone = list.clone();

                //To test your program correctness, we clear the items in the clone
                //This should not affect your list.
                clone.clear();
            }
            else if (line[0].equalsIgnoreCase("contains")) //Check if list contains specified
            {
                boolean trueOrFalse = list.contains(new ListItem(line[1]));
                System.out.print(trueOrFalse + " ");
            }
            else if (line[0].equalsIgnoreCase("indexOf"))    //Returns index of first occurance
            {
                int index = list.indexOf(new ListItem(line[1]));
                System.out.print(index + " ");
            }
            else if (line[0].equalsIgnoreCase("lastIndexOf"))    //Returns index of last occurance
            {
                int index = list.lastIndexOf(new ListItem(line[1]));
                System.out.print(index + " ");
            }
            // Prints the list at every command so that we can check if the 
            // list is correctly implemented.
            // Note that we can print the object directly only because we 
            // had implemented the toString() method in the MyArrayList class!
            System.out.println(list);
        }
        while (!line[0].trim().equalsIgnoreCase("quit"));
    }
}

