package cards.maumau.model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PlayerHandler {
    private final MauMau game;
    private final List<Player> players = new LinkedList<>();
    private final List<Player> ranking = new ArrayList<>();
    private Player remember;

    private PlayerState waitForNextTurnState;
    private PlayerState waitForMauState;
    private PlayerState waitForMauMauState;
    private PlayerState finishedState;

    private PlayerState currentState;

    public PlayerHandler(MauMau game) {
        this.game = game;

        //Initialize all the States to be used
        this.waitForNextTurnState = new WaitForNextTurnState(this);
        this.waitForMauState = new WaitForMauState(this);
        this.waitForMauMauState = new WaitForMauMauState(this);
        this.finishedState = new FinishedState();

        //set waitingForNextTurnState as default
        this.currentState = waitForNextTurnState;
    }

    public void nextTurn(int n) {
        currentState.nextTurn(n);
    }

    public void mau(Player p) {
        currentState.mau(p);
    }

    public void maumau(Player p) {
        currentState.maumau(p);
    }

    public List<Player> getPlayers() {
        return players;
    }

    public List<Player> getRanking() {
        return ranking;
    }

    public void addPlayer(Player player) {
        players.addLast(player);
    }

    public void localNextTurn(int n) {
        for(int i = 0; i < n; i++){
            players.addLast(players.removeFirst());
            // players.removeFirst();
        }
    }

    public void finishPlayer(Player p) {
        ranking.addLast(players.removeLast());
    }

    public Player getCurrentPlayer() {
        return players.isEmpty() ? null : players.get(0);
    }

    public void setRemember(Player player) {
        this.remember = player;
    }

    public Player getRemember() {
        return remember;
    }

    public PlayerState getWaitForNextTurnState() {
        return waitForNextTurnState;
    }

    public PlayerState getWaitForMauState() {
        return waitForMauState;
    }

    public PlayerState getWaitForMauMauState() {
        return waitForMauMauState;
    }

    public PlayerState getFinishedState() {
        return finishedState;
    }

    public void setCurrentState(PlayerState state) {
        this.currentState = state;
    }

    public void finishGame() {
        game.getActionHandler().finishGame();
    }
}
