package cards.maumau.model;

public class WaitForMauMauState implements PlayerState {
    private final PlayerHandler handler;

    public WaitForMauMauState(PlayerHandler handler) {
        this.handler = handler;
    }

    @Override
    public void nextTurn(int n) {
        // Draw a card and proceed to next turn
        handler.getRemember().drawCards(1);
        if (handler.getCurrentPlayer().getCards().isEmpty()) {
            handler.setRemember(handler.getCurrentPlayer());
            handler.localNextTurn(n);
            handler.setCurrentState(handler.getWaitForMauMauState());
        } else if (handler.getCurrentPlayer().getCards().size() == 1) {
            handler.setRemember(handler.getCurrentPlayer());
            handler.localNextTurn(n);
            handler.setCurrentState(handler.getWaitForMauState());
        } else {
            handler.localNextTurn(n);
            handler.setCurrentState(handler.getWaitForNextTurnState());
        }
    }

    @Override
    public void mau(Player p) {
        // Do nothing or handle invalid state transition
    }

    @Override
    public void maumau(Player p) {
        if (p == handler.getRemember()) {


            handler.finishPlayer(p);


            if (handler.getPlayers().size() == 1) {
                handler.finishPlayer(handler.getCurrentPlayer());
                handler.finishGame();
                handler.setCurrentState(handler.getFinishedState());
            } else {
                handler.setCurrentState(handler.getWaitForNextTurnState());
            }


        }
    }
}
