import java.io.IOException;

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


public class SetUp extends GameCanvas implements Runnable{
	
	public boolean isLoading = true;
	public int loadPercent;
	public DreamBubbleMidlet dreamBubbleMidlet;
	
	private Graphics g;
	private Image loadingImage;
	
	private int screenWidth;
	private int screenHeight;
	
	public SetUp(DreamBubbleMidlet dreamBubbleMidlet){
		super(true);
		this.setFullScreenMode(true);
		this.dreamBubbleMidlet = dreamBubbleMidlet;
		this.screenWidth = this.getWidth();
		this.screenHeight = this.getHeight();
		
		try {
			this.loadingImage = Image.createImage("/Game/Loading.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		g = this.getGraphics();
	}	
	
	public void run(){
		while(this.isLoading){
			g.setColor(66, 70, 246);
			g.fillRect(0, 0, screenWidth, screenHeight);

			g.drawImage(loadingImage, screenWidth / 2, 2 * screenHeight / 5,
					Graphics.HCENTER | Graphics.VCENTER);

			g.setColor(0, 255, 0);
			g.fillRect((screenWidth - 120) / 2, 2 * screenHeight / 3,
					(this.loadPercent * 120) / 100, 10);

			g.setColor(255, 0, 0);
			g.drawRect((screenWidth - 120) / 2, 2 * screenHeight / 3, 120, 10);

			this.flushGraphics();
		}
		this.dreamBubbleMidlet.start.showMe();
	}
	
	public void showMe(){
		new Thread(new LoadingDriver()).start();
		new Thread(this).start();
		this.dreamBubbleMidlet.show(this);
	}

	class LoadingDriver implements Runnable {
		public void run() {
			SetUp.this.isLoading = true;
			SetUp.this.dreamBubbleMidlet.init();
			System.gc();
			SetUp.this.isLoading = false;
		}
	}
}
