package cpsc2150.listDec;

import java.util.List;
import java.util.Random;

/**
 * Generic interface that extends List and provides methods to shuffle items in a list and swap two items in a list.
 *
 * @param <T> Type of Object list will comprise of
 *
 * Initialization ensures:
 *           ShuffleList contains nothing, i.e., an empty string {@code <>}.
 */
public interface IShuffleList<T> extends List<T> {

    int MIN_SIZE = 2;

    /**
     * Randomly picks two positions in the list and swaps them. Repeats this swaps times.
     *
     * @param swaps Number of times to swap
     *
     * @pre
     *      [ #self must be initialized ] AND
     *      MIN_SIZE {@code <=} #self.size() AND
     *      0 {@code <=} swaps
     *
     * @post
     *      self = [ #self randomly shuffled swaps times ]
     */
    default void shuffle(int swaps) {

        // Create a random number generator
        Random rand = new Random();

        // Loop through the number of swaps
        for (int i = 0; i < swaps; i++) {

            // Get random positions based on the size of the list
            int randPos1 = rand.nextInt(size());
            int randPos2 = rand.nextInt(size());

            // Call default method swap with random positions
            swap(randPos1, randPos2);

        }

    }

    /**
     * Exchanges the values at positions i and j in the list.
     *
     * @param i First position to swap
     * @param j Second position to swap
     *
     * @pre
     *      [ #self must be initialized ] AND
     *      MIN_SIZE {@code <=} #self.size() AND
     *      0 {@code <} i {@code <=} #self.size() AND
     *      0 {@code <} j {@code <=} #self.size()
     *
     * @post
     *      self = [ #self with values at positions i and j swapped ]
     */
    default void swap(int i, int j) {

        // Get values at positions passed as parameters
        T pos1 = get(i);
        T pos2 = get(j);

        // Set the positions with the opposite value
        set(i, pos2);
        set(j, pos1);

    }

}
