package Menu;

import DreamBubbleMidlet;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

import Game.Global;

public class Set extends GameCanvas implements Runnable {
	protected DreamBubbleMidlet dreamBubbleMidlet;

	protected Graphics g;
	protected Image setImage;
	protected Image characterImage;
	protected Image gradeImage;
	protected Image itemImage;

	protected Sprite JohnSprite;
	protected Sprite JackSprite;
	protected Sprite simpleSprite;
	protected Sprite normalSprite;
	protected Sprite hardSprite;

	protected LayerManager layerManager;

	protected int screenWidth;
	protected int screenHeight;
	protected int itemIndex;
	protected int characterIndex;
	protected int gradeIndex;

	protected boolean isEnd;

	public Set(DreamBubbleMidlet dreamBubbleMidlet) {
		super(false);
		this.setFullScreenMode(true);
		this.dreamBubbleMidlet = dreamBubbleMidlet;

		this.screenWidth = this.getWidth();
		this.screenHeight = this.getHeight();

		try {
			this.setImage = Image.createImage("/Menu/Set.png");
			this.characterImage = Image.createImage("/Menu/Character.png");
			this.gradeImage = Image.createImage("/Menu/Grade.png");
			this.itemImage = Image.createImage("/Menu/Item.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.g = this.getGraphics();

		this.layerManager = new LayerManager();
		this.layerManager.setViewWindow(0, 0, screenWidth, screenHeight);

		this.JohnSprite = new Sprite(itemImage, 80, 80);
		this.JackSprite = new Sprite(itemImage, 80, 80);
		this.simpleSprite = new Sprite(itemImage, 80, 80);
		this.normalSprite = new Sprite(itemImage, 80, 80);
		this.hardSprite = new Sprite(itemImage, 80, 80);

		this.JohnSprite.setFrame(0);
		this.JackSprite.setFrame(1);
		this.simpleSprite.setFrame(2);
		this.normalSprite.setFrame(3);
		this.hardSprite.setFrame(4);

		this.layerManager.append(JohnSprite);
		this.layerManager.append(JackSprite);
		this.layerManager.append(simpleSprite);
		this.layerManager.append(normalSprite);
		this.layerManager.append(hardSprite);

		this.JohnSprite.setPosition(screenWidth / 3 - 40, 60);
		this.JackSprite.setPosition(2 * screenWidth / 3 - 40, 60);
		this.simpleSprite.setPosition(screenWidth / 6 - 40, 200);
		this.normalSprite.setPosition(screenWidth / 2 - 40, 200);
		this.hardSprite.setPosition(5 * screenWidth / 6 - 40, 200);

	}

	public void drawScreen() {
		g.setColor(0x000000);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(setImage, screenWidth / 2, screenHeight / 2,
				Graphics.VCENTER | Graphics.HCENTER);
		g.drawImage(characterImage, screenWidth / 2, 15, Graphics.TOP
				| Graphics.HCENTER);
		g.drawImage(gradeImage, screenWidth / 2, screenHeight / 2 + 10,
				Graphics.TOP | Graphics.HCENTER);
		layerManager.paint(g, 0, 0);
	}

	public void reflash() {
		switch (this.itemIndex) {
		case 0:
			if (this.characterIndex == 0) {
				this.JohnSprite.setFrame(5);
				this.JackSprite.setFrame(1);
				this.dreamBubbleMidlet.menu.SingleGameHeroType = Global.JOHN;
				this.dreamBubbleMidlet.menu.netGameHeroType = Global.JOHN;
			} else {
				this.JohnSprite.setFrame(0);
				this.JackSprite.setFrame(6);
				this.dreamBubbleMidlet.menu.SingleGameHeroType = Global.JACK;
				this.dreamBubbleMidlet.menu.netGameHeroType = Global.JACK;
			}
			break;
		case 1:
			switch (this.gradeIndex) {
			case 0:
				this.simpleSprite.setFrame(7);
				this.normalSprite.setFrame(3);
				this.hardSprite.setFrame(4);
				this.dreamBubbleMidlet.menu.SingleGameGrade = Global.SIMPLE;
				break;
			case 1:
				this.simpleSprite.setFrame(2);
				this.normalSprite.setFrame(8);
				this.hardSprite.setFrame(4);
				this.dreamBubbleMidlet.menu.SingleGameGrade = Global.NORMAL;
				break;
			case 2:
				this.simpleSprite.setFrame(2);
				this.normalSprite.setFrame(3);
				this.hardSprite.setFrame(9);
				this.dreamBubbleMidlet.menu.SingleGameGrade = Global.HARD;
				break;
			}
		}
	}

	protected void keyPressed(int key) {
		switch (key) {
		case -1:
		case -3:
			if (this.itemIndex == 0) {
				if (this.characterIndex == 0) {
					this.characterIndex = 1;
				} else {
					this.characterIndex = 0;
				}
			} else {
				if (this.gradeIndex == 0) {
					this.gradeIndex = 2;
				} else {
					this.gradeIndex = (this.gradeIndex - 1) % 3;
				}
			}
			break;			
		case -2:
		case -4:
			if (this.itemIndex == 0) {
				if (this.characterIndex == 0) {
					this.characterIndex = 1;
				} else {
					this.characterIndex = 0;
				}
			} else {
				this.gradeIndex = (this.gradeIndex + 1) % 3;
			}
			break;
		case -5:
		case -6:
		case 53:
			if (this.itemIndex == 0) {
				if (this.characterIndex == 0) {
					this.JohnSprite.setFrame(10);
					this.JackSprite.setFrame(1);
				} else {
					this.JohnSprite.setFrame(0);
					this.JackSprite.setFrame(11);
				}
				this.itemIndex = 1;
			} else {
				switch (this.gradeIndex) {
				case 0:
					this.simpleSprite.setFrame(12);
					this.normalSprite.setFrame(3);
					this.hardSprite.setFrame(4);
					break;
				case 1:
					this.simpleSprite.setFrame(2);
					this.normalSprite.setFrame(13);
					this.hardSprite.setFrame(4);
					break;
				case 2:
					this.simpleSprite.setFrame(2);
					this.normalSprite.setFrame(3);
					this.hardSprite.setFrame(14);
					break;
				}
				this.showMenu();
			}
		case -7:
			this.showMenu();
		}
	}

	public void run() {
		while (!isEnd) {
			this.reflash();
			this.drawScreen();
			this.flushGraphics();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void showMe() {
		this.isEnd = false;
		new Thread(this).start();
		this.dreamBubbleMidlet.show(this);
	}

	public void showMenu() {
		this.isEnd = true;
		this.itemIndex = 0;
		this.characterIndex = 0;
		this.gradeIndex = 0;
		this.dreamBubbleMidlet.menu.showMe();
	}
}
