

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.*;

import acm.graphics.GLabel;
import acm.graphics.GLine;
import acm.graphics.GObject;
import acm.graphics.GOval;
import acm.graphics.GRect;
import acm.program.*;

public class Mindset extends GraphicsProgram implements MindsetConstants {

	private MindsetDataBase db = new MindsetDataBase();
	private BubbleMaker bubbleMaker = new BubbleMaker();

	private GLabel yearLabel = new GLabel("1800");

	private JTextField countryField = new JTextField(10);

	private String selectedCountryName = null;
	private int currYear = START_YEAR;

	public void init() {
		add(new JLabel("Country: "), NORTH);
		add(countryField, NORTH);
		add(new JButton("Select"), NORTH);
		addActionListeners();
	}

	public void run() {
		redraw();
		while(true) {
			drawClickInstructions();
			waitForClick();
			animate();
		}
	}

	public void actionPerformed(ActionEvent e) {
		selectedCountryName = countryField.getText();
		redraw();
		if(currYear == END_YEAR) {
			drawClickInstructions();
		}
	}

	private void animate() {
		for(currYear = START_YEAR; currYear <= END_YEAR; currYear++) {
			redraw();
			pause(DELAY);
		}
		currYear = END_YEAR;
	}

	private void redraw() {
		clear();
		drawGraphBackground();
		yearLabel.setText("" + currYear);
		plotBubbles(currYear);
		if(selectedCountryName != null) {
			Country toDraw = db.getCountryByName(selectedCountryName);
			drawCountryBox(toDraw);
		} else {
			drawCountryBox(null);
		}
	}


	private void plotBubbles(int currYear) {
		GOval selected = null;
		for(String name : db.getAllCountryNames()) {
			Country country = db.getCountryByName(name);
			GOval bubble = bubbleMaker.makeBubble(country, currYear);
			if(name.equals(selectedCountryName)) {
				selected = bubble;
				bubble.setColor(Color.RED);
			}
			add(bubble);
		}
		if(selected != null) {
			selected.sendToFront();
		}
	}

	/***************************************************************
	 *           PRIVATE!!! trespassers will be prosecuted         *
	 ***************************************************************/

	private void drawCountryBox(Country country) {
		GRect rect = new GRect(200, 200);
		int endY = APPLICATION_HEIGHT - X_AXIS_OFFSET;
		add(rect, getWidth() - rect.getWidth(), endY - rect.getHeight());
	}

	private void drawGraphBackground() {
		drawXAxis();
		drawYAxis();
		addCenteredLabel(yearLabel, APPLICATION_HEIGHT - 150);
	}

	private void drawClickInstructions() {
		GLabel label = new GLabel("Click to Animate");
		label.setFont("Times-32");
		label.setColor(new Color(0, 0, 255, 100));
		double x = (APPLICATION_WIDTH - label.getWidth()) / 2;
		double y = (APPLICATION_HEIGHT - label.getHeight()) / 2;
		add(label, x, y);
	}

	private void drawXAxis() {
		int y = APPLICATION_HEIGHT - X_AXIS_OFFSET;
		GLabel xAxisLabel = new GLabel(X_AXIS_LABEL);
		addCenteredLabel(xAxisLabel, y + 30);
		GLine line = new GLine(0, y, APPLICATION_WIDTH, y);
		add(line);
	}

	private void drawYAxis() {
		GLabel yAxisLabel = new GLabel(Y_AXIS_LABEL);
		yAxisLabel.rotate(90);
		yAxisLabel.setFont("Times-24");
		add(yAxisLabel, 25, APPLICATION_HEIGHT * 0.65);
		GLine line = new GLine(Y_AXIS_OFFSET, 0, Y_AXIS_OFFSET, getHeight());
		add(line);
	}

	private void addCenteredLabel(GLabel label, int y) {
		label.setFont("Times-24");
		double x = (APPLICATION_WIDTH - label.getWidth()) / 2;
		add(label, x, y);
	}
}
