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

using namespace polygun::renderer;

static int sizex = SCR_WIDTH, sizey = SCR_HEIGHT;

namespace polygun::engine {
	static Engine* g_current_engine = nullptr;
	static void on_framebuffer_resize(int width, int height) {
		sizex = width;
		sizey = height;
		g_current_engine->get_gui_renderer()->on_viewport_resize(width, height);
	}

	Engine::Engine() :
		m_screen_manager(*this),
		m_window(nullptr),
		m_master_renderer(new renderer::MasterRenderer)
	{}

	void Engine::init(DirectJoinArguments* direct_join) {
		g_current_engine = this;

		// TODO: Load requested renderer from config or determine by platform
		renderer::RendererType renderer_type = renderer::RendererType::RENDERER_TYPE_GL;
		m_window = window::Window::create_window("PolyGun", SCR_WIDTH, SCR_HEIGHT, renderer_type);
		m_master_renderer->init(renderer_type, m_window);

		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_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:
					on_framebuffer_resize(event.m_data.m_window_event.m_width, event.m_data.m_window_event.m_height);
				default:
					m_screen_manager.event(event);
					break;
				}
			}
		}

		delete m_master_renderer;
		delete m_window;
	}

	int Engine::get_screen_width() {
		return sizex;
	}

	int Engine::get_screen_height() {
		return sizey;
	}
}
