/**
 * ShuffleBoardFrame.java
 *
 * Caitlin Ross
 * 100735219
 */

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//import java.util.Timer.*;

public class ShuffleBoardFrame extends JFrame{
	
	enum Speeds {FAST, REGULAR, SLOW};
	
	private Color currentColor1;
	private Color currentColor2;
	private Speeds currentSpeed;
	private ShuffleBoardMenu aMenu;
	private ShuffleBoardPanel boardPanel;
	private CloseUpPanel closePanel;
	private Graphics g;// = this.getGraphics();
	private Weight player1[];
	private Weight player2[];
	private int numOfWeights = 4;
	
	//Timer used to draw motion
    /*private int milliseconds = 2; //time between timer events
    private Timer timer = new Timer(milliseconds, this); */
	
    public ShuffleBoardFrame() {
    	super("Shuffleboard: Caitlin Ross");
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	
    	aMenu = new ShuffleBoardMenu(this);
    	setJMenuBar(aMenu);
    	//setDefaults();
    	
    	boardPanel = new ShuffleBoardPanel(this);
    	closePanel = new CloseUpPanel();
    	setLayout(new FlowLayout());
    	add(boardPanel);
    	add(closePanel);
    	//setContentPane(aPanel);
    	//setResizable(false);
    	
    	startGame();
    	resetGame();
    	
    	pack();
    }
    
    private void startGame(){
    	setDefaults();
    	resetGame();
    }
    
    public void resetGame(){
    	player1 = new Weight[numOfWeights];
    	player2 = new Weight[numOfWeights];
    	for(int i=0; i<numOfWeights; i++){
    		int y = boardPanel.getHeight() - 20 - 40*i;
    		int x1 = 30;
    		int x2 = boardPanel.getWidth() - 30;
    		player1[i] = new Weight(new Point(x1, y), currentColor1);
    		player2[i] = new Weight(new Point(x2, y), currentColor2);
    	}
    	boardPanel.reset(boardPanel.getGraphics(), player1, player2);
    	closePanel.reset(closePanel.getGraphics());
    }
    
    public void exitGame(){
    	boardPanel.endTimer();
    	dispose();
    }
    
    public Color setColor(int colorNum){
    	switch (colorNum){
    		case 0:		return Color.red;
    		case 1:		return Color.orange;
    		case 2:		return Color.yellow;
    		case 3:		return Color.green;
    		case 4:		return Color.blue;
    		case 5:		return Color.cyan;
    		case 6:		return Color.magenta;
    		case 7:		return Color.pink;
    		case 8:		return Color.white;
    		case 9:		return Color.black;
    		default:	return Color.lightGray;
    	}
    }
    
    public void updateColors(ActionEvent e){
    	for(int i=0; i<10; i++){
    		if(e.getSource() == aMenu.getColors1(i)){
    			currentColor1 = setColor(i);
    			for(int j=0; j<10; j++){
    				aMenu.getColors2(j).setEnabled(true);
    			}
    			aMenu.getColors2(i).setEnabled(false);
    		}
    		if(e.getSource() == aMenu.getColors2(i)){
    			currentColor2 = setColor(i);
    			for(int j=0; j<10; j++){
    				aMenu.getColors1(j).setEnabled(true);
    			}
    			aMenu.getColors1(i).setEnabled(false);
    		}
    	}
    	/*System.out.println("Player 1: " + currentColor1);
    	System.out.println("Player 2: " + currentColor2 + "\n");*/
    	boardPanel.setColors(currentColor1, currentColor2);
    	redrawPanels();
    }
    
    private void setDefaults(){
    	currentColor1 = Color.red;
    	currentColor2 = Color.yellow;
    	currentSpeed = Speeds.REGULAR;
    	aMenu.setDefaults();
    	boardPanel.setColors(currentColor1, currentColor2);
    }
    
    private void redrawPanels(){
    	boardPanel.repaint();
    	closePanel.repaint();
    }
    
    public void updateSpeed(int aSpeed){
    	boardPanel.setSpeed(aSpeed);
    }
    
}