package domain;

import java.util.ArrayList;
import java.util.Arrays;

public class Box {

    private int categoryOne;
    private int categoryTwo;
    private int categoryThree;
    private int categoryFour;
    private int categoryFive;
    private int categorySix;
    private int categorySeven;
    private int categoryEight;

    private Integer setCategoryOne;
    private Integer setCategoryTwo;
    private Integer setCategoryThree;
    private Integer setCategoryFour;
    private Integer setCategoryFive;
    private Integer setCategorySix;
    private Integer setCategorySeven;
    private Integer setCategoryEight;

    private int categoryTripleMatch;
    private int categoryFourOfAKind;
    private int categoryFullHouse;
    private int categorySmallStreet;
    private int categoryBigStreet;
    private int categoryYahtzee;
    private int categoryChance;
    private int categoryR2D2;

    private Integer setCategoryTripleMatch;
    private Integer setCategoryFourOfAKind;
    private Integer setCategoryFullHouse;
    private Integer setCategorySmallStreet;
    private Integer setCategoryBigStreet;
    private Integer setCategoryYahtzee;
    private Integer setCategoryChance;
    private Integer setCategoryR2D2;

    private int counter[] = new int[8];

    public void evaluateUpperBoxOptions(ArrayList<Integer> diceNumbers) {

        if (diceNumbers.contains(1))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 1)
                    this.categoryOne += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(2))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 2)
                    this.categoryTwo += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(3))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 3)
                    this.categoryThree += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(4))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 4)
                    this.categoryFour += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(5))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 5)
                    this.categoryFive += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(6))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 6)
                    this.categorySix += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(7))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 7)
                    this.categorySeven += diceNumbers.get(i);
            }
        else if (diceNumbers.contains(8))
            for (int i = 0; i < diceNumbers.size(); i++) {
                if (diceNumbers.get(i) == 8)
                    this.categoryEight += diceNumbers.get(i);
            }
    }

    public void evaluateLowerBoxOptions(ArrayList<Integer> diceNumbers) {
        int sumOfAllNumbers = 0;

        for (int i = 0; i < diceNumbers.size(); i++) {
            if (diceNumbers.get(i) == 1)
                counter[0]++;
            else if (diceNumbers.get(i) == 2)
                counter[1]++;
            else if (diceNumbers.get(i) == 3)
                counter[2]++;
            else if (diceNumbers.get(i) == 4)
                counter[3]++;
            else if (diceNumbers.get(i) == 5)
                counter[4]++;
            else if (diceNumbers.get(i) == 6)
                counter[5]++;
            else if (diceNumbers.get(i) == 7)
                counter[6]++;
            else if (diceNumbers.get(i) == 8)
                counter[7]++;
        }

        for (int i = 0; i < diceNumbers.size(); i++) {
            sumOfAllNumbers += diceNumbers.get(i);
        }

        categoryChance = sumOfAllNumbers;

        for (int i = 0; i < counter.length; i++) {
            if (counter[i] >= 3)
                categoryTripleMatch = sumOfAllNumbers;
            if (counter[i] >= 4)
                categoryFourOfAKind = sumOfAllNumbers;
            if (counter[i] == 5) {
                categoryYahtzee = 50;
            }
            for (int s = 0; s < counter.length; s++) {
                if (counter[i] == 3 && counter[s] == 2)
                    categoryFullHouse = 25;
            }

        }

        for (int i = 0; i < 6; i++) {

            if (counter[i] == 1 && counter[i + 1] == 1 && counter[i + 2] == 1)
                categorySmallStreet = 30;

        }

        for (int i = 0; i < 5; i++) {

            if (counter[i] == 1 && counter[i + 1] == 1 && counter[i + 2] == 1 && counter[i + 3] == 1)
                categoryBigStreet = 40;

        }

        if (counter[0] == 1 && counter[1] == 2 && counter[3] == 1 && counter[7] == 1)
            categoryR2D2 = 42;

    }

    public void evaluateBoxOptions(ArrayList<Integer> diceNumbers) {

        evaluateUpperBoxOptions(diceNumbers);
        evaluateLowerBoxOptions(diceNumbers);

    }

    public ArrayList<String> giveAllOptions() {
        ArrayList<String> res = new ArrayList<>();
        if (categoryOne > 0 && (setCategoryOne == null))
            res.add("categoryOne: " + categoryOne);
        if (categoryTwo > 0 && (setCategoryTwo == null))
            res.add("categoryTwo: " + categoryTwo);
        if (categoryThree > 0 && (setCategoryThree == null))
            res.add("categoryThree: " + categoryThree);
        if (categoryFour > 0 && (setCategoryFour == null))
            res.add("categoryFour: " + categoryFour);
        if (categoryFive > 0 && (setCategoryFive == null))
            res.add("categoryFive: " + categoryFive);
        if (categorySix > 0 && (setCategorySix == null))
            res.add("categorySix: " + categorySix);
        if (categorySeven > 0 && (setCategorySeven == null))
            res.add("categorySeven: " + categorySeven);
        if (categoryEight > 0 && (setCategoryEight == null))
            res.add("categoryEight: " + categoryEight);
        if (categoryTripleMatch > 0 && (setCategoryTripleMatch == null))
            res.add("categoryTrippleMatch: " + categoryTripleMatch);
        if (categoryFourOfAKind > 0 && (setCategoryFourOfAKind == null))
            res.add("categoryFourOfAKind: " + categoryFourOfAKind);
        if (categoryFullHouse > 0 && (setCategoryFullHouse == null))
            res.add("categoryFullHouse: " + categoryFullHouse);
        if (categorySmallStreet > 0 && (setCategorySmallStreet == null))
            res.add("categorySmallStreet: " + categorySmallStreet);
        if (categoryBigStreet > 0 && (setCategoryBigStreet == null))
            res.add("categoryBigStreet: " + categoryBigStreet);
        if (categoryYahtzee > 0)
            res.add("categoryYahtzee: " + categoryYahtzee);
        if (categoryChance > 0 && (setCategoryChance == null))
            res.add("categoryChance: " + categoryChance);
        if (categoryR2D2 > 0 && (setCategoryR2D2 == null))
            res.add("categoryR2D2: " + categoryR2D2);

        if (res.isEmpty())
            res.add("No options left");

        return res;

    }

    public void selectOption(String option) {

        if (option.equals("categoryOne"))
            setCategoryOne = categoryOne;
        else if (option.equals("categoryTwo"))
            setCategoryTwo = categoryTwo;
        else if (option.equals("categoryThree"))
            setCategoryThree = categoryThree;
        else if (option.equals("categoryFour"))
            setCategoryFour = categoryFour;
        else if (option.equals("categoryFive"))
            setCategoryFive = categoryFive;
        else if (option.equals("categorySix"))
            setCategorySix = categorySix;
        else if (option.equals("categorySeven"))
            setCategorySeven = categorySeven;
        else if (option.equals("categoryEight"))
            setCategoryEight = categoryEight;
        else if (option.equals("categoryTripleMatch"))
            setCategoryTripleMatch = categoryTripleMatch;
        else if (option.equals("categoryFourOfAKind"))
            setCategoryFourOfAKind = categoryFourOfAKind;
        else if (option.equals("categoryFullHouse"))
            setCategoryFullHouse = categoryFullHouse;
        else if (option.equals("categorySmallStreet"))
            setCategorySmallStreet = categorySmallStreet;
        else if (option.equals("categoryBigStreet"))
            setCategoryBigStreet = categoryBigStreet;
        else if (option.equals("categoryYahtzee")) {
            if (setCategoryYahtzee > 0) {
                setCategoryYahtzee += 50;
                if (counter[0] == 5)
                    setCategoryOne = 5;
                else if (counter[1] == 5)
                    setCategoryTwo = 10;
                else if (counter[2] == 5)
                    setCategoryThree = 15;
                else if (counter[3] == 5)
                    setCategoryFour = 20;
                else if (counter[4] == 5)
                    setCategoryFive = 25;
                else if (counter[5] == 5)
                    setCategorySix = 30;
                else if (counter[6] == 5)
                    setCategorySeven = 35;
                else if (counter[7] == 5)
                    setCategoryEight = 40;
            } else
                setCategoryYahtzee = categoryYahtzee;

        } else if (option.equals("categoryChance"))
            setCategoryChance = categoryChance;
        else if (option.equals("categoryR2D2"))
            setCategoryR2D2 = categoryR2D2;

    }

    public void resetOptions() {

        categoryOne = categoryTwo = categoryThree = categoryFour = categoryFive = categorySix = categorySeven = categoryEight = categoryTripleMatch = categoryFourOfAKind = categoryFullHouse = categorySmallStreet = categoryBigStreet = categoryYahtzee = categoryChance = categoryR2D2 = 0;

        Arrays.fill(counter, 0);

    }

    public int returnTotalPoints() {

        return setCategoryOne + setCategoryTwo + setCategoryThree + setCategoryFour + setCategoryFive
                + setCategorySix + setCategorySeven + setCategoryEight + setCategoryTripleMatch + setCategoryFourOfAKind
                + setCategoryFullHouse + setCategorySmallStreet + setCategoryBigStreet + setCategoryYahtzee
                + setCategoryChance
                + setCategoryR2D2;

    }

}
