/*
PolyGun

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

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_SCREENS_GAME_SESSION_SCREEN_HPP
#define POLYGUN_SCREENS_GAME_SESSION_SCREEN_HPP

#include "game/engine/screen.hpp"

#include <atomic>
#include <thread>
#include <config.hpp>

#include "game/engine/player_camera.hpp"
#include "game/world/chunk.hpp"
#include "game/engine/greedy_meshing.hpp"
#include "game/engine/resource_manager.hpp"
#include "game/engine/content_registry.hpp"
#include "game/engine/texture_atlas.hpp"

namespace polygun::renderer {
	class Shader;
	class Mesh;
	class Texture;
}

namespace polygun::screens {
	enum GameState {
		GAME_STATE_JOINING,
		GAME_STATE_LOADING,
		GAME_STATE_FAILED,
		GAME_STATE_TIMED_OUT,
		GAME_STATE_FINISHED,
		GAME_STATE_RUNNING
	};
	class GameSessionScreen final : public engine::Screen {
	public:
		GameSessionScreen(const std::string& ip, unsigned short port, const std::string& player_nick, bool start_server = false);

		virtual void begin() override;
		virtual void render() override;
		virtual void update(double delta) override;
		virtual void event(const window::Event& ev) override;
		virtual void finish() override;

	private:
		engine::greedy_meshing m_greedy;
		std::string m_ip;
		unsigned short m_port;
		std::string m_player_nick;
#if defined(BUILD_SERVER)
		bool m_start_server;
		std::atomic<bool> m_server_running;
#endif
		renderer::Shader* m_chunk_shader;
		renderer::Mesh* m_node_mesh;
		world::Chunk m_chnk;
		engine::PlayerCamera m_camera;
		std::unique_ptr<engine::ResourceManager> m_resource_manager;
		std::unique_ptr<engine::ContentRegistry> m_content_registry;
		std::unique_ptr<engine::TextureAtlas> m_texture_atlas;
		//bool m_is_esc_pressed;
		bool m_was_camera_locked;
		std::unique_ptr<std::thread> m_connection_thread;
		std::mutex m_network_mutex;
#if defined(BUILD_SERVER)
		std::unique_ptr<std::thread> m_local_server_thread;
#endif
		std::atomic<GameState> m_game_state;
		uint32_t m_player_uuid;

	private:
		void connection_thread_func();
#if defined(BUILD_SERVER)
		void local_server_thread_func();
#endif
		void update_mesh();
	};
}

#endif // POLYGUN_SCREENS_GAME_SESSION_SCREEN_HPP
