/*
 * Created on Dec 12, 2003
 * CS 108
 * Section A
 */
package finalproject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author Eric Knibbe
 * Implements a panel where the user enters the tile they want to move, and where 
 * messages are displayed. 
 */
public class EntryPanel extends JPanel {
	/**
	 * The Sliding Tile game.
	 */
	private SlidingTile mySlidingTile;
	/**
	 * The label for the entry field.
	 */
	private JLabel myEntryLabel;
	/**
	 * The label that displays messages for the user.
	 */
	private JLabel myMessageLabel;
	/**
	 * The text field for data entry.
	 */
	private JTextField myEntryField;
	/**
	 * The Move button.
	 */
	private JButton myMoveTileButton;
	/**
	 * The action carried out when Move is clicked.
	 */
	private MoveAction moveAction;
	/**
	 * The game panel.
	 */
	private GamePanel myGamePanel;
	/**
	 * Accessor for the text in the entry text field.
	 * @return String text from entry field
	 */
	public String getEntryText() {
		return myEntryField.getText();
	}
	/**
	 * Sets the text in the message label.
	 * @param text
	 */
	public void setMessage(String text) {
		myMessageLabel.setText(text);
	}
	/**
	 * Sets the text in the entry text label.
	 * @param text
	 */
	public void setEntryText(String text) {
		myEntryField.setText(text);
	}

	/**
	 * Adds the listener to the Move button.
	 * @param listener
	 */
	public void setMoveAction(ActionListener listener) {
		myMoveTileButton.addActionListener(listener);
	}

	/**
	 * Constructor for the entry panel. Initializes the contained components and adds them to the view.
	 * @param slidingTile
	 * @param gamePanel
	 */
	public EntryPanel(SlidingTile slidingTile, GamePanel gamePanel) {
		mySlidingTile = slidingTile;
		myGamePanel = gamePanel;
		initializeComponents();
		addComponents();
	}

	/**
	 * Initializes the four components in the entry panel.
	 */
	private void initializeComponents() {
		myEntryLabel = new JLabel("Enter a tile #: ");
		myEntryField = new JTextField(2);
		myMessageLabel = new JLabel("Slide some tiles...");
		myMoveTileButton = new JButton("Move");
		MoveAction moveAction = new MoveAction(myGamePanel);
		myMoveTileButton.addActionListener(moveAction);
	}

	/**
	 * Adds the compnents to the entry view with a border layout.
	 */
	private void addComponents() {
		setLayout(new BorderLayout());
		add(myMessageLabel, BorderLayout.NORTH);
		add(myEntryLabel, BorderLayout.CENTER);
		add(myMoveTileButton, BorderLayout.SOUTH);
		add(myEntryField, BorderLayout.EAST);
	}

	/**
	 * If the puzzle is solved and this method is called, the components are disabled or set to an
	 * end-game state.
	 */
	public void puzzleIsSolved() {
		myEntryLabel.setText(" ");
		myEntryField.setEnabled(false);
		myMoveTileButton.setEnabled(false);
		myMessageLabel.setText("Nice job");
	}

	/**
	 * @author Eric Knibbe
	 * Class to handle the movement of a tile.
	 */
	public class MoveAction implements ActionListener {
		/**
		 * The game panel.
		 */
		private GamePanel myGP;
		/**
		 * Constructor for MoveAction. Getting myGP allows this class to 
		 * repaint GamePanel when neccessary.
		 * @param gamePanel
		 */
		public MoveAction(GamePanel gamePanel) {
			myGP = gamePanel;
		}

		/**
		 * Method to move tiles. When called, it gets the number from the entry 
		 * field, checks its validity with SlidingTile's selectionIsValid method, and 
		 * if so performs the tile movenent with exchangeTiles. It then repaints the 
		 * game panel and checks if the puzzle is solved yet. 
		 */
		public void actionPerformed(ActionEvent event) {
			int value = 0;
			try {
				value = Integer.parseInt(getEntryText());
			} catch (NumberFormatException e) {
				setMessage("Nice try");
			}
			if (mySlidingTile.selectionIsValid(value)) {
				mySlidingTile.exchangeTiles();
				setMessage(" ");
				myGP.repaint();
			} else {
				setMessage("Nice try");
			}
			setEntryText("");
			if (mySlidingTile.puzzleIsSovled()) {
				puzzleIsSolved();
			}
		}
	}

}
