import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PigSwing extends JPanel implements ActionListener{

	//keep track of the current state of the game.
	private int playerScore;
	private int cpuScore;
	private int turnTotal;

	//UI components we will need access to.
	private JTextField cpuScoreField, playerScoreField, statusField;
	private JButton holdButton, rollButton;
	
	//Constructor
	public PigSwing() {
		//setup the use interface
		setupUI();
		
		//reset the game state
		reset();
	}
	
	public void reset() {
		//reset the game state and UI
		playerScore = 0;
		cpuScore = 0;
		turnTotal = 0;
		cpuScoreField.setText("0");
		playerScoreField.setText("0");
		statusField.setText("");
	}
	
	public void setupUI() {
		//Computer Score panel.
		cpuScoreField = new JTextField(10);
		cpuScoreField.setEditable(false);
		JLabel cpuScoreLabel = new JLabel("Computer Score:");
		JPanel cpuPanel = new JPanel(new FlowLayout());
		cpuPanel.add(cpuScoreLabel);
		cpuPanel.add(cpuScoreField);

		//Play Score panel
		playerScoreField = new JTextField(10);
		playerScoreField.setEditable(false);
		JLabel playerScoreLabel = new JLabel("Player Score:");
		JPanel playerPanel = new JPanel(new FlowLayout());
		playerPanel.add(playerScoreLabel);
		playerPanel.add(playerScoreField);
		
		//Button panel
		JPanel buttonPanel = new JPanel(new FlowLayout());
		holdButton = new JButton("Hold");
		rollButton = new JButton("Roll");
		holdButton.addActionListener(this);
		rollButton.addActionListener(this);
		buttonPanel.add(holdButton);
		buttonPanel.add(rollButton);
		
		//Status field for game updates
		statusField = new JTextField();
		statusField.setEditable(false);
		
		//Put all of the panels together
		setLayout(new GridLayout(4,1));
		add(playerPanel);
		add(cpuPanel);
		add(buttonPanel);
		add(statusField);
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == rollButton) {
			//roll button pushed
			int roll = PigHelper.roll();
			if(roll == 1) {
				//turn is over (rolled 1)
				turnTotal = 0;
				//let the computer play
				computerTurn("You rolled a 1. ");
			}
			else {
				//turn continues
				turnTotal += roll;
				//update the status field.
				String status = String.format("Roll was %d. Turn total is now %d.", roll, turnTotal);
				statusField.setText(status);
			}
		}
		else if(e.getSource() == holdButton) {
			//hold button pushed
			//finish the player's turn
			playerScore += turnTotal;
			int ttOld = turnTotal;
			turnTotal = 0;
			//update the score's UI
			playerScoreField.setText(""+playerScore);
			
			//did we win?
			if(PigHelper.isGameOver(playerScore)) {
				//finish the game
				handleGameOver(true);
			}
			else {
				//let the computer take a turn
				computerTurn("You held at " + ttOld + ".\n ");
			}
		}
		
	}
	
	public void handleGameOver(boolean playerWins) {
		String msg = "The computer won!";
		if(playerWins) {
			msg = "You win!";
		}
		//show a dialog box and keep playing if they answer yes
		if(JOptionPane.showConfirmDialog(null, msg + " Play Again?") == JOptionPane.YES_OPTION) {
			reset();
		}
		else {
			//otherwise end the program
			System.exit(0);
		}
		
	}
	
	
	public void computerTurn(String msg) {
		//get the string from the helper
		String result = PigHelper.cpuTurn(cpuScore, playerScore);
		
		//get the last value from the string, which is the turn total
		String turn = result.substring(result.indexOf('=') + 1);
		int cpuTurn = Integer.parseInt(turn);
		
		//update the score and the UI
		cpuScore += cpuTurn;
		cpuScoreField.setText(""+cpuScore);
		statusField.setText(msg + result);
		
		//check if the computer won
		if(PigHelper.isGameOver(cpuScore)) {
			//finish the game
			handleGameOver(false);
		}
	}
	
	public static void main(String[] args) {
		//set up the window
		JFrame frame = new JFrame("Pig");
		
		frame.add(new PigSwing());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//fit the window around the panels
		frame.pack();
		//show the window
		frame.setVisible(true);
	}



}
