package horstmann.ch09_animation2;
import java.awt.BorderLayout;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

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

/**
   This program animates a sort algorithm.
 */
public class AnimationTester
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		ArrayComponent panel = new ArrayComponent();
		frame.add(panel, BorderLayout.CENTER);

		JButton stepButton = new JButton("Step");
		final JButton runButton = new JButton("Run");

		JPanel buttons = new JPanel();
		buttons.add(stepButton);
		buttons.add(runButton);
		frame.add(buttons, BorderLayout.NORTH);
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);

		Double[] values = new Double[VALUES_LENGTH];
		for (int i = 0; i < values.length; i++)
			values[i] = Math.random() * panel.getHeight();

		final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
		queue.add("Step");

		final Sorter sorter = new Sorter(values, panel, queue);

		stepButton.addActionListener(event -> {
			queue.add("Step");
			runButton.setEnabled(true);
		});

		runButton.addActionListener(event -> {
			runButton.setEnabled(false);
			queue.add("Run");
		});

		Thread sorterThread = new Thread(sorter);
		sorterThread.start();
	}

	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 300;
	private static final int VALUES_LENGTH = 30;
}
