package plots;

import java.awt.*;

public class AnimationPanel extends Panel {
	UnstickyButton bc[] = new UnstickyButton[3];
	AnimatedCanvas ac;
	boolean[] enabled = new boolean[3];
	static boolean[] E = { true, true, true };


	public AnimationPanel(AnimatedCanvas ac) {
		this(ac, 12, 60, 24);
	}

	public AnimationPanel(AnimatedCanvas ac, int N) {
		this(ac, N, 60, N+12);
	}

	public AnimationPanel(AnimatedCanvas ac, int N, int h) {
		this(ac, N, 60, h);
	}

	public AnimationPanel(AnimatedCanvas ac, int N, int w, int h) {
		this(ac, N, w, h, E);
	}

	public AnimationPanel(AnimatedCanvas ac, int N, int w, int h, boolean[] e) {
		super();
		this.ac = ac;
		Font fn = new Font("Helvetica", Font.BOLD, N);
		Panel bp = new Panel();
		setLayout(new GridBagLayout());
		bp.setLayout(new GridLayout(1, 3));
		add(bp);
		for (int i=0;i<3;i++) {
			enabled[i] = e[i];
		}
		if (enabled[0]) {
			bc[0] = new UnstickyButton("Run", w, h);
			bc[0].setFont(fn);
		} else {
			bc[0] = new UnstickyButton("", w, h);
		}
		bp.add(bc[0]);
		if (enabled[1]) {
			bc[1] = new UnstickyButton("Step", w, h);
			bc[1].setFont(fn);
		} else {
			bc[1] = new UnstickyButton("", w, h);
		}
		bp.add(bc[1]);
		if (enabled[2]) {
			bc[2] = new UnstickyButton("Reset", w, h);
			bc[2].setFont(fn);
		} else {
			bc[2] = new UnstickyButton("", w, h);
		}
		bp.add(bc[2]);
	}

	public void enable(int i, boolean b) {
		enabled[i] = b;
	}

	public boolean handleEvent(Event e) {
		if (e.target == bc[0] && enabled[0]) {
			if (e.id == Event.MOUSE_DOWN) {
				ac.start();
				return(true);
			} else if (e.id == Event.MOUSE_UP) {
				ac.stop();
				ac.update();
				return(true);
			}
		} else if (e.target == bc[1] && e.id == Event.MOUSE_DOWN && enabled[1]) {
			ac.step();
			ac.update();
			return(true);
		} else if (e.target == bc[2] && e.id == Event.MOUSE_DOWN && enabled[2]) {
			ac.reset();
			ac.update();
			return(true);
		}
		return(super.handleEvent(e));
	}

}