#include "NetworkInterface.h"

#include <godot_cpp/classes/multiplayer_api.hpp>
#include <godot_cpp/classes/multiplayer_peer.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

namespace blitz {

using namespace godot;

void NetworkInterface::_bind_methods() {
	ClassDB::bind_method(D_METHOD("AddPlayer", "a_PlayerId", "a_PlayerName"), &NetworkInterface::AddPlayer);
	ClassDB::bind_method(D_METHOD("RemovePlayer", "a_PlayerId"), &NetworkInterface::RemovePlayer);
	ClassDB::bind_method(D_METHOD("SetPlayerPositionAndRotation", "a_PlayerId", "a_Position", "a_Rotation"),
		&NetworkInterface::SetPlayerPositionAndRotation);

	ADD_SIGNAL(MethodInfo("AddPlayer", PropertyInfo(Variant::INT, "a_PlayerId"), PropertyInfo(Variant::STRING, "a_PlayerName")));
	ADD_SIGNAL(MethodInfo("RemovePlayer", PropertyInfo(Variant::INT, "a_PlayerId")));
	ADD_SIGNAL(MethodInfo("SetPlayerPositionAndRotation", PropertyInfo(Variant::INT, "a_PlayerId"),
		PropertyInfo(Variant::VECTOR3, "a_Position"), PropertyInfo(Variant::VECTOR3, "a_Rotation")));
}

NetworkInterface::NetworkInterface() {}

NetworkInterface::~NetworkInterface() {}

void NetworkInterface::AddPlayer(int64_t a_PlayerId, godot::String a_PlayerName) {
	emit_signal("AddPlayer", a_PlayerId, a_PlayerName);
}

void NetworkInterface::RemovePlayer(int64_t a_PlayerId) {
	emit_signal("RemovePlayer", a_PlayerId);
}

void NetworkInterface::SetPlayerPositionAndRotation(int64_t a_PlayerId, Vector3 a_Position, Vector3 a_Rotation) {
	emit_signal("SetPlayerPositionAndRotation", a_PlayerId, a_Position, a_Rotation);
}

void NetworkInterface::_ready() {
	Dictionary config;
	config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;
	config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
	config["call_local"] = true;
	config["channel"] = 0;
	rpc_config("AddPlayer", config);
	rpc_config("RemovePlayer", config);

	Dictionary config2;
	config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
	config["channel"] = 0;
	config2["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;
	config2["call_local"] = false;
	rpc_config("SetPlayerPositionAndRotation", config2);
}


}  // namespace blitz