import java.awt.Label;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JButton;

/**
 * The ColorBox class is a color pallette. It stores the color the user clicked
 * with the right and left mouse button. It also contains a set of preset colors
 * which can be changed with the advanced feature.
 **/
public class ColorBox extends JPanel 
{
	public static Label foreGround, backGround;
	public static int selectedSwatchX, selectedSwatchY;
	public static ColorLabel[][] colorPal;
	private JButton clrPnlBtn;
	private ColorPanel clrPnl;
	
	/** Creates a default ColorBox. **/
	public ColorBox()
	{
		// sets the layout to null and sets a size to the color box
		setLayout(null);
		setSize(350,120);	
		// instantiates the Mouse Left and Mouse Right color labels
		setForeBack();
	
		// instantiates the Color Panel
		clrPnl = new ColorPanel();
		
		// instantiates a button that calls for advanced settings, sets the size
		// and the x,y coordinates and adds it to screen
		clrPnlBtn = new JButton("Advanced");
		clrPnlBtn.setBounds(100,65,100,20);
		add(clrPnlBtn);
		clrPnlBtn.addActionListener(new AdvancedListener());
		
		// instantiates the color label and it finds its corresponding color 
		// depending on its x and y coordinates
		colorPal = new ColorLabel[2][14];
		for(int i=0;i<colorPal.length;i++)
			for(int j=0;j<colorPal[i].length;j++)
			{
				Color c = setDefaultColors(i,j);
				colorPal[i][j] = new ColorLabel(c,j*20+40,i*20+20,i,j);
				add(colorPal[i][j]);
			}
		
		setVisible(true);
	}
	
	/** Sets the ColorLabels with its corresponding default colors **/
	public Color setDefaultColors(int x, int y)
	{
		if(x == 0 && y == 0) return Color.BLACK;
		if(x == 0 && y == 1) return new Color(128,128,128);
		if(x == 0 && y == 2) return new Color(128,0,0);
		if(x == 0 && y == 3) return new Color(128,128,0);
		if(x == 0 && y == 4) return new Color(0,128,0);
		if(x == 0 && y == 5) return new Color(0,128,128);
		if(x == 0 && y == 6) return new Color(0,0,128);
		if(x == 0 && y == 7) return new Color(128,0,128);
		if(x == 0 && y == 8) return new Color(128,128,64);
		if(x == 0 && y == 9) return new Color(0,64,64);
		if(x == 0 && y == 10) return new Color(0,128,255);
		if(x == 0 && y == 11) return new Color(0,64,128);
		if(x == 0 && y == 12) return new Color(128,0,255);
		if(x == 0 && y == 13) return new Color(128,64,0);
		
		if(x == 1 && y == 0) return Color.WHITE;
		if(x == 1 && y == 1) return new Color(192,192,192);
		if(x == 1 && y == 2) return Color.RED;
		if(x == 1 && y == 3) return Color.YELLOW;
		if(x == 1 && y == 4) return Color.GREEN;
		if(x == 1 && y == 5) return Color.CYAN;
		if(x == 1 && y == 6) return Color.BLUE;
		if(x == 1 && y == 7) return Color.MAGENTA;
		if(x == 1 && y == 8) return new Color(255,255,128);
		if(x == 1 && y == 9) return new Color(0,255,128);
		if(x == 1 && y == 10) return new Color(128,255,255);
		if(x == 1 && y == 11) return new Color(128,128,255);
		if(x == 1 && y == 12) return new Color(255,0,128);
		if(x == 1 && y == 13) return new Color(255,128,64);
		
		return Color.WHITE;
	}
	
	/** Instantiates the mouse right and left click colors **/
	public void setForeBack()
	{
		// creates the foreground and background label and then sets the size
		// and its x,y coordinates and adds it to the Color Box
		foreGround = new Label();
		backGround = new Label();
		foreGround.setBounds(10,10,15,15);
		foreGround.setBackground(new Color(255,255,255));
		backGround.setBounds(17,17,15,15);
		backGround.setBackground(new Color(255,255,255));
		add(foreGround);
		add(backGround);
	}
	
	class AdvancedListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			clrPnl.setVisible(true);
		}	
	}	
}