/*
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_RESOURCE_MANAGER_HPP
#define POLYGUN_ENGINE_RESOURCE_MANAGER_HPP

#include "common/thread_safe.hpp"

#include <string>
#include <map>
#include <vector>
#include <mutex>
#include <chrono>

#include "game/renderer/surface.hpp"

namespace polygun::renderer {
	class MasterRenderer;
	class Texture;
}

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

namespace polygun::engine {
	class ResourceManager final : public utils::ThreadSafe {
	private:
		template<typename T>
		struct LoadedResource {
			std::chrono::time_point<std::chrono::steady_clock> m_last_used;
			T m_resource;
		};

		struct PendingResource {
			std::string m_resource_id;
			std::string m_notifier_name;
		};

	public:
		ResourceManager(renderer::MasterRenderer* master_renderer, std::mutex& network_mutex);
		~ResourceManager();

		void on_packet(network::NetworkPacket& packet);

		renderer::Texture* get_texture(const std::string& resource_id, const std::string& notifier_name = "");
		renderer::Surface& get_surface(const std::string& resource_id, const std::string& notifier_name = "");
		bool is_notified(const std::string& notifier_name);
		void set_network_manager(network::NetworkManager* network_manager) { m_network_manager = network_manager; }
		bool is_pending();

	private:
		std::string m_user_dir;
		renderer::MasterRenderer* m_master_renderer;
		network::NetworkManager* m_network_manager;
		std::mutex& m_network_mutex;
		// Can be used to check for changes in any of notify groups
		// This is useful for using placeholder texture until server sends texture data and then replacing that
		// placeholder with actual texture
		std::map<std::string, bool> m_notifiers;
		// Loaded textures
		std::map<std::string, LoadedResource<renderer::Texture*>> m_textures;
		// Loaded surfaces
		std::map<std::string, LoadedResource<renderer::Surface>> m_surfaces;
		// Returned when texture/surface couldn't be loaded
		renderer::Texture* m_empty_texture;
		renderer::Surface m_empty_surface;
		// All resources that were requested from server but their data didn't arrive yet
		std::vector<PendingResource> m_pending_resources;
		// All resources that were either downloaded from server or their checksum matches with one from server
		std::vector<std::string> m_validated_resources;
	};
}

#endif // POLYGUN_ENGINE_RESOURCE_MANAGER_HPP
