package Menu;

import DreamBubbleMidlet;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;

import Game.Game;
import Game.NetGameSelect;
import Game.SingleGame;

public class Menu extends Canvas implements Runnable {

	private DreamBubbleMidlet dreamBubbleMidlet;

	private final static int LEFT = 0;
	private final static int RIGHT = 1;

	private Image menuImage[] = new Image[6];
	private Image menuName[] = new Image[6];
	private Image background;

	public Player musicPlayer;

	private int xCenter;
	private int yCenter;
	private int radius = 40;
	private int screenWidth;
	private int screenHeight;
	private int[] angle = { 90, 150, 210, 270, 330, 30 };

	private int count = 0;
	private int index = 0;
	private int direction;
	private int sleepTime = 6;

	private boolean firstTime = true;
	private boolean circling = false;

	public int SingleGameHeroType;
	public int netGameHeroType;
	public int SingleGameGrade;

	private Game game;
	private NetGameSelect netGameSelect;
	private Set set;
	private Help help;

	public Menu(DreamBubbleMidlet dreamBubbleMidlet) {
		this.setFullScreenMode(true);
		this.dreamBubbleMidlet = dreamBubbleMidlet;

		try {
			this.background = Image.createImage("/Menu/Menu.png");
			for (int i = 0; i < 6; i++) {
				this.menuImage[i] = Image.createImage("/Menu/" + (i + 1)
						+ ".png");
				this.menuName[i] = Image.createImage("/Menu/" + (i + 1 + 6)
						+ ".png");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 得到图片的长和宽
		this.screenWidth = this.getWidth();
		this.screenHeight = this.getHeight();
		xCenter = this.screenWidth / 2;
		yCenter = 3 * this.screenHeight / 5;
	}

	public void paint(Graphics g) {
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, this.screenWidth, this.screenHeight);
		g.drawImage(this.background, this.getWidth() / 2, this.getHeight() / 2,
				Graphics.HCENTER | Graphics.VCENTER);
		for (int i = 0; i < this.menuImage.length; i++) {
			g.drawImage(menuImage[i], (int) (this.xCenter + radius
					* Math.cos(Math.toRadians(angle[i]))),
					(int) (this.yCenter - radius
							* Math.sin(Math.toRadians(angle[i]))),
					Graphics.HCENTER | Graphics.VCENTER);
		}
		if (!firstTime)
			g.drawImage(menuName[index], xCenter, yCenter, Graphics.VCENTER
					| Graphics.HCENTER);
	}

	public void run() {
		while (true) {
			if (firstTime) {
				count = 0;
				while (count < 30) {
					for (int i = 0; i < angle.length; i++) {
						angle[i] -= 6;
						angle[i] = angle[i] % 360;
					}
					if (count % 2 == 0)
						this.radius += 1;
					repaint();
					count++;
					try {
						Thread.sleep(sleepTime);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				count = 0;
				while (count < 31) {
					for (int i = 0; i < angle.length; i++) {
						angle[i] -= 6;
						angle[i] = angle[i] % 360;
					}
					if (count % 2 == 0)
						this.radius += 1;
					repaint();
					count++;
					try {
						Thread.sleep(sleepTime);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				this.direction = RIGHT;
				this.shake();
				this.firstTime = false;
				repaint();
				break;
			} else {
				this.circling = true;
				if (this.direction == LEFT) {
					index--;
					if (index < 0)
						index = 5;
				}
				if (this.direction == RIGHT) {
					index++;
					if (index > 5)
						index = 0;
				}
				count = 0;
				while (count < 11) {
					for (int i = 0; i < angle.length; i++) {
						if (this.direction == LEFT)
							angle[i] += 6;
						else
							angle[i] -= 6;
					}
					repaint();
					count++;
					try {
						Thread.sleep(sleepTime);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				this.shake();
				this.circling = false;
				break;
			}
		}
	}

	public void showMe() {
		if (this.firstTime) {
			this.dreamBubbleMidlet.setup = null;
			this.dreamBubbleMidlet.start = null;
			this.dreamBubbleMidlet.waveAnimate = null;
			this.dreamBubbleMidlet.beforeMenu = null;
			System.gc();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			new Thread(this).start();
		} else {
			releaseResource();
		}
		try {
			this.musicPlayer = Manager.createPlayer(getClass()
					.getResourceAsStream("/Sound/start.mid"), "audio/midi");
			this.musicPlayer.prefetch();
			this.musicPlayer.setLoopCount(-1);
			this.musicPlayer.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.dreamBubbleMidlet.show(this);
	}

	public void releaseResource() {
		if (this.game != null) {
			this.game = null;
		}
		if (this.netGameSelect != null) {
			this.netGameSelect.releasReource();
			this.netGameSelect = null;
		}
		if (this.set != null) {
			this.set = null;
		}
		if (this.help != null) {
			this.help = null;
		}
		if(this.musicPlayer != null){
			try {
				this.musicPlayer.close();
				this.musicPlayer = null;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		System.gc();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void keyPressed(int keycode) {
		if (this.firstTime) {
			return;
		}

		switch (keycode) {
		case 50:
		case 52:
		case -1:
		case -3:
			if (this.circling)
				return;
			this.direction = LEFT;
			Thread t2 = new Thread(this);
			t2.start();
			break;
		case 56:
		case 54:
		case -2:
		case -4:
			if (this.circling)
				return;
			this.direction = RIGHT;
			Thread t1 = new Thread(this);
			t1.start();
			break;
		case 53:
		case -5:
			switch (index) {
			case 0:
				this.game = new SingleGame(this.dreamBubbleMidlet,
						this.SingleGameHeroType, this.SingleGameGrade);
				this.game.showMe();
				break;
			case 1:
				this.netGameSelect = new NetGameSelect(this.dreamBubbleMidlet);
				this.netGameSelect.showMe();
				break;
			case 2:
				this.set = new Set(this.dreamBubbleMidlet);
				this.set.showMe();
				break;
			case 4:
				this.help = new Help(this.dreamBubbleMidlet);
				this.help.showMe();
				break;
			case 5:
				this.dreamBubbleMidlet.exit();
				break;
			}
			break;
		}
	}

	private void shake() {
		count = 0;
		while (count < 4) {
			for (int i = 0; i < angle.length; i++) {
				if (direction == LEFT)
					angle[i] -= 3;
				else
					angle[i] += 3;
			}
			repaint();
			count++;
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		count = 0;

		while (count < 6) {
			for (int i = 0; i < angle.length; i++) {
				if (direction == LEFT)
					angle[i] += 2;
				else
					angle[i] -= 2;
			}
			repaint();
			count++;
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		count = 0;

		while (count < 6) {
			for (int i = 0; i < angle.length; i++) {
				if (direction == LEFT)
					angle[i] -= 1;
				else
					angle[i] += 1;
			}
			repaint();
			count++;
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		count = 0;
	}
}
