#include "mbutton.h"

#include <iostream>

#include "geometry/margin.h"
#include "widget/panel_widget/mbox.h"

mbutton::mbutton() {
	color_ = {0.5, 0.5, 0.5, 1};
	slot_.h_alignment(horizontal_alignment_t::center);
	slot_.v_alignment(vertical_alignment_t::center);
}

void mbutton::init() {
	mborder::init();
	set_visibility(visibility_t::visible);
}

void mbutton::on_paint(mirage_paint_context& in_context) {
	in_context.drawer().make_rounded_rect(
		{ 0, 0 },
		in_context.geo().get_local_size(),
		in_context.geo(),
		{ color_ },
		{ 10 }
	);
}

Eigen::Vector2f mbutton::compute_desired_size(float in_layout_scale_multiplier) const {
	const margin_t button_padding = { 10, 10, 10, 10 }; // TODO 临时值, 以后会从主题中获取
	Eigen::Vector2f desired_size = button_padding.get_total_spacing();

	if (const auto& child = get_child_slot().get()) {
		desired_size += child->compute_desired_size(in_layout_scale_multiplier);
	}

	return desired_size;
}

void mbutton::on_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
	mborder::on_click(in_position, in_button);
	std::cout << this << ": Button clicked!" << in_position.x() << ", " << in_position.y() << std::endl;
	color_ = {0, 0, 1, 1};
	// const auto& parent = get_parent();
	// if (std::shared_ptr<mh_box> hbox = std::dynamic_pointer_cast<mh_box>(parent)) {
	// 	hbox->add_child<mbutton>()
	// 	.stretch()
	// 	.margin({ 5 });
	// }
	// if (std::shared_ptr<mv_box> vbox = std::dynamic_pointer_cast<mv_box>(parent)) {
	// 	vbox->add_child<mbutton>()
	// 	.stretch()
	// 	.margin({ 5 });
	// }
	invalidate(invalidate_reason::paint);
}

void mbutton::on_double_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
	mborder::on_double_click(in_position, in_button);
	std::cout << this << ": Button double clicked!" << in_position.x() << ", " << in_position.y() << std::endl;
	color_ = {1, 1, 1, 1};
	invalidate(invalidate_reason::paint);
}

void mbutton::on_mouse_enter() {
	mborder::on_mouse_enter();
	std::cout << this << ": Mouse entered!" << std::endl;
	color_ = {0, 1, 0, 1};
	invalidate(invalidate_reason::paint);
}

void mbutton::on_mouse_leave() {
	mborder::on_mouse_leave();
	std::cout << this << ": Mouse left!" << std::endl;
	color_ = {0.5, 0.5, 0.5, 1};
	invalidate(invalidate_reason::paint);
}

hit_test_handle mbutton::on_mouse_move(const Eigen::Vector2f& in_position) {
	mborder::on_mouse_move(in_position);
	// std::cout << this << ": Mouse moved!" << in_position.x() << ", " << in_position.y() << std::endl;
	color_ = {1, 1, 0, 1};
	invalidate(invalidate_reason::paint);
	return hit_test_handle::handled();
}

hit_test_handle mbutton::on_mouse_button_down(const Eigen::Vector2f& in_position, mouse_button in_button) {
	mborder::on_mouse_button_down(in_position, in_button);
	std::cout << this << ": Mouse pressed!" << in_position.x() << ", " << in_position.y() << std::endl;
	color_ = {0, 0, 1, 1};
	invalidate(invalidate_reason::paint);
	return hit_test_handle::handled();
}

hit_test_handle mbutton::on_mouse_button_up(const Eigen::Vector2f& in_position, mouse_button in_button) {
	mborder::on_mouse_button_up(in_position, in_button);
	std::cout << this << ": Mouse released!" << in_position.x() << ", " << in_position.y() << std::endl;
	color_ = {1, 0, 0, 1};
	invalidate(invalidate_reason::paint);
	return hit_test_handle::handled();
}

hit_test_handle mbutton::on_mouse_wheel(const Eigen::Vector2f& in_position, const wheel_event& in_delta) {
	mborder::on_mouse_wheel(in_position, in_delta);
	std::cout << this << ": Mouse wheeled!" << in_position.x() << ", " << in_position.y() << std::endl;
	color_ = {0, 1, 1, 1};
	invalidate(invalidate_reason::paint);
	return hit_test_handle::handled();
}
