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

#include "game/screens/main_menu_screen.hpp"

#include "game/engine/engine.hpp"
#include "game/renderer/texture.hpp"
#include "game/screens/game_session_screen.hpp"
#include "game/gui/button.hpp"
#include "game/gui/text_edit.hpp"
#include "common/locale/util.hpp"

using namespace polygun::screens;

void MainMenuScreen::begin() {
	logo_texture = m_engine->get_master_renderer()->create_texture();
	logo_texture->load_from_file("res/gui/polygun_header.png");

	gui::GUIMaster& gui_master = m_engine->get_gui_master();
	gui_master.load_from_file("res/gui/main_menu.xml", m_engine);
	gui::Button* const button_join = gui_master.get_component_by_id<gui::Button>("button_join");
	assert(button_join);
	button_join->set_callback(&MainMenuScreen::button_join_callback, reinterpret_cast<void*>(this));
	gui::Button* const button_host = gui_master.get_component_by_id<gui::Button>("button_host");
	assert(button_host);
#if defined(BUILD_SERVER)
	button_host->set_caption(_("Host"));
	button_host->set_callback(&MainMenuScreen::button_host_callback, reinterpret_cast<void*>(this));
#else
	button_host->set_caption(_("Host (server not compiled in)"));
#endif
}

void MainMenuScreen::render() {
	m_engine->get_master_renderer()->clear_screen(0, 0, 0);
	m_engine->get_gui_renderer()->render_texture(glm::vec2(-170,-180), logo_texture, glm::vec2(12,15));
}

void MainMenuScreen::update(double delta) {}

void MainMenuScreen::event(const window::Event& ev) {}

void MainMenuScreen::finish() {
	delete logo_texture;
	m_engine->get_gui_master().clear();
}

void MainMenuScreen::button_join_callback(void* screen) {
	MainMenuScreen* main_menu_screen = reinterpret_cast<MainMenuScreen*>(screen);
	gui::GUIMaster& gui_master = main_menu_screen->m_engine->get_gui_master();
	gui::TextEdit* const text_edit_nickname = gui_master.get_component_by_id<gui::TextEdit>("text_edit_nickname");
	assert(text_edit_nickname);
	gui::TextEdit* const text_edit_server = gui_master.get_component_by_id<gui::TextEdit>("text_edit_server");
	assert(text_edit_server);
	gui::TextEdit* const text_edit_port = gui_master.get_component_by_id<gui::TextEdit>("text_edit_port");
	assert(text_edit_port);
	std::unique_ptr<GameSessionScreen> game_session_screen = std::make_unique<GameSessionScreen>(text_edit_server->get_text_buffer(),
		std::stoi(text_edit_port->get_text_buffer()),
		text_edit_nickname->get_text_buffer()
	);
	main_menu_screen->m_engine->get_screen_manager().push_screen(std::move(game_session_screen));
}

#if defined(BUILD_SERVER)
void MainMenuScreen::button_host_callback(void* screen) {
	MainMenuScreen* main_menu_screen = reinterpret_cast<MainMenuScreen*>(screen);
	gui::GUIMaster& gui_master = main_menu_screen->m_engine->get_gui_master();
	gui::TextEdit* const text_edit_nickname = gui_master.get_component_by_id<gui::TextEdit>("text_edit_nickname");
	assert(text_edit_nickname);
	gui::TextEdit* const text_edit_port = gui_master.get_component_by_id<gui::TextEdit>("text_edit_port");
	assert(text_edit_port);
	std::unique_ptr<GameSessionScreen> game_session_screen = std::make_unique<GameSessionScreen>("127.0.0.1", std::stoi(text_edit_port->get_text_buffer()),
		text_edit_nickname->get_text_buffer(),
		true
	);
	main_menu_screen->m_engine->get_screen_manager().push_screen(std::move(game_session_screen));
}
#endif
