/*
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.
*/

#ifndef POLYGUN_ENGINE_CONTENT_REGISTRY_HPP
#define POLYGUN_ENGINE_CONTENT_REGISTRY_HPP

#include "common/thread_safe.hpp"

#include <map>
#include <mutex>
#include <vector>

#include "common/world/node_def.hpp"

namespace polygun::network {
	class NetworkPacket;
	class NetworkManager;
}

namespace polygun::engine {
	class ContentRegistry final : public utils::ThreadSafe {
	public:
		ContentRegistry(std::mutex& network_mutex);
		~ContentRegistry() = default;

		void on_packet(network::NetworkPacket& packet);
		void request_node_def(uint16_t id);

		void set_network_manager(network::NetworkManager* network_manager) { m_network_manager = network_manager; }
		void set_registered_node_count(uint16_t registered_node_count) { m_registered_node_count = registered_node_count; }
		uint16_t get_registered_node_count() const { return m_registered_node_count; }
		world::NodeDef get_node_def(uint16_t id);
		bool is_notified(uint16_t node_id);
		bool are_all_node_defs_available();

	private:
		std::mutex& m_network_mutex;
		network::NetworkManager* m_network_manager;
		uint16_t m_registered_node_count;
		std::map<uint16_t, world::NodeDef> m_node_defs;
		std::map<uint16_t, bool> m_notifiers;
		std::vector<uint16_t> m_pending_defs;
		world::NodeDef m_unknown_node;
	};
}

#endif
