package domain;

import java.util.ArrayList;

public class Hand {

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

    public Hand(String gamemode) {

        this.savedGamemode = gamemode;

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

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

        }
    }

    private 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));
                return;
            }

        }

    }

    public void lockInDices(String... dices) {

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

    public boolean autoLockInDices() {

        int mem = this.dices.size();

        if (leftRolls == 0) {

            for (int i = 0; i < mem; i++) {
                lockInDice(dices.get(0).getDiceNumber());
            }
            return true;
        
        }
        return false;
    }

    public int getLockedInDiceAmount() {

        if (lockedInDices != null)
            return lockedInDices.size();
        else
            return 0;
    }

    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 < getLockedInDiceAmount(); i++) {

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

        }
        return res;

    }

    public int getLeftRolls() {

        return this.leftRolls;
    }

}
