package bt;

import java.io.DataInputStream;

import Game.Global;

public class Reader implements Runnable {

	public EndPoint endpt;

	private boolean done = false;

	public Reader() {

	}

	public void stop() {
		done = true;
	}

	public void run() {
		try {
			DataInputStream datain = endpt.con.openDataInputStream();
			while (!done) {
				byte signal = datain.readByte();
				if (signal == NetLayer.SIGNAL_GAME_MSG) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.callback.handAction(
							BTListener.EVENT_RECEIVED_POSITION_AND_ACTION,
							endpt, new MsgPacket(signal, buffer));
				} else if (signal == NetLayer.SIGNAL_HANDSHAKE) {

					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					if (endpt.btnet.isServer) {
						buffer[0] = Global.SERVER;
						buffer[1] = endpt.btnet.mapInfo;
						buffer[2] = endpt.btnet.roleInfo;
						buffer[3] = endpt.btnet.nowPlayerNumber;
						endpt.putString(NetLayer.SIGNAL_HANDSHAKE, buffer);
					} else {
						if (buffer[0] != Global.CLIENT) {
							endpt.callback.handAction(
									BTListener.EVENT_HOST_FOUND, endpt,
									new MsgPacket(signal, buffer));

							// 需要修改，这里默认第一个发现的房间即加入的房间
							endpt.remoteDeviceIsHost = true;
						}

						endpt.netPlayerRole = buffer[2];
						buffer[0] = Global.CLIENT;
						buffer[2] = endpt.btnet.roleInfo;
						endpt.putString(NetLayer.SIGNAL_HANDSHAKE_ACK, buffer);
					}

				} else if (signal == NetLayer.SIGNAL_HANDSHAKE_ACK) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.netPlayerRole = buffer[2];
					endpt.btnet.netPlayerRole = buffer[2];
				} else if (signal == NetLayer.SIGNAL_PLAYER_ENTER) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					byte[] temp = new byte[Global.INFORMATION_LENGTH];
					if (endpt.btnet.nowPlayerNumber == Global.MAX_PLAYER_NUMBER) {
						temp[0] = Global.FAILED;
						endpt.putString(NetLayer.SIGNAL_PLAYER_ENTER_ACK, temp);
					} else {
						endpt.inRoom = true;
						++endpt.btnet.nowPlayerNumber;
						temp[0] = Global.SUCCESSED;
						temp[3] = endpt.btnet.nowPlayerNumber;
						endpt.callback.handAction(
								BTListener.EVENT_RECEIVED_SYSTEM_MSG, endpt,
								new MsgPacket(signal, buffer));
						endpt.putString(NetLayer.SIGNAL_PLAYER_ENTER_ACK, temp);
					}
				} else if (signal == NetLayer.SIGNAL_PLAYER_ENTER_ACK) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.callback.handAction(
							BTListener.EVENT_RECEIVED_SYSTEM_MSG, endpt,
							new MsgPacket(signal, buffer));
				} else if (signal == NetLayer.SIGNAL_READY) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.btnet.myPlayerNumber = buffer[0];
					endpt.btnet.actualPlayerNum = buffer[1];
					System.out.println(buffer[0] + "  " + buffer[1]);
					endpt.btnet.sendPlayerNetNum();
					endpt.putString(NetLayer.SIGNAL_READY_ACK,
							endpt.btnet.information);
				} else if (signal == NetLayer.SIGNAL_PLAYER_NET_NUMBER) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.netPlayerNumber = buffer[0];
				} else if (signal == NetLayer.SIGNAL_READY_ACK) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.callback.handAction(
							BTListener.EVENT_RECEIVED_SYSTEM_MSG, endpt,
							new MsgPacket(signal, buffer));
				} else if (signal == NetLayer.SIGNAL_START_PLAY) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.putString(NetLayer.SIGNAL_START_PLAY_ACK, buffer);
					endpt.callback.handAction(
							BTListener.EVENT_RECEIVED_SYSTEM_MSG, endpt,
							new MsgPacket(signal, buffer));
				} else if (signal == NetLayer.SIGNAL_START_PLAY_ACK) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.callback.handAction(
							BTListener.EVENT_RECEIVED_SYSTEM_MSG, endpt,
							new MsgPacket(signal, buffer));
				} else if (signal == NetLayer.SIGNAL_TERMINATE) {
					byte[] buffer = new byte[Global.INFORMATION_LENGTH];
					datain.read(buffer);
					endpt.btnet.cleanupRemoteEndPoint(endpt);
					stop();
				}

			}
			datain.close();
		} catch (Exception e) {
			e.printStackTrace();
			endpt.callback.handAction(BTListener.EVENT_LEAVE, endpt, null);
		}
	}
}
