/**
 * @(#)ColorChooser.java	11/03/05
 * 
 * Copyright 2005 3GP01, KCL.  All unmodified code is copyright of its respective owner.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * The GNU General Public Licence should be contained within licence.txt;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 * Color chooser for GUI colours - not enabled as still needs some work, but 
 * basic functionality exists.
 * 
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 */

public class ColorChooser extends JPanel
						  implements ActionListener {

	static JFrame frame;
    protected JColorChooser tcc;
    protected JButton baseColorButton;
    protected JButton supplementaryColorButton;
    protected JButton highlightColorButton;
    protected JButton okButton;
    protected JButton resetColorsButton;
    protected JButton cancelButton;

	Color supplementaryColor = Scrabble.supplementaryColor;
	Color highlightColor = Scrabble.highlightColor;

	Border outlineBorder = BorderFactory.createEtchedBorder(Color.WHITE,
			Color.GRAY);

	Border border;
    
    public ColorChooser() 
    {
        super(new BorderLayout());
                
        JPanel colorButtonPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        
        baseColorButton = new JButton("Base Color");
        baseColorButton.addActionListener(this);
        supplementaryColorButton = new JButton("Supplementary Color");
        supplementaryColorButton.addActionListener(this);
        highlightColorButton = new JButton("Highlight Color");
        highlightColorButton.addActionListener(this);
        okButton = new JButton("OK");
        okButton.addActionListener(this);
        resetColorsButton = new JButton("Reset Colors");
        resetColorsButton.addActionListener(this);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
                
        colorButtonPanel.add(baseColorButton);
        colorButtonPanel.add(supplementaryColorButton);
        colorButtonPanel.add(highlightColorButton);
        
        buttonPanel.add(okButton);
        buttonPanel.add(resetColorsButton);
        buttonPanel.add(cancelButton);
        
        //Set up color chooser for setting text color
        tcc = new JColorChooser();
        tcc.setBorder(BorderFactory.createTitledBorder(
                                             "Choose Colors"));

        add(colorButtonPanel, BorderLayout.NORTH);
        add(tcc, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) 
	{
		Object eventSource = e.getSource();
		
		if (eventSource instanceof JButton)
  		{ 	JButton cb = (JButton) eventSource;
  			String command = cb.getText();
  			if(command.equals("Base Color"))
  			{	
  				Color c = tcc.getColor();
  				Scrabble.baseColor = c;
  				baseColorButton.setBackground(c);
  			}
  			else if(command.equals("Supplementary Color"))
  			{	
  				Color c = tcc.getColor();
				supplementaryColorButton.setBackground(c);
  				Scrabble.supplementaryColor = c;
  			}
  			else if(command.equals("Highlight Color"))
  			{	Color c = tcc.getColor();
				highlightColorButton.setBackground(c);
  				Scrabble.highlightColor = c;
  			}
  			else if(command.equals("Reset Colors"))
  			{
  				baseColorButton.setBackground(Scrabble.lightTurquoise);
  				supplementaryColorButton.setBackground(Scrabble.lightTurquoise);
  				highlightColorButton.setBackground(Scrabble.lightTurquoise);
  				Scrabble.highlightColor = Scrabble.orange;
  				Scrabble.supplementaryColor = Scrabble.lightTurquoise;
  				Scrabble.baseColor = Scrabble.turquoise;
  			}
  			else if(command.equals("OK"))
  			{
  				frame.dispose();
  			}
  			else if(command.equals("Cancel"))
  			{
  				//reset colors
  				baseColorButton.setBackground(Scrabble.lightTurquoise);
  				supplementaryColorButton.setBackground(Scrabble.lightTurquoise);
  				highlightColorButton.setBackground(Scrabble.lightTurquoise);
  				Scrabble.highlightColor = Scrabble.orange;
  				Scrabble.supplementaryColor = Scrabble.lightTurquoise;
  				Scrabble.baseColor = Scrabble.turquoise;
  				frame.dispose();
  			}
			Scrabble.updateUIDefaults();
  		}
	}
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public static void createAndShowGUI() 
    {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        frame = new JFrame("Color Chooser");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new ColorChooser();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}
