package bt;

import DreamBubbleMidlet;
import Game.Global;
import Game.NetGame;
import Game.NetGameReleaseSourceListener;
import Game.NetGameSelect;

/**
 * 蓝牙通讯的初始化类
 * 
 * @author DZF
 * @version 1.0.0
 * 
 */
public class BTMain implements BTListener, NetGameReleaseSourceListener {

	public static BTMain instance;

	public NetGameSelect selectui;

	public NetLayer btnet;

	public NetGame game;

	private DreamBubbleMidlet dreamBubbleMidlet;

	public BTMain(DreamBubbleMidlet dreamBubbleMidlet, NetGameSelect selectui) {
		this.dreamBubbleMidlet = dreamBubbleMidlet;
		this.selectui = selectui;
		instance = this;
	}

	public void startSearch(boolean isServer, byte map, byte role) {
		btnet = new NetLayer();
		btnet.isServer = isServer;
		btnet.mapInfo = map;
		btnet.roleInfo = role;
		System.out.println("roleinfo:" + role);
		btnet.init(this);
		btnet.query();
	}

	public void enterRoom() {
		btnet.sendJoinMsg(NetLayer.SIGNAL_PLAYER_ENTER);
	}

	public void startGame() {
		btnet.actualPlayerNum = btnet.nowPlayerNumber;
		btnet.sendBeReadyMsg();
		btnet.sendPlayMsg();
	}

	public void handAction(byte action, EndPoint endpt, MsgPacket m) {
		// TODO Auto-generated method stub

		switch (action) {

		case BTListener.EVENT_RECEIVED_POSITION_AND_ACTION: {

			System.out.println(endpt.netPlayerNumber);
			if (endpt.netPlayerNumber == 2) {
				game.resetPositionHero2(
						(m.information[1] + Global.POSITION_BASEOFF),
						(m.information[2] * 10 + m.information[3]));
				game.echoInputHero2(m.information[0]);
			} else if (endpt.netPlayerNumber == 1) {
				game.resetPositionHero1(
						(m.information[1] + Global.POSITION_BASEOFF),
						(m.information[2] * 10 + m.information[3]));
				game.echoInputHero1(m.information[0]);
			} else {
				game.resetPositionHero3(
						(m.information[1] + Global.POSITION_BASEOFF),
						(m.information[2] * 10 + m.information[3]));
				game.echoInputHero3(m.information[0]);
			}

			break;
		}
		case BTListener.EVENT_HOST_FOUND: {
			btnet.mapInfo = m.information[1];
			btnet.netPlayerRole = m.information[2];

			// 临时解决方法
			String temp = endpt.remoteName + " " + m.information[3] + "/"
					+ Global.MAX_PLAYER_NUMBER;
			System.out.println(temp);
			selectui.addRoom(temp);

			break;
		}
		case BTListener.EVENT_RECEIVED_SYSTEM_MSG: {

			if (m.type == NetLayer.SIGNAL_PLAYER_ENTER_ACK) {
				if (m.information[0] == Global.FAILED) {
					selectui
							.isShowMessageInWaitingRoom(true, "房间人数已满，无法加入指定游戏");
				} else if (m.information[0] == Global.SUCCESSED) {
					selectui.isShowMessageInWaitingRoom(true, "成功加入指定游戏");
					selectui.isInRoom = true;
					endpt.netPlayerNumber = 1;
				}

			} else if (m.type == NetLayer.SIGNAL_PLAYER_ENTER) {
				// messageui.msgs.addElement(endpt.remoteName
				// + " enter this room...");
				// messageui.addCommand(new Command("Ready", Command.SCREEN,
				// messageui.commandNum++));
				// messageui.msgs
				// .addElement(endpt.remoteName + " is in the room!");
				// messageui.repaint();
				selectui.addPlayer(endpt.remoteName);
				btnet.actualPlayerNum = btnet.nowPlayerNumber;
				btnet.sendBeReadyMsg();
			} else if (m.type == NetLayer.SIGNAL_READY_ACK) {
				// messageui.msgs.addElement(endpt.remoteName + " is ready!");
				btnet.readyPlayerNum++;
				selectui.isShowMessageInWaitingPlayer(true, "玩家加入游戏，您可以开始游戏");
			} else if (m.type == NetLayer.SIGNAL_START_PLAY) {

				this.dreamBubbleMidlet.menu.releaseResource();

				btnet.stopSearch();

				btnet.actualPlayerNum = m.information[3];
				System.out.println("Max:" + btnet.actualPlayerNum);
				System.out.println("mapinfo:" + btnet.mapInfo);
				game = new NetGame(this.dreamBubbleMidlet, btnet.mapInfo,
						btnet.roleInfo, btnet.netPlayerRole,
						btnet.netPlayer2Role);
				game.netlayer = btnet;
				//--------------------------------------
				//加的东西是注册释放资源监听器，在game里面释放资源
				game.addNetGameReleaseSourceListener(this);
				//--------------------------------------
				this.game.showMe();
			} else if (m.type == NetLayer.SIGNAL_START_PLAY_ACK) {
				this.dreamBubbleMidlet.menu.releaseResource();

				btnet.stopSearch();

				System.out.println("mapinfo:" + btnet.mapInfo);
				game = new NetGame(this.dreamBubbleMidlet, btnet.mapInfo,
						btnet.roleInfo, btnet.netPlayerRole,
						btnet.netPlayer2Role);
				game.netlayer = btnet;
				//----------------------------------------
				game.addNetGameReleaseSourceListener(this);
				//----------------------------------------
				this.game.showMe();
			}
			// messageui.repaint();
			break;
		}
		case BTListener.EVENT_LEAVE: {
			if (game != null)
				game.endNetGame("由于网络不稳定，你的朋友离开了游戏");
			break;
		}
		}
	}

	/**
	 * 这里实现了一个释放资源的借口，和你没有多大关系估计
	 */
	public void releaseResource() {
		this.game = null;
		System.gc();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
