#include "mbutton.h"

#include <iostream>

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

mbutton::mbutton() {
	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::setup_widget(const button_args& in_args) {
	normal_color_ = in_args.normal_color();
    hover_color_   = in_args.hover_color();
    pressed_color_ = in_args.pressed_color();

    color_ = normal_color_;
    slot_.h_alignment(horizontal_alignment_t::center);
    slot_.v_alignment(vertical_alignment_t::center);
    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 }
	);
}

void mbutton::on_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
	mborder::on_click(in_position, in_button);
	std::println(std::cout, "点击 Button: {}, {}", in_position.x(), in_position.y());
	color_ = hover_color_;
	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::println(std::cout, "双击 Button: {}, {}", in_position.x(), in_position.y());
	invalidate(invalidate_reason::paint);
}

void mbutton::on_mouse_enter() {
	mborder::on_mouse_enter();
	std::println(std::cout, "鼠标进入 Button");
	color_ = hover_color_;
	invalidate(invalidate_reason::paint);
}

void mbutton::on_mouse_leave() {
	mborder::on_mouse_leave();
	std::println(std::cout, "鼠标离开 Button");
	color_ = normal_color_;
	invalidate(invalidate_reason::paint);
}

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::println(std::cout, "鼠标按下 Button: {}, {}", in_position.x(), in_position.y());
	color_ = pressed_color_;
	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::println(std::cout, "鼠标释放 Button: {}, {}", in_position.x(), in_position.y());
	color_ = hover_color_;
	invalidate(invalidate_reason::paint);
	return hit_test_handle::handled();
}
