package Game;

import DreamBubbleMidlet;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import bt.NetLayer;

public class NetGame extends Game {

	public NetLayer netlayer;

	protected Vector netPlayers;

	private NetGameRole hero1;

	private NetGameRole hero2;

	private NetGameRole hero3;

	private int myHeroType;

	private int netHeroType;

	private NetGameReleaseSourceListener netGameReleaseSourceListener;

	public NetGame(DreamBubbleMidlet dreamBubbleMidlet, int map,
			int myHeroType, int netHeroType, int netHero2Type) {

		super(dreamBubbleMidlet);
		// TODO Auto-generated constructor stub
		// hero2 = (NetGameRole)netPlayers.elementAt(0);
		this.myHeroType = myHeroType;
		this.netHeroType = netHeroType;
		this.stageIndex = map;
	}

	public void loadPlayer() {

		if (netlayer.actualPlayerNum == 2) {
			if (netlayer.isServer) {
				this.player = NetGameNativeRole.createNetGameNativeRole(this,
						this.myHeroType, this.bornPlace[0][0],
						this.bornPlace[0][1]);
			} else {
				this.player = NetGameNativeRole.createNetGameNativeRole(this,
						this.myHeroType, this.bornPlace[1][0],
						this.bornPlace[1][1]);
			}
			this.gameLayerManager.append(player);

			this.netPlayers = new Vector();

			NetGameRole netPlayer;
			if (netlayer.isServer)
				netPlayer = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[1][0], this.bornPlace[1][1]);
			else
				netPlayer = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[0][0], this.bornPlace[0][1]);

			this.netPlayers.addElement(netPlayer);
			this.gameLayerManager.append(netPlayer);

