/*
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/gui/style.hpp"

#include <algorithm>

#include "common/xml/parser.hpp"
#include "common/xml/xml_node.hpp"
#include "common/logger.hpp"
#include "game/gui/style_change_listener.hpp"

using namespace polygun::gui;

Style* Style::s_instance = nullptr;

Style::Style() :
	m_text_color(255, 255, 255, 255),
	m_inactive_text_color(36, 56, 54, 255),
	m_highlight_color(0, 0, 0, 255),
	m_background_color(39, 59, 86, 255),
	m_lower_background_color(19, 39, 65, 255),
	m_font(),
	m_extrusion_size(2),
	m_change_listeners()
{}

bool Style::load_from_file(const std::string& path) {
	xml::XMLNode* node;
	if(!xml::parse_file(path, node))
		return false;

	if(node->get_name()!="style") {
		LOG_ERROR("Style '%s' is invalid: root node is not 'style'", path.c_str());
		delete node;
		return false;
	}

	load_text_style(node);
	load_misc(node);
	load_font(node);

	delete node;

	for(StyleChangeListener* listener : m_change_listeners)
		listener->on_style_change();

	return true;
}

void Style::add_change_listener(StyleChangeListener* listener) {
	m_change_listeners.push_back(listener);
}

void Style::erase_change_listener(StyleChangeListener* listener) {
	std::vector<StyleChangeListener*>::iterator it = std::find(m_change_listeners.begin(), m_change_listeners.end(), listener);
	if(it!=m_change_listeners.end())
		m_change_listeners.erase(it);
}

void Style::create() {
	if(s_instance)
		cleanup();
	s_instance = new Style;
}

void Style::cleanup() {
	delete s_instance;
	s_instance = nullptr;
}

void Style::load_text_style(xml::XMLNode* node) {
	xml::XMLNode* text = node->next_node("text");
	if(!text)
		return;
	xml::XMLNode* normal = text->next_node("normal");
	xml::XMLNode* inactive = text->next_node("inactive");
	if(normal)
		parse_color(normal, m_text_color);
	if(inactive)
		parse_color(inactive, m_inactive_text_color);
}

void Style::load_misc(xml::XMLNode* node) {
	xml::XMLNode* misc = node->next_node("misc");
	if(!misc)
		return;
	xml::XMLNode* highlight = misc->next_node("highlight");
	if(highlight)
		parse_color(highlight, m_highlight_color);
	xml::XMLNode* background = misc->next_node("background");
	if(background)
		parse_color(background, m_background_color);
	xml::XMLNode* extrusion = misc->next_node("extrusion");
	if(extrusion)
		m_extrusion_size = std::stoi(extrusion->get_text());
}

void Style::load_font(xml::XMLNode* node) {
	xml::XMLNode* font = node->next_node("font");
	if(!font)
		return;
	m_font.load_from_file(font->get_text());
}

void Style::parse_color(const xml::XMLNode* node, glm::vec4ub& output) {
	std::vector<unsigned char> components;
	std::string element;
	for(char ch : node->get_text()) {
		if(ch==';') {
			if(!element.empty()) {
				components.push_back(std::stoi(element));
				element.clear();
			}
		}
		else
			element+=ch;
	}
	if(!element.empty())
		components.push_back(std::stoi(element));

	if(components.size()<3 || components.size()>4) {
		LOG_ERROR("Color '%s' is invalid", node->get_text().c_str());
		return;
	}

	output.r = components[0];
	output.g = components[1];
	output.b = components[2];
	output.a = components.size()==4?components[3]:255;
}

glm::vec4ub polygun::gui::add_to_color(const glm::vec4ub& target, const glm::vec4ub& val) {
	int r = static_cast<int>(target.r)+static_cast<int>(val.r);
	int g = static_cast<int>(target.g)+static_cast<int>(val.g);
	int b = static_cast<int>(target.b)+static_cast<int>(val.b);
	int a = static_cast<int>(target.a)+static_cast<int>(val.a);
	return glm::vec4ub(std::max(std::min(r, 255), 0), std::max(std::min(g, 255), 0), std::max(std::min(b, 255), 0), std::max(std::min(a, 255), 0));
}

glm::vec4ub polygun::gui::subtract_from_color(const glm::vec4ub& target, const glm::vec4ub& val) {
	int r = static_cast<int>(target.r)-static_cast<int>(val.r);
	int g = static_cast<int>(target.g)-static_cast<int>(val.g);
	int b = static_cast<int>(target.b)-static_cast<int>(val.b);
	int a = static_cast<int>(target.a)-static_cast<int>(val.a);
	return glm::vec4ub(std::max(std::min(r, 255), 0), std::max(std::min(g, 255), 0), std::max(std::min(b, 255), 0), std::max(std::min(a, 255), 0));
}
