package domain;

import java.util.ArrayList;

public class Hand {

    private ArrayList<Dice> dices = new ArrayList<>(5);
    private ArrayList<Dice> lockedInDices;
    private int leftRolls = 2;
    private String savedGamemode;

    public Hand(String gamemode) {

        this.savedGamemode = gamemode;

        for (int i = 0; i < dices.size(); i++) {

            dices.add(i, new Dice(gamemode));

        }
    }

    public void lockInDice(int dice) {

        for (int i = 0; i < this.dices.size(); i++) {

            if (this.dices.get(i).getDiceNumber() == dice) {
                this.dices.remove(i);
                this.lockedInDices.add(new Dice(savedGamemode, dice));
            }

        }

    }

    public void lockInDices(int... dices) {

        for (int i = 0; i < dices.length; i++) {
            lockInDice(dices[i]);
        }
    }

    public void rollDices() {

        if (this.leftRolls > 0)
            for (int i = 0; i < this.dices.size(); i++) {
                this.dices.get(i).rerollDice();
            }

        leftRolls--;
    }

    public ArrayList<Integer> getAllDiceNumbers() {
        ArrayList<Integer> res = new ArrayList<>();

        for (int i = 0; i < this.dices.size(); i++) {

            res.add(this.dices.get(i).getDiceNumber());

        }
        for (int i = 0; i < this.lockedInDices.size(); i++) {

            res.add(this.lockedInDices.get(i).getDiceNumber());

        }
        return res;

    }

    public ArrayList<Integer> getFreeDiceNumbers() {
        ArrayList<Integer> res = new ArrayList<>();

        for (int i = 0; i < this.dices.size(); i++) {

            res.add(this.dices.get(i).getDiceNumber());

        }

        return res;

    }

    public ArrayList<Integer> getLockedInDiceNumbers() {
        ArrayList<Integer> res = new ArrayList<>();

        for (int i = 0; i < this.lockedInDices.size(); i++) {

            res.add(this.lockedInDices.get(i).getDiceNumber());

        }
        return res;

    }

    public int getLeftRolls() {

        return this.leftRolls;
    }
}
