package Begin;
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;

public class Start extends GameCanvas implements Runnable {

	private DreamBubbleMidlet dreamBubbleMidlet;

	public final static int LEFT = 0;
	public final static int RIGHT = 1;
	public final static int UP = 2;
	public final static int DOWN = 3;
	public final static int LEFT_UP = 4;
	public final static int RIGHT_UP = 5;
	public final static int LEFT_DOWN = 6;
	public final static int RIGHT_DOWN = 7;

	private Graphics g;
	private Image universePet;
	private Image logo;
	private Image startBG;

	private LayerManager logoManager;

	private int screenWidth;
	private int screenHeight;
	private int xPetCoordinate;
	private int yPetCoordinate;
	private int imgPetWidth;
	private int imgPetHeight;
	private int imgLogoWidth;
	private int imgLogoHeight;
	private int step = 4;
	private int width = 0;
	
	private boolean isEnd;

	public Start(DreamBubbleMidlet dreamBubbleMidlet) {
		super(true);
		this.setFullScreenMode(true);

		this.dreamBubbleMidlet = dreamBubbleMidlet;
		this.g = this.getGraphics();
		this.logoManager = new LayerManager();

		try {
			this.universePet = Image.createImage("/Begin/universePet.png");
			this.logo = Image.createImage("/Begin/logo.png");
			this.startBG = Image.createImage("/Begin/StartBG.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.logoManager.append(new Sprite(this.logo));

		this.screenWidth = this.getWidth();
		this.screenHeight = this.getHeight();
		this.imgPetWidth = this.universePet.getWidth();
		this.imgPetHeight = this.universePet.getHeight();
		this.xPetCoordinate = this.screenWidth - 3 * this.imgPetWidth / 5;
		this.yPetCoordinate = this.screenHeight - this.imgPetHeight;
		this.imgLogoWidth = this.logo.getWidth();
		this.imgLogoHeight = this.logo.getHeight();
	}

	public void run() {
		this.move(LEFT, 52);
		this.move(LEFT_UP, 144);
		new Thread(new LogoDriver()).start();
		this.move(RIGHT, 30);		
		this.move(LEFT, 16);
		this.move(DOWN, 36);
		this.move(RIGHT, 34);
		this.move(UP, 36);
		this.move(RIGHT_DOWN, 22);
		this.move(LEFT_DOWN, 14);
		this.move(RIGHT_UP, 14);
		this.move(RIGHT, 24);
		this.move(UP, 22);
		this.move(RIGHT_DOWN, 14);
		this.move(RIGHT_UP, 14);
		this.move(DOWN, 36);
		this.move(RIGHT, 4);
		this.move(RIGHT_UP, 22);
		this.move(RIGHT_DOWN, 22);
		this.move(RIGHT_UP, 36);
		this.move(DOWN, 36);
		this.move(LEFT_UP, 36);
		this.move(UP, 200);		
		this.showNextScreen();
	}

	public void drawScreen(Graphics g) {
		g.drawImage(this.startBG, screenWidth / 2, screenHeight / 2,
				Graphics.HCENTER | Graphics.VCENTER);
		g.drawImage(this.universePet, this.xPetCoordinate, this.yPetCoordinate,
				Graphics.VCENTER | Graphics.HCENTER);
		this.logoManager.setViewWindow(0, 0, width, this.imgLogoHeight);
		this.logoManager.paint(g, (this.screenWidth - this.imgLogoWidth) / 2,
				(this.screenHeight - this.imgLogoHeight) / 2);
	}

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

	public void move(int direction, int distance) {
		for (int i = 0; i < distance / this.step; i++) {
			this.drawScreen(g);
			this.flushGraphics();
			this.moveByStep(direction);
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void moveByStep(int direction) {
		switch (direction) {
		case Start.LEFT:
			this.xPetCoordinate -= step;
			break;
		case Start.RIGHT:
			this.xPetCoordinate += step;
			break;
		case Start.UP:
			this.yPetCoordinate -= step;
			break;
		case Start.DOWN:
			this.yPetCoordinate += step;
			break;
		case Start.LEFT_UP:
			this.xPetCoordinate -= step;
			this.yPetCoordinate -= step;
			break;
		case Start.LEFT_DOWN:
			this.xPetCoordinate -= step;
			this.yPetCoordinate += step;
			break;
		case Start.RIGHT_UP:
			this.xPetCoordinate += step;
			this.yPetCoordinate -= step;
			break;
		case Start.RIGHT_DOWN:
			this.xPetCoordinate += step;
			this.yPetCoordinate += step;
			break;
		}
	}

	private class LogoDriver implements Runnable {
		public void run() {
			while (!isEnd) {
				if (Start.this.width < Start.this.imgLogoWidth)
					if (Start.this.width < Start.this.imgLogoWidth / 2)
						Start.this.width += 1;
					else{
						Start.this.width += 1;
					}
					else {
						break;
					}
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
