import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class SolarSystem extends Applet implements Runnable, MouseListener {
	int WIDTH;
	Planet[] planetes;
	Thread t;
	GregorianCalendar cal;
	boolean running;
	
	public void init() {
		planetes = new Planet[] {
			new Planet("Soleil", Color.white, 0, 1, 0, false),
			new Planet("Mercure", Color.orange, 57, 87, 110, false),
			new Planet("Vénus", Color.yellow, 108, 224, 215, false),
			new Planet("Terre", Color.cyan, 150, 365, 270, false),
			new Planet("Mars", Color.red, 227, 686, 210, false)
		};
		WIDTH =planetes[planetes.length-1].getDemiGrandAxe()*2;
		resize(WIDTH, WIDTH);
		t = new Thread(this);
		cal = new GregorianCalendar();
		addMouseListener(this);
	}
	
	public void start() {
		running = true;
		t.start();
	}
	
	public void addOneDay() {
		cal.add(Calendar.DATE, 1);
		for (int i=0;i<planetes.length;i++) {
			planetes[i].addOneDay();
		}
	}
	
	public void display() {
		for (int i=0;i<planetes.length;i++) {
			System.out.println(planetes[i].getNom() + "\t" + (int) planetes[i].getAngle());
		}
	}
	
	public void run() {
		while (true) {
			if (running) {
				addOneDay();
				display();
				System.out.println();
				repaint();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					System.err.println(e);
					System.exit(0);
				}
			}
		}
	}
	
	public String getDate() {
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int month = cal.get(Calendar.MONTH)+1;
		int year = cal.get(Calendar.YEAR);
		return new String(day + "/" + month + "/" + year);
	}

	public void paint(Graphics g) {
		setBackground(Color.black);
		for (int i=0;i<planetes.length;i++) {
			Planet planete = planetes[i];
			int grandAxe = 2*planetes[i].getDemiGrandAxe();
			int delta = (WIDTH - grandAxe) / 2;
			g.setColor(Color.white);
			g.drawOval(delta, delta, grandAxe, grandAxe);
			Point pos = planete.getCoord();
			pos.translate(WIDTH/2, WIDTH/2);
			g.setColor(planete.getColor());
			g.fillOval((int) pos.getX() - 5, (int) pos.getY() - 5, 10, 10);
			g.drawString(planete.getNom(), (int) pos.getX() + 10, (int) pos.getY() + 10);
		}
		g.setColor(Color.white);
		g.drawString(getDate(), 10, 10);
	}

	public void mouseClicked(MouseEvent arg0) {
		running = !running;
		
	}

	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
}
