using System;
using UnityEngine;
using UnityEngine.Networking;

public class PacketCode {
	public const short Message = 100;
	public const short Move = 200;
	public const short EVA = 201;
	public const short Spawn = 202;
	public const short ControllerChange = 203;
	public const short NewPlayer = 204;
	public const short RequestMessage = 300;
	public const short RequestMove = 301;
	public const short RequestEVA = 302;
	public const short RequestControllerChange = 303;
}

public abstract class Buffer : MessageBase {
	public abstract short getID();
}

public abstract class ActionBuffer : Buffer {
	public void sendToAll() {
		//NetworkServer.SendToAll(getID(), this);
		NetworkManager.toClients(this);
	}

	public void sendToClient(int connectionID) {
		//NetworkServer.SendToAll(getID(), this);
		NetworkManager.toClient(this, connectionID);
	}

	public abstract void sendToAllAndExecute();
}

public abstract class RequestBuffer : Buffer {
	public void sendToServer() {
		//if (NetworkManager.networkClient.isConnected)
		//	NetworkManager.networkClient.Send(getID(), this);
		NetworkManager.toServer(this);
	}
}

//==============================================================================
//Template
//==============================================================================

/*
public class TemplateBuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.Template;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.template(this);
	}

	//TODO: fields
}

public class RequestTemplateBuffer : RequestBuffer {
	override public short getID() {
		return PacketCode.RequestTemplate;
	}

	//TODO: same fields
}
*/

//==============================================================================
//Message
//==============================================================================

public class MessageBuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.Message;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.writeMessage(this);
	}

	public string message;
}

public class RequestMessageBuffer : RequestBuffer {
	override public short getID() {
		return PacketCode.RequestMessage;
	}

	public string message;
}

//==============================================================================
//Move
//==============================================================================

public class MoveBuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.Move;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.move(this);
	}

	public void sendToNonOwnerAndExecute(int connectionID) {
		/*foreach (NetworkConnection i in NetworkServer.connections) {
			if (i == null || i.connectionId == connectionID) continue;
			NetworkServer.SendToClient(i.connectionId, getID(), this);
		}*/
		NetworkManager.toClients(this, connectionID);
		Actions.move(this);
	}

	//Maybe add unreliable send for this one

	public int id;
	public Vector2 position;
	public Vector2 speed;
	public float rotation;
}

public class RequestMoveBuffer : RequestBuffer {
	override public short getID() {
		return PacketCode.RequestMove;
	}

	//Maybe add unreliable send for this one

	public int id;
	public Vector2 position;
	public Vector2 speed;
	public float rotation;
}

//==============================================================================
//EVA
//==============================================================================

public class EVABuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.EVA;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.EVA(this);
	}

	public int id;
}

public class RequestEVABuffer : RequestBuffer {
	override public short getID() {
		return PacketCode.RequestEVA;
	}

	public int id;
}

//==============================================================================
//Spawn
//==============================================================================


public class SpawnBuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.Spawn;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.spawn(this);
	}

	public ObjectType type;
	public int index;
	public Vector2 position;
	public float rotation;
	public int id;
	public int parentSpaceShipID;
}

//==============================================================================
//Controller Change
//==============================================================================


public class ControllerChangeBuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.ControllerChange;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.changeController(this);
	}

	public int playerID;
	public int gameObjectID;
	public int internalControllerID;
}

public class RequestControllerChangeBuffer : RequestBuffer {
	override public short getID() {
		return PacketCode.RequestControllerChange;
	}

	public int playerID;
	public int gameObjectID;
	public int internalControllerID;
}

//==============================================================================
//New Player
//==============================================================================

public class NewPlayerBuffer : ActionBuffer {
	override public short getID() {
		return PacketCode.NewPlayer;
	}

	override public void sendToAllAndExecute() {
		sendToAll();
		Actions.newPlayer(this);
	}

	public void sendToNonNewAndExecute(int connectionID) {
		NetworkManager.toClients(this, connectionID);
		Actions.newPlayer(this);
	}

	public int playerID;
	public int characterID;
	public bool isSelf;
}