import java.util.ArrayList;
import java.util.Iterator;
import java.lang.IndexOutOfBoundsException;

/**
 * K_SimpleAL provides a very basic, stripped down subset of the Java
 * ArrayList class.
 *
 * @author Alyce Brady
 * @version Fall 2025
 */
public class K_SimpleAL<T> implements K_SimpleList<T>, Iterable<T>
{
    protected ArrayList<T> theList;

    /** Constructs a simplified K_SimpleAL facade around a fully functional
     * ArrayList.
     */
    public K_SimpleAL()
    {
        theList = new ArrayList<>();
    }

    /** {@inheritDoc}
     */
    @Override
    public int size()
    {
        return theList.size();
    }

    /** {@inheritDoc}
     */
    @Override
    public boolean isEmpty()
    {
        return theList.isEmpty();
    }

    /** {@inheritDoc}
     */
    @Override
    public void add(T newElement)
    {
        theList.add(newElement);
    }

    /** {@inheritDoc}
     */
    @Override
    public T get(int index) throws IndexOutOfBoundsException
    {
        return theList.get(index);
    }

    /** {@inheritDoc}
     */
    @Override
    public int find(T element)
    {
        for ( int i = 0; i < theList.size(); i++ )
        {
            T currentElt = theList.get(i);
            if ( currentElt.equals(element) )
                return i;
        }
        return -1;
    }

    /** {@inheritDoc}
     */
    @Override
    public boolean contains(T element)
    {
        return find(element) != -1;
    }

    /** {@inheritDoc}
     */
    @Override
    public T remove(int index) throws IndexOutOfBoundsException
    {
        return theList.remove(index);
    }

    /** Removes the given element from the list.  More formally, it removes
     *  an element from the list that matches the parameter element using
     *  <code>equals</code>, if such an element exists.
     *    @param element  the element to remove
     *    @return <code>true</code> if the list contained at least one
     *              element that matched the parameter; <code>false</code>
     *              otherwise
     */
    public boolean remove(T element)
    {
        return theList.remove(element);
    }

    /** {@inheritDoc}
     */
    @Override
    public String toString()
    {
        return theList.toString();
    }

    /** Returns an iterator.
     */
    public Iterator<T> iterator()
    {
        return new K_ListIterator<T>(this);
    }

    /** Returns the underlying ArrayList.
     *    @return the contents of this list as an ArrayList (it is the same
     *            list, not a duplicate)
     */
    public ArrayList<T> toArrayList()
    {
        return theList;
    }

    class K_ListIterator<T> implements Iterator<T>
    {
        private K_SimpleList<T> simpleList;
        private int current = 0;

        // Initialize current to be index of first element.
        public K_ListIterator(K_SimpleList<T> list)
        {
            simpleList = list;
            current = 0;
        }

        // returns false if next elemetn does not exist
        public boolean hasNext()
        {
            return current < simpleList.size();
        }

        // return current data and update current
        public T next()
        {
            // Postfix ++ increments current AFTER using to get item to
            // return.
            // T data = simpleList.get(current);
            // current++;
            // return data;
            return simpleList.get(current++);
        }

        // not implemented in this class
        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }

}
