package Begin;

import DreamBubbleMidlet;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

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

public class WaveAnimate extends GameCanvas implements Runnable {
	private DreamBubbleMidlet dreamBubbleMidlet;

	private int screenWidth;
	private int screenHeight;
	private int screenHalfWidth;
	private int screenHalfHeight;

	private Graphics g;
	private Image BG;
	private Image logo;
	private Image img;

	private int imgWidth;
	private int imgHeight;
	private int height;

	private long time;

	private Timer timer;

	private boolean isEnd;

	public WaveAnimate(DreamBubbleMidlet dreamBubbleMidlet) {
		super(true);

		this.setFullScreenMode(true);
		this.dreamBubbleMidlet = dreamBubbleMidlet;

		screenWidth = getWidth();
		screenHeight = getHeight();
		screenHalfWidth = screenWidth >> 1;
		screenHalfHeight = screenHeight >> 1;

		g = getGraphics();
		try {
			BG = Image.createImage("/Begin/StartBG.png");
			logo = Image.createImage("/Begin/logo.png");
			img = Image.createImage("/Begin/1.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		imgWidth = img.getWidth();
		imgHeight = img.getHeight();
	}

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

	public void run() {
		while (!isEnd) {
			long beginTime = System.currentTimeMillis();
			if (height + 1 < imgHeight) {
				height += 3;
			} else {
				timer = new Timer();
				timer.schedule(new TimerTask() {
					public void run() {
						showNextScreen();
					}
				}, 1500);
			}

			g.drawImage(BG, screenHalfWidth, screenHalfHeight, Graphics.VCENTER
					| Graphics.HCENTER);
			g.drawImage(logo, screenHalfWidth, screenHalfHeight,
					Graphics.VCENTER | Graphics.HCENTER);
			g.setClip(0, 0, screenWidth, screenHeight);
			g.drawRegion(img, 0, 0, imgWidth, height, Sprite.TRANS_NONE,
					screenHalfWidth, screenHalfHeight, Graphics.HCENTER
							| Graphics.BOTTOM);

			this.drawImage(g, img, true, 0.2, 0, 0, imgWidth, height,
					screenHalfWidth, screenHalfHeight, Graphics.HCENTER
							| Graphics.TOP);
			flushGraphics();
			long endTime = System.currentTimeMillis();
			if (endTime - beginTime < 50)
				try {
					Thread.sleep(50 - (endTime - beginTime));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		}
	}

	public void showNextScreen() {
		this.isEnd = true;
		timer.cancel();
		timer = null;
		this.dreamBubbleMidlet.beforeMenu.showMe();
	}

	public void drawImage(Graphics g, Image img, boolean inverted,
			double power, int x_src, int y_src, int width, int height,
			int x_dest, int y_dest, int anchor) throws IllegalArgumentException {
		switch (anchor) {
		case Graphics.TOP | Graphics.LEFT:
		case Graphics.TOP | Graphics.RIGHT:
		case Graphics.TOP | Graphics.HCENTER:
		case Graphics.BOTTOM | Graphics.LEFT:
		case Graphics.BOTTOM | Graphics.RIGHT:
		case Graphics.BOTTOM | Graphics.HCENTER:
		case Graphics.VCENTER | Graphics.LEFT:
		case Graphics.VCENTER | Graphics.RIGHT:
		case Graphics.VCENTER | Graphics.HCENTER:
			break;
		default:
			throw new IllegalArgumentException();
		}

		if (power < 0 || power > 1) {
			throw new IllegalArgumentException();
		}

		if (x_src < 0 || y_src < 0 || width < 0 || height < 0
				|| x_src + width > img.getWidth()
				|| y_src + height > img.getHeight()) {
			throw new IllegalArgumentException();
		}

		int swing = height >> 3;
		int offsetX = -((anchor & Graphics.RIGHT) != 0 ? width
				: (anchor & Graphics.HCENTER) != 0 ? width >> 1 : 0);
		int offsetY = -((anchor & Graphics.BOTTOM) != 0 ? height
				: (anchor & Graphics.VCENTER) != 0 ? height >> 1 : 0);

		time++;

		for (int n = 0, offset; n < height; n++) {
			offset = (int) ((swing * (n + 20)
					* Math.sin((double) swing * (height - n) / (n + 1) + time) / height) * power);

			if (n + offset < 0) {
				offset = -n;
			} else if (n + offset >= height) {
				offset = height - n - 1;
			}

			g.setClip(x_dest + offsetX, y_dest + offsetY + n, width, 1);

			if (inverted) {
				g.drawImage(img, x_dest + offsetX - x_src, y_dest + offsetY
						- y_src - height + 1 + (n << 1) + offset, Graphics.LEFT
						| Graphics.TOP);
			} else {
				g.drawImage(img, x_dest + offsetX - x_src, y_dest + offsetY
						- y_src - offset, Graphics.LEFT | Graphics.TOP);
			}
		}
	}
}
