/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "game/engine/content_registry.hpp"

#include <cassert>
#include <algorithm>

#include "common/network/network_packet.hpp"
#include "common/network/network_manager.hpp"
#include "common/logger.hpp"

using namespace polygun::engine;

ContentRegistry::ContentRegistry(std::mutex& network_mutex) :
	m_network_mutex(network_mutex),
	m_network_manager(nullptr),
	m_registered_node_count(),
	m_node_defs(),
	m_notifiers(),
	m_pending_defs(),
	m_unknown_node()
{
	world::NodeDef& air_node = m_node_defs[0];
	air_node.m_string_id = "builtin:air";
	air_node.m_display_icon = "builtin/texture/air";

	m_unknown_node.m_string_id = "builtin:unknown";
	m_unknown_node.m_display_icon = "builtin/texture/empty";
}

void ContentRegistry::on_packet(network::NetworkPacket& packet) {
	acquire();
	assert(packet.get_id()==network::NetworkPacketID::PACKET_NODE_DEFINITION);
	uint16_t node_id;
	if(!packet.read(node_id) || !packet.read(&m_node_defs[node_id])) {
		LOG_ERROR("Invalid node definition packet received");
		release();
		return;
	}

	std::vector<uint16_t>::iterator it = std::find(m_pending_defs.begin(), m_pending_defs.end(), node_id);
	if(it==m_pending_defs.end()) {
		release();
		return;
	}

	m_pending_defs.erase(it);
	m_notifiers[node_id] = true;

	release();
}

void ContentRegistry::request_node_def(uint16_t id) {
	acquire();
	m_network_mutex.lock();
	network::NetworkPacket packet = m_network_manager->create_packet(network::NetworkPacketFlag::FLAG_RELIABLE, network::NetworkPacketID::PACKET_NODE_DEFINITION);
	m_network_mutex.unlock();
	packet.write(id);
	m_network_mutex.lock();
	m_network_manager->send_packet(packet);
	m_network_manager->update();
	m_network_mutex.unlock();
	m_pending_defs.push_back(id);
	release();
}

polygun::world::NodeDef ContentRegistry::get_node_def(uint16_t id) {
	acquire();

	if(m_node_defs.count(id)) {
		const world::NodeDef result = m_node_defs[id];
		release();
		return result;
	}

	std::vector<uint16_t>::iterator it = std::find(m_pending_defs.begin(), m_pending_defs.end(), id);
	if(it!=m_pending_defs.end()) {
		release();
		return m_unknown_node;
	}
	release();

	request_node_def(id);

	return m_unknown_node;
}

bool ContentRegistry::is_notified(uint16_t node_id) {
	acquire();
	if(m_notifiers[node_id]) {
		m_notifiers[node_id] = false;
		release();
		return true;
	}
	release();
	return false;
}

bool ContentRegistry::are_all_node_defs_available() {
	acquire();
	bool result = m_node_defs.size()==m_registered_node_count;
	release();
	return result;
}
