/*
 * @topic T00887 Using Comparable interface Fall 2015
 * @brief DicePair class
 */
package diceroll;

public class DicePair {

    // data fields

    private Die dieOne;
    private Die dieTwo;
    int[] totalStats;
    Stats[] sortedStats;

    // constructors:
    // construct dice with equal number of sides
    public DicePair(int dieSideCount) {
        dieOne = new Die(dieSideCount);
        dieTwo = new Die(dieSideCount);
        totalStats = new int[1 + dieSideCount * 2];
        // create an array of ints having enough space
        // for a range of dice combinations between
        // 2 and dieSideCount*2 (e.g. 2 through 12)
        // plus 1 element which is unused index [0]
        sortedStats = new Stats[totalStats.length - 2];
    }

    // construct dice with unequal number of sides
    public DicePair(int numberOfSides, int numberOfSides2) {
        dieOne = new Die(numberOfSides);
        dieTwo = new Die(numberOfSides2);
    }

    // operations
    public void rollSeries(int numberOfRolls) {
        while (numberOfRolls-- > 0) {
            int total = roll();
            ++totalStats[ total];
        }
    }

    public void sortStats() {
        // populate the array of Stats to sort
        for (int idx = 2; idx < totalStats.length; ++idx) {
            sortedStats[ idx - 2] = new Stats(idx, totalStats[idx]);
        }

        java.util.Arrays.sort(sortedStats);
    }

    public void displayTotalStats() {
        for (int total = 2;
                total <= (dieOne.getSideCount() + dieTwo.getSideCount());
                ++total) {
            System.out.println(
                    total + " rolled " + totalStats[ total] + " times");
        }
    }

    public void resetStats() {
        for (int total = 2;
                total <= (dieOne.getSideCount() + dieTwo.getSideCount());
                ++total) {
            totalStats[ total] = 0;
        }
    }

    // generate new combination
    public int roll() {
        int total = dieOne.roll() + dieTwo.roll();
        return total;
    }

    // obtain reference to a particular die object
    public Die getDieObject(int index) {
        if (index == 1) {
            return dieOne;
        }
        // TODO -- validate that index is 2.
        // If not valid -- report application logic error
        // and throw an exception.

        return dieTwo;
    }

    public void displayStats() {
        // header
        System.out.println(
                "combination\t count\n"
                + "-----------\t -----"
        );
        // details
        for (Stats stat : sortedStats) {
            System.out.println(
                    stat.getCombination()
                    + "\t\t "
                    + stat.getRollCount()
            );
        }
    }

}//class DicePair