import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 * In this game, the user is presented with an array of buttons that are 
 * initialized randomly to either black or white. Clicking on a button 
 * changes the color of the button and its two neighbors, if they exist. 
 * The object of the game is to reach a state when the buttons are all 
 * the same color. If this happens, something signals the win (how is 
 * up to you). Finish the code!
 *
 * @author Matt Boutell.
 */
public class TogglerFrame extends JFrame  implements ActionListener {
	
	private ArrayList<JButton> list = new ArrayList<JButton>();
	private JButton button;
	
	/**
	 * Starts here.
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		int nButtons = 7; // could vary, of course!
		boolean win = false;
		TogglerFrame frame = new TogglerFrame(nButtons);

//		for(int i = 0;i<=frame.list.size()-1;i++){
//			frame.list.get(i).addActionListener(frame);
//		}
		frame.setVisible(true);		
		while(win == false){
			for(int j = 1;j<=frame.list.size();j++)
				if(frame.list.get(j).getBackground()!=frame.list.get(j-1).getBackground())
					return;
			win = true;
		}
		frame.setTitle("YOU WIN! GOOD JOB");
	}

	/**
	 * Create a frame with the given number of buttons in it. 
	 * You are given the typical frame code to start.
	 * 
	 * @param nButtons The number of buttons to draw.
	 */
	public TogglerFrame(int nButtons){
		this.setTitle("Toggler, by Matt Boutell");
		this.setSize(new Dimension(800, 100));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1,nButtons));
		for(int i = 0; i<=nButtons-1;i++){
			button = new JButton();
			button.setPreferredSize(new Dimension(50,50));
			double prob = Math.random();
			if(prob <= .5)
				button.setBackground(Color.white);
			else if(prob > .5)
				button.setBackground(Color.black);
			this.list.add(button);
		}
		for(int j = 0; j<=this.list.size()-1;j++){
			this.list.get(j).addActionListener(this);
			panel.add(this.list.get(j));
		}
		this.add(panel);
	}

	public void actionPerformed(ActionEvent arg0) {
		this.button = (Jutton)arg0.getSource();
		Color back1 = this.button.getBackground();
		Color back2 = null;
		Color back3 = null;
		if(this.list.indexOf(this.button)>0)
			back3 = this.list.get(list.indexOf(this.button)-1).getBackground();
		if(this.list.indexOf(this.button) < this.list.size()-1)
			back2 = this.list.get(list.indexOf(this.button)+1).getBackground();
		
		if(back1 == Color.white)
			this.button.setBackground(Color.black);
		else if(back1 == Color.black)
			this.button.setBackground(Color.white);
		if(back2 != null){
			if(back2 == Color.white)
				this.list.get(this.list.indexOf(this.button)+1).setBackground(Color.black);
			else
				this.list.get(this.list.indexOf(this.button)+1).setBackground(Color.white);
		}
		if(back3 != null){
			if(back3 == Color.white)
				this.list.get(this.list.indexOf(this.button)-1).setBackground(Color.black);
			else
				this.list.get(list.indexOf(this.button)-1).setBackground(Color.white);
		}
	}

}
