import java.awt.event.*;

/**
 * A class that represents controllers for the game of Snakes & Ladders
 * @author Derek Bridge 666 d.bridge@cs.ucc.ie
 */
public class SLController
   implements ActionListener
{
/* =======================================================================
       CONSTRUCTORS
   =======================================================================
*/
    /**
     * Allocates a controller for the game.
     * @param theStart the start of the board
     * @param theUser the user player
     * @param theComputer the computer player
     */
    public SLController(Square theStart,
        Player theUser, Player theComputer)
    {   start = theStart;
        user = theUser;
        computer = theComputer;
    }

/* =======================================================================
       PUBLIC INTERFACE
   =======================================================================
*/

    /**
     * Sets the GUI
     * @param theGUI the gui being controlled
     */
    public void setGUI(SLGUI theGUI)
    {   gui = theGUI;
    }

    /**
     * Respond to button clicks
     * @param ae the button click event
     */
    public void actionPerformed(ActionEvent ae)
    {   if (ae.getActionCommand().equals(SLGUI.NEW))
        {   gui.disable();
            user.setSquare(start);
            computer.setSquare(start);
            usersTurn = true;
            gui.showGameStart();
            gui.enable();
        }
        else // DICE
        {   gui.disable();
            if (usersTurn)
            {   int initialSquareNum = user.getSquare().getSquareNumber();
                int roll = user.roll();
                user.moveBy(roll);
                int finalSquareNum = user.getSquare().getSquareNumber();
                boolean userWins = ! user.getSquare().hasNext();
                usersTurn = false;
                gui.showUserOutcome(
                    roll, initialSquareNum, finalSquareNum, userWins);
                if (! userWins)
                {   gui.enable();
                }
            }
            else // computer's turn
            {   int initialSquareNum = computer.getSquare().getSquareNumber();
                int roll = computer.roll();
                computer.moveBy(roll);
                int finalSquareNum = computer.getSquare().getSquareNumber();
                boolean computerWins = ! computer.getSquare().hasNext();
                usersTurn = true;
                gui.showComputerOutcome(
                    roll, initialSquareNum, finalSquareNum, computerWins);
                if (! computerWins)
                {   gui.enable();
                }
            }
        }
    }

/* =======================================================================
       INSTANCE VARIABLES & CLASS VARIABLES
   =======================================================================
*/
    /**
     * The starting square of the board being played on
     */
    private Square start;

    /**
     * The user player
     */
    private Player user;

    /**
     * The computer player
     */
    private Player computer;

    /**
     * Whose turn it is. if true, it's the user's turn; else it's the
     * computer's turn.
     */
    private boolean usersTurn;

    /**
     * The GUI
     */
    private SLGUI gui;
}
