package Game;

import java.io.IOException;

import javax.microedition.lcdui.Image;

public class NetGameNativeRole extends Role{

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

		protected NetGameNativeRole(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 updateRole() {
			if (this.characterClock < 10000) {
				this.characterClock++;
			} else {
				this.characterClock = 100;
			}

			int row = this.getRow();
			int col = this.getCol();

			if (this.isAlive) {
				
				int tool = this.game.map.getToolLayer().getCell(col, row);
				
				if (tool > 0) {
					this.pickupTool(tool);
					this.game.map.getToolLayer().setCell(col, row, 0);
				}

				if (this.game.map.hasFeature(row, col, Global.DEADLY)) {
					this.isAlive = false;
					((NetGame)this.game).sendActionAndPosition(Global.ISDEAD);
					
					return;
				}

				if (this.status == Global.BORN
						&& this.characterClock > Global.BORN_TIME) {
					this.status = Global.SOUTH;
					this.setFrame(Global.SOUTH * 6);
					this.isCanOperate = true;
				}

				if (this.status == Global.BORN) {
					if (this.characterClock % 2 == 0)
						this.setFrame(Global.BORN * 6 + (this.getFrame() - 1) % 4);
					return;
				}
				
			} else {
				this.isCanOperate = false;
				if (this.deadTime <= 20) {
					this.deadTime++;
				} else {
					this.deadTime = 100;
					this.setVisible(false);
					return;
				}

				if (this.characterClock % 2 == 0) {
					if (this.getFrame() < Global.DEAD * 6) {
						this.setFrame(Global.DEAD * 6);
					} else {
						if (this.getFrame() < 29) {
							this.setFrame(this.getFrame() + 1);
						} else {
							if (this.characterClock % 4 == 0) {
								this.setFrame(29);
								this.setVisible(true);
							} else {
								this.setVisible(false);
							}
						}
					}
				}
			}
		}

		public void pickupTool(int tool) {
			if (!this.isAlive || !this.isCanOperate)
				return;
			NetGame netGame = (NetGame)this.game;
			netGame.sendTool(tool);
			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 NetGameBomb(this.game,this));		
			this.game.map.removeFeature(row, col, Global.CAN_PASS);
			this.game.map.addFeature(row, col, Global.BOMB);
			this.bombNums--;
		}
}
