/*
PolyGun

Copyright (c) 2023 kacperks https://kacperks.cubesoftware.xyz

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 "engine.hpp"

#include "game/audio/audio.hpp"

#include "common/logger.hpp"
#include "game/screens/main_menu_screen.hpp"
#include "game/screens/game_session_screen.hpp"
#include "game/window/window.hpp"
#include "game/gui/style.hpp"
#include "game/gui/constraint.hpp"

using namespace polygun::renderer;

static unsigned g_window_width = 1280;
static unsigned g_window_height = 720;

static polygun::renderer::RendererType determine_renderer_type() {
	// TODO: Implement loading renderer type from config
#if defined(RENDERER_GL)
	return polygun::renderer::RendererType::RENDERER_TYPE_GL;
#elif defined(RENDERER_LEGACY_GL)
	return polygun::renderer::RendererType::RENDERER_TYPE_LEGACY_GL;
#endif
	LOG_FATAL("Failed to automatically determine renderer. You may want to set it manually.");
	return polygun::renderer::RendererType::RENDERER_TYPE_GL;
}

namespace polygun::engine {
	Engine::Engine() :
		m_screen_manager(*this),
		m_window(nullptr),
		m_master_renderer(nullptr)
	{}

	void Engine::init(DirectJoinArguments* direct_join) {
		renderer::RendererType renderer_type = determine_renderer_type();
		m_window = window::Window::create_window("PolyGun", g_window_width, g_window_height, renderer_type);
		m_master_renderer = renderer::MasterRenderer::create_master_renderer(renderer_type, m_window);
		m_gui.set_width(new gui::FixedConstraint(get_screen_width()));
		m_gui.set_height(new gui::FixedConstraint(get_screen_height()));

		gui::Style::create();
		gui::Style::get().load_from_file("res/gui/styles/blue.xml");

		if(direct_join) {
			// Push game screen
			std::unique_ptr<screens::GameSessionScreen> game_session_screen = std::make_unique<screens::GameSessionScreen>(direct_join->m_ip, direct_join->m_port, direct_join->m_nickname, direct_join->m_host_server);
			m_screen_manager.push_screen(std::move(game_session_screen));
		}
		else {
			// Push menu screen
			std::unique_ptr<screens::MainMenuScreen> main_menu_screen = std::make_unique<screens::MainMenuScreen>();
			m_screen_manager.push_screen(std::move(main_menu_screen));
		}

		bool running = true;
		window::Event event;
		while (running && m_screen_manager.has_screen())
		{
			m_screen_manager.render();
			m_gui.render(get_gui_renderer());
			m_screen_manager.update();

			m_window->finish_frame();
			while(m_window->poll_events(event)>0) {
				switch(event.m_type) {
				case window::EventType::EVENT_TYPE_WINDOW_CLOSE:
					running = false;
					break;
				case window::EventType::EVENT_TYPE_WINDOW_RESIZE:
					g_window_width = event.m_data.m_window_event.m_width;
					g_window_height = event.m_data.m_window_event.m_height;
					m_gui.set_width(new gui::FixedConstraint(get_screen_width()));
					m_gui.set_height(new gui::FixedConstraint(get_screen_height()));
					get_gui_renderer()->on_viewport_resize(g_window_width, g_window_height);
				default:
					if(m_gui.update(event))
						break;
					m_screen_manager.event(event);
					break;
				}
			}
		}

		m_screen_manager.clear();
		gui::Style::cleanup();
		delete m_master_renderer;
		delete m_window;
	}

	unsigned Engine::get_screen_width() {
		return g_window_width;
	}

	unsigned Engine::get_screen_height() {
		return g_window_height;
	}
}
