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

#include "game/engine/engine.hpp"
#include "game/gui/style.hpp"
#include "game/renderer/texture.hpp"
#include "game/window/window.hpp"
#include "common/xml/reader.hpp"
#include "common/locale/util.hpp"

using namespace polygun::gui;

Button::Button(engine::Engine* engine, Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, const ButtonProp& prop) :
	Component(x, y, width, height, id),
	StyleChangeListener(),
	m_engine(engine),
	m_properties(prop),
	m_caption_texture(engine->get_master_renderer()->create_texture()),
	m_pressed(false)
{
	set_caption(prop.m_caption);
}

Button::Button(engine::Engine* engine, Container* parent, xml::XMLNode* node) :
	Component(node),
	StyleChangeListener(),
	m_engine(engine),
	m_properties(),
	m_caption_texture(engine->get_master_renderer()->create_texture()),
	m_pressed(false)
{
	set_parent(parent);
	xml::XMLReader reader(node);
	std::string val;
	if(reader.get_attribute("text", val))
		set_caption(val);
	else if(reader.get_attribute("tr_text", val))
		set_caption(_(val));
	else
		set_caption("");
}

Button::~Button() {
	delete m_caption_texture;
}

void Button::set_caption(const std::string& caption) {
	const glm::vec4ub color = m_inactive?Style::get().m_inactive_text_color:Style::get().m_text_color;
	std::unique_ptr<renderer::Surface> surf = Style::get().m_font.render_text_in_area(glm::vec2(get_width()-3, get_height()), caption, 15, color);
	m_caption_texture->load_from_surface(*surf);
	m_properties.m_caption = caption;
}

void Button::set_callback(void (*callback)(void*), void* callback_data) {
	m_properties.m_callback = callback;
	m_properties.m_callback_data = callback_data;
}

void Button::set_inactive(bool inactive) {
	m_inactive = inactive;
	set_caption(m_properties.m_caption);
}

void Button::render(renderer::GUIRenderer* renderer) {
	const float width = std::min(m_parent->get_right(), get_right())-get_x();
	const float height = std::min(m_parent->get_bottom(), get_bottom())-get_y();
	const unsigned extrusion = Style::get().m_extrusion_size;
	renderer->fill_rect(glm::vec2(get_x()+extrusion, get_y()+extrusion), glm::vec2(width-extrusion, height-extrusion), subtract_from_color(Style::get().m_background_color, glm::vec4ub(30, 30, 30, 0)));
	if(m_highlighted)
		renderer->fill_rect(glm::vec2(get_x()-1, get_y()-1), glm::vec2(width+3, height+3), Style::get().m_highlight_color);
	renderer->fill_rect(glm::vec2(get_x(), get_y()), glm::vec2(width-extrusion, height-extrusion), add_to_color(Style::get().m_background_color, glm::vec4ub(100, 100, 100, 0)));
	renderer->fill_rect(glm::vec2(get_x()+extrusion, get_y()+extrusion), glm::vec2(width-extrusion*2, height-extrusion*2), subtract_from_color(Style::get().m_background_color, m_pressed?glm::vec4ub(30, 30, 30, 0):glm::vec4ub(0, 0, 0, 0)));
	renderer->render_texture(glm::vec2(get_x()+3, get_y()+(height-m_caption_texture->get_height())/2), m_caption_texture);
}

bool Button::update(const window::Event& event) {
	const float width = std::min(m_parent->get_right(), get_right())-get_x();
	const float height = std::min(m_parent->get_bottom(), get_bottom())-get_y();
	switch(event.m_type) {
	case window::EventType::EVENT_TYPE_MOUSE_BUTTON_UP:
		if(event.m_data.m_mouse_event.m_button==window::MouseButton::MOUSE_BUTTON_LEFT && m_pressed) {
			if(m_engine->get_window()->is_mouse_inside_area(glm::vec2(get_x(), get_y()), glm::vec2(width, height)) && m_properties.m_callback)
				m_properties.m_callback(m_properties.m_callback_data);
			m_pressed = false;
			return true;
		}
		break;
	case window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN:
		if(m_engine->get_window()->is_mouse_inside_area(glm::vec2(get_x(), get_y()), glm::vec2(width, height)) &&
			event.m_data.m_mouse_event.m_button==window::MouseButton::MOUSE_BUTTON_LEFT && !m_inactive) {
			m_engine->get_gui_master().request_highlight(this);
			m_pressed = true;
			return true;
		}
		break;
	case window::EventType::EVENT_TYPE_KEY_DOWN:
		if(m_highlighted && !m_inactive && event.m_data.m_key_event.m_key==window::KeyCode::KEY_SPACE) {
			m_pressed = true;
			if(m_properties.m_callback)
				m_properties.m_callback(m_properties.m_callback_data);
			return true;
		}
		break;
	case window::EventType::EVENT_TYPE_KEY_UP:
		if(event.m_data.m_key_event.m_key==window::KeyCode::KEY_SPACE) {
			m_pressed = false;
			return true;
		}
	default:
		break;
	}
	return false;
}

void Button::on_style_change() {
	set_caption(m_properties.m_caption);
}
