package Game;

import java.io.IOException;

import javax.microedition.lcdui.Image;

public class SingleGameRole extends Role {

	public static SingleGameRole createSingleGameRole(Game game, int type, int row,
			int col) {
		SingleGameRole singleGameRole = null;
		switch (type) {
		case Global.JACK:
			try {
				singleGameRole = new SingleGameRole(Image
						.createImage("/Character/Jack.png"), Global.MAP_CELL,
						Global.JACK_HEIGHT, game);
				singleGameRole.height = Global.JACK_HEIGHT;
				singleGameRole.power = Global.JACK_POWER;
				singleGameRole.bombNums = Global.JACK_BOMBNUM;
				singleGameRole.speed = Global.JACK_SPEED;
				singleGameRole.setFrame(6);
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case Global.JOHN:
			try {
				singleGameRole = new SingleGameRole(Image
						.createImage("/Character/John.png"), Global.MAP_CELL,
						Global.JOHN_HEIGHT, game);
				singleGameRole.height = Global.JOHN_HEIGHT;
				singleGameRole.power = Global.JOHN_POWER;
				singleGameRole.bombNums = Global.JOHN_BOMBNUM;
				singleGameRole.speed = Global.JOHN_SPEED;
				singleGameRole.setFrame(6);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		singleGameRole.type = type;
		singleGameRole.row = row;
		singleGameRole.col = col;
		singleGameRole.status = Global.BORN;
		singleGameRole.width = Global.MAP_CELL;
		singleGameRole.xCoodinate = Global.MAP_CELL * col;
		singleGameRole.yCoodinate = Global.MAP_CELL * (row + 1) - singleGameRole.height;
		singleGameRole.setPosition(singleGameRole.xCoodinate, singleGameRole.yCoodinate);
		return singleGameRole;
	}

	protected SingleGameRole(Image image, int width, int Height, Game game) {
		super(image, width, Height, game);
	}

	public void collisionCheck(int direction) {
		switch (direction) {
		case Global.NORTH:
			if (this.getPreY(this.yCoodinate - this.speed) >= 0
					&& this.game.map.hasFeature(this.getRow(this
							.getPreY(yCoodinate - speed)), this
							.getCol(xCoodinate), Global.CAN_PASS)
					&& this.game.map.hasFeature(this.getRow(this
							.getPreY(yCoodinate - speed)), this.getCol(this
							.getRightX(xCoodinate)), Global.CAN_PASS)) {
				this.yCoodinate -= speed;
			} else {
				this.yCoodinate -= Math.min(this.getPreY(yCoodinate)
						% Global.MAP_CELL, speed);

				if (this.xCoodinate % Global.MAP_CELL <= Global.INGORE_ALIGNMENT) {
					this.xCoodinate -= Math.min(this.xCoodinate
							% Global.MAP_CELL, speed);
				} else {
					if (Global.MAP_CELL - this.xCoodinate % Global.MAP_CELL <= Global.INGORE_ALIGNMENT) {
						this.xCoodinate += Math.min(Global.MAP_CELL
								- this.xCoodinate % Global.MAP_CELL, speed);
					}
				}
			}
			break;
		case Global.SOUTH:
			if (this.getBottomY(yCoodinate + speed) < this.game.map.mapRows
					* Global.MAP_CELL
					&& this.game.map.hasFeature(this.getRow(this
							.getBottomY(yCoodinate + speed)), this
							.getCol(xCoodinate), Global.CAN_PASS)
					&& this.game.map.hasFeature(this.getRow(this
							.getBottomY(yCoodinate + speed)), this.getCol(this
							.getRightX(xCoodinate)), Global.CAN_PASS)) {
				this.yCoodinate += speed;
			} else {
				this.yCoodinate += Math.min(
						(Global.MAP_CELL - (yCoodinate + this.height)
								% Global.MAP_CELL)
								% Global.MAP_CELL, speed);
				if (this.xCoodinate % Global.MAP_CELL <= Global.INGORE_ALIGNMENT) {
					this.xCoodinate -= Math.min(this.xCoodinate
							% Global.MAP_CELL, speed);
				} else {
					if (Global.MAP_CELL - this.xCoodinate % Global.MAP_CELL <= Global.INGORE_ALIGNMENT) {
						this.xCoodinate += Math.min(this.xCoodinate
								% Global.MAP_CELL, speed);
					}
				}
			}
			break;
		case Global.WEST:
			if (this.xCoodinate - this.speed >= 0
					&& this.game.map.hasFeature(this.getRow(this
							.getPreY(yCoodinate)), this.getCol(xCoodinate
							- speed), Global.CAN_PASS)
					&& this.game.map.hasFeature(this.getRow(this
							.getBottomY(yCoodinate)), this.getCol(xCoodinate
							- speed), Global.CAN_PASS)) {
				this.xCoodinate -= speed;
			} else {
				this.xCoodinate -= Math.min(this.xCoodinate % Global.MAP_CELL,
						speed);
				if (this.getPreY(this.yCoodinate) % Global.MAP_CELL <= Global.INGORE_ALIGNMENT)
					this.yCoodinate -= Math.min(speed, getPreY(this.yCoodinate)
							% Global.MAP_CELL);
				else if (Global.MAP_CELL - getPreY(this.yCoodinate)
						% Global.MAP_CELL <= Global.INGORE_ALIGNMENT)
					this.yCoodinate += Math.min(speed, Global.MAP_CELL
							- getPreY(this.yCoodinate) % Global.MAP_CELL);
			}
			break;
		case Global.EAST:
			if (this.getRightX(xCoodinate + speed) < this.game.map.mapCols
					* Global.MAP_CELL
					&& this.game.map.hasFeature(this.getRow(this
							.getPreY(yCoodinate)), this.getCol(this
							.getRightX(xCoodinate + speed)), Global.CAN_PASS)
					&& this.game.map.hasFeature(this.getRow(this
							.getBottomY(yCoodinate)), this.getCol(this
							.getRightX(xCoodinate + speed)), Global.CAN_PASS)) {
				this.xCoodinate += speed;
			} else {
				this.xCoodinate += Math.min((Global.MAP_CELL - xCoodinate
						% Global.MAP_CELL)
						% Global.MAP_CELL, speed);

				if (getPreY(this.yCoodinate) % Global.MAP_CELL <= Global.INGORE_ALIGNMENT) {
					yCoodinate -= Math.min(speed, getPreY(yCoodinate)
							% Global.MAP_CELL);
				} else {
					if (Global.MAP_CELL - getPreY(yCoodinate) % Global.MAP_CELL <= Global.INGORE_ALIGNMENT)
						yCoodinate += Math.min(speed, Global.MAP_CELL
								- getPreY(yCoodinate) % Global.MAP_CELL);
				}
			}
		}
	}

	public void pickupTool(int tool) {
		if (!this.isAlive || !this.isCanOperate)
			return;
		switch ((tool - 1) / 3) {
		case Global.SHOES_INC:
			if (this.speed < Global.MAX_SPEED) {
				this.speed++;
			}
			break;
		case Global.SUPER_SHOES:
			this.speed = Global.MAX_SPEED;
			break;
		case Global.POWER_INC:
			if (this.power < Global.MAX_POWER)
				this.power++;
			break;
		case Global.BOMB_INC:
			increaseBomb();
			break;
		case Global.SUPER_POWER:
			this.power = Global.MAX_POWER;
			break;
		case Global.SUPER_BOMB:
			this.bombNums = Global.MAX_BOMB_NUMBER;
			break;
		}
	}

	public void setBomb(int row, int col) {
		if (!this.isAlive || !this.isCanOperate || this.bombNums == 0
				|| this.game.map.hasFeature(row, col, Global.BOMB))
			return;
		String key = row * 10 + "" + col;
		this.game.bombTable.put(key, new SingleGameBomb(this.game,this));		
		this.game.map.removeFeature(row, col, Global.CAN_PASS);
		this.game.map.addFeature(row, col, Global.BOMB);
		this.bombNums--;
	}
}