			if (netlayer.isServer)
				hero2 = netPlayer;
			else
				hero1 = netPlayer;
		} else if (netlayer.actualPlayerNum == 3) {

			this.netPlayers = new Vector();
			NetGameRole netPlayer;
			NetGameRole netPlayer2;

			if (netlayer.isServer) {
				this.player = NetGameNativeRole.createNetGameNativeRole(this,
						this.myHeroType, this.bornPlace[0][0],
						this.bornPlace[0][1]);
				netPlayer = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[1][0], this.bornPlace[1][1]);
				netPlayer2 = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[2][0], this.bornPlace[2][1]);
				this.gameLayerManager.append(player);
				this.netPlayers.addElement(netPlayer);
				this.gameLayerManager.append(netPlayer);
				this.netPlayers.addElement(netPlayer2);
				this.gameLayerManager.append(netPlayer2);
				hero2 = netPlayer;
				hero3 = netPlayer2;
			} else if (netlayer.myPlayerNumber == 2) {
				netPlayer = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[0][0], this.bornPlace[0][1]);
				this.player = NetGameNativeRole.createNetGameNativeRole(this,
						this.myHeroType, this.bornPlace[1][0],
						this.bornPlace[1][1]);
				netPlayer2 = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[2][0], this.bornPlace[2][1]);
				this.gameLayerManager.append(player);
				this.netPlayers.addElement(netPlayer);
				this.gameLayerManager.append(netPlayer);
				this.netPlayers.addElement(netPlayer2);
				this.gameLayerManager.append(netPlayer2);
				hero1 = netPlayer;
				hero3 = netPlayer2;
			} else {
				netPlayer = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[0][0], this.bornPlace[0][1]);
				netPlayer2 = NetGameRole.createNetGameRole(this, netHeroType,
						this.bornPlace[1][0], this.bornPlace[1][1]);
				this.player = NetGameNativeRole.createNetGameNativeRole(this,
						this.myHeroType, this.bornPlace[2][0],
						this.bornPlace[2][1]);
				this.gameLayerManager.append(player);
				this.netPlayers.addElement(netPlayer);
				this.gameLayerManager.append(netPlayer);
				this.netPlayers.addElement(netPlayer2);
				this.gameLayerManager.append(netPlayer2);
				hero1 = netPlayer;
				hero2 = netPlayer2;
			}

		}

	}

	public void playerUpdate() {
		this.player.updateRole();
		if (this.player.deadTime == 21) {
			this.nextStage();
			return;
		}
		this.netPlayersUpdate();
	}

	protected void keyPressed(int key) {
		if (!this.isPlaying)
			return;
		if (this.isEnd) {
			this.showNextScreen();
			return;
		}
		if (!this.isPause && key == -7) {// 右键
			this.sendActionAndPosition(Global.PAUSE_REQ);
			return;
		}
		if (key == 35) {// #键
			this.nextStage();
			return;
		}
		if (key == 42) {// *键
			this.preStage();
			return;
		}
		if (this.isPause) {
			switch (key) {
			case -1:
			case -3:
				if (this.chooseIndex == 0)
					this.chooseIndex = 2;
				else
					this.chooseIndex = (this.chooseIndex - 1) % 3;
				break;
			case -2:
			case -4:
				this.chooseIndex = (this.chooseIndex + 1) % 3;
				break;
			case -5:// 确认键
			case -6:// 左软键
				switch (chooseIndex) {
				case 0:
					this.continueGame();
					this.sendActionAndPosition(Global.CONTINUE_REQ);
					break;
				case 1:
					this.restart();
					break;
				case 2:
					this.sendActionAndPosition(Global.QUIT);
					this.endNetGame("你已经离开了游戏");
					break;
				}
				break;
			default:
				break;
			}
		} else {
			switch (key) {
			case 53:
			case -5:// 确认键
				this.player.setBomb(this.player.getRow(), this.player.getCol());
				this.sendActionAndPosition(Global.SETBOMB);
				break;
			}
		}
	}

	public void operate() {
		int keyStates = getKeyStates();
		if ((keyStates & DOWN_PRESSED) != 0) {
			this.player.walk(Global.SOUTH);
			this.sendActionAndPosition(Global.GODOWN);
		} else {
			if ((keyStates & UP_PRESSED) != 0) {
				this.player.walk(Global.NORTH);
				this.sendActionAndPosition(Global.GOUP);
			} else {
				if ((keyStates & RIGHT_PRESSED) != 0) {
					this.player.walk(Global.EAST);
					this.sendActionAndPosition(Global.GORIGHT);
				} else {
					if ((keyStates & LEFT_PRESSED) != 0) {
						this.player.walk(Global.WEST);
						this.sendActionAndPosition(Global.GOLEFT);
					}
				}
			}
		}
	}

	public void netPlayersUpdate() {
		Enumeration enu = this.netPlayers.elements();
		while (enu.hasMoreElements())// 遍历炸弹集合
		{
			NetGameRole netPlayer = (NetGameRole) enu.nextElement();
			netPlayer.updateRole();
			if (netPlayer.deadTime == 21) {
				netPlayer.setVisible(false);
				this.netPlayers.removeElement(netPlayer);
				System.out.println(netPlayer.deadTime);
				netPlayer = null;
				if (this.netPlayers.isEmpty()) {
					this.nextStage();
				}
			}
		}
	}

	public void sendActionAndPosition(byte action) {
		byte heroX = (byte) (this.player.xCoodinate - Global.POSITION_BASEOFF);
		byte heroY1 = (byte) (this.player.yCoodinate / 10);
		byte heroY2 = (byte) (this.player.yCoodinate % 10);
		byte[] s = { action, heroX, heroY1, heroY2 };
		this.netlayer.sendPosition(s);
	}

	public void sendTool(int tool) {
		sendActionAndPosition((byte) (tool + Global.PICKTOOL_BASEOFF));
	}

	public void resetPositionHero1(int x, int y) {
		hero1.xCoodinate = x;
		hero1.yCoodinate = y;
	}

	public void echoInputHero1(byte s) {
		if (s == Global.ISDEAD) {
			this.hero1.isAlive = false;
			return;
		}
		if (s == Global.SETBOMB) {
			this.hero1.setBomb(this.hero1.getRow(), this.hero1.getCol());
			return;
		}
		if (s == Global.GODOWN) {
			this.hero1.walk(Global.SOUTH);
			return;
		}
		if (s == Global.GOUP) {
			this.hero1.walk(Global.NORTH);
			return;
		}
		if (s == Global.GORIGHT) {
			this.hero1.walk(Global.EAST);
			return;
		}
		if (s == Global.GOLEFT) {
			this.hero1.walk(Global.WEST);
			return;
		}
		if (s > Global.PICKTOOL_BASEOFF) {
			hero1.pickupTool(s - Global.PICKTOOL_BASEOFF);
			return;
		}
		if (s == Global.PAUSE_REQ) {
			this.sendActionAndPosition(Global.PAUSE_ACK);
			this.chooseIndex = 0;
			this.pauseGame();
			return;
		}
		if (s == Global.PAUSE_ACK) {
			this.chooseIndex = 0;
			this.pauseGame();
			return;
		}
		if (s == Global.CONTINUE_REQ) {
			this.sendActionAndPosition(Global.CONTINUE_ACK);
			this.continueGame();
			return;
		}
		if (s == Global.CONTINUE_ACK) {
			this.continueGame();
			return;
		}
		if (s == Global.QUIT) {
			this.endNetGame("你的朋友离开了游戏");
			return;
		}
	}

	public void resetPositionHero2(int x, int y) {
		hero2.xCoodinate = x;
		hero2.yCoodinate = y;
	}

	public void echoInputHero2(byte s) {
		if (s == Global.ISDEAD) {
			this.hero2.isAlive = false;
			return;
		}
		if (s == Global.SETBOMB) {
			this.hero2.setBomb(this.hero2.getRow(), this.hero2.getCol());
			return;
		}
		if (s == Global.GODOWN) {
			this.hero2.walk(Global.SOUTH);
			return;
		}
		if (s == Global.GOUP) {
			this.hero2.walk(Global.NORTH);
			return;
		}
		if (s == Global.GORIGHT) {
			this.hero2.walk(Global.EAST);
			return;
		}
		if (s == Global.GOLEFT) {
			this.hero2.walk(Global.WEST);
			return;
		}
		if (s > Global.PICKTOOL_BASEOFF) {
			hero2.pickupTool(s - Global.PICKTOOL_BASEOFF);
			return;
		}
		if (s == Global.PAUSE_REQ) {
			this.sendActionAndPosition(Global.PAUSE_ACK);
			this.chooseIndex = 0;
			this.pauseGame();
			return;
		}
		if (s == Global.PAUSE_ACK) {
			this.chooseIndex = 0;
			this.pauseGame();
			return;
		}
		if (s == Global.CONTINUE_REQ) {
			this.sendActionAndPosition(Global.CONTINUE_ACK);
			this.continueGame();
			return;
		}
		if (s == Global.CONTINUE_ACK) {
			this.continueGame();
			return;
		}
		if (s == Global.QUIT) {
			this.endNetGame("你的朋友离开了游戏");
			return;
		}
	}

	public void resetPositionHero3(int x, int y) {
		hero3.xCoodinate = x;
		hero3.yCoodinate = y;
	}

	public void echoInputHero3(byte s) {
		if (s == Global.ISDEAD) {
			this.hero3.isAlive = false;
			return;
		}
		if (s == Global.SETBOMB) {
			this.hero3.setBomb(this.hero3.getRow(), this.hero3.getCol());
			return;
		}
		if (s == Global.GODOWN) {
			this.hero3.walk(Global.SOUTH);
			return;
		}
		if (s == Global.GOUP) {
			this.hero3.walk(Global.NORTH);
			return;
		}
		if (s == Global.GORIGHT) {
			this.hero3.walk(Global.EAST);
			return;
		}
		if (s == Global.GOLEFT) {
			this.hero3.walk(Global.WEST);
			return;
		}
		if (s > Global.PICKTOOL_BASEOFF) {
			hero3.pickupTool(s - Global.PICKTOOL_BASEOFF);
			return;
		}
		if (s == Global.PAUSE_REQ) {
			this.sendActionAndPosition(Global.PAUSE_ACK);
			this.chooseIndex = 0;
			this.pauseGame();
			return;
		}
		if (s == Global.PAUSE_ACK) {
			this.chooseIndex = 0;
			this.pauseGame();
			return;
		}
		if (s == Global.CONTINUE_REQ) {
			this.sendActionAndPosition(Global.CONTINUE_ACK);
			this.continueGame();
			return;
		}
		if (s == Global.CONTINUE_ACK) {
			this.continueGame();
			return;
		}
		if (s == Global.QUIT) {
			this.endNetGame("你的朋友离开了游戏");
			return;
		}
	}

	public void endNetGame(String gameResult) {
		this.isEnd = true;
		this.mainThread = null;
		this.drawException(gameResult);
	}

	/**
	 * 这个是新加的方法，这个处理的是在有异常后随便按一个键，就返回主界面 这个不是你调用的，你只需要调用上面的那个endNetGame方法就可以了
	 */
	public void showNextScreen() {
		this.netlayer.disconnect();
		this.netGameReleaseSourceListener.releaseResource();
		this.dreamBubbleMidlet.menu.showMe();
	}

	public void drawException(String exception) {
		this.g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
				Font.SIZE_SMALL));
		this.g.setColor(0x00ff00);
		this.g.drawString(exception, screenWidth / 2, screenHeight / 2,
				Graphics.HCENTER | Graphics.TOP);
		this.flushGraphics();
	}

	public void addNetGameReleaseSourceListener(
			NetGameReleaseSourceListener netGameReleaseSourceListener) {
		this.netGameReleaseSourceListener = netGameReleaseSourceListener;
	}
}
