#include <iostream>

#include "mirage.h"
#include "window/mwindow.h"
#include "font/font_system.h"
#include "style/mirage_style.h"
#include "widget/widget_new.h"
#include "widget/compound_widget/mbutton.h"
#include "widget/leaf_widget/mtext_block.h"
#include "widget/panel_widget/mbox.h"

#include "utf8.h"
#include "widget/panel_widget/moverlay.h"

void test_color() {
	const char* test_cases[] = {
		"#FFF",                     // hex rgb
		"#ff0000",                  // hex RRGGBB
		"#00ff0080",                // hex RRGGBBAA
		"#1234",                    // hex RGBA
		"rgb(255,0,0)",             // rgb int
		"rgba(0,255,0,128)",        // rgba int
		"rgba(0, 0, 255, 255)",     // rgba int with spaces
		"rgb(1.0, 0.0, 0.0)",       // rgb float
		"rgba(0.0, 1.0, 0.0, 0.5)", // rgba float
		" rgb(0.0, 0.0, 1.0) ",     // rgb float with spaces
		"rgba(1, 0.5, 0, 1.0)",     // rgba mixed - should parse as float
		"invalid",
		"#12345",
		"rgb(300,0,0)",
		"rgba(1.1, 0, 0, 1)",
		"rgba(100,100,100)" // missing alpha
	};

	for (const char* test_str: test_cases) {
		std::optional<linear_color> color = linear_color::from_string(test_str);
		std::println(std::cout, "Parsing '{}': ", test_str);
		if (color) {
			std::println(std::cout, "Success -> r:{}, g:{}, b:{}, a:{}", color->r, color->g, color->b, color->a);
		}
		else { std::println(std::cerr, "Failed to parse color string: {}", test_str); }
	}
}

int main(int argc, char* argv[]) {
	mirage_app::get().init();

	font_manager::instance().load_default_font();

	auto name        = mirage_style::get().name();
	auto version     = mirage_style::get().version();
	auto author      = mirage_style::get().author();
	auto description = mirage_style::get().description();
	auto license     = mirage_style::get().license();

	std::stringstream ss;
	ss << "name: " << name << "\n";
	ss << "version: " << version << "\n";
	ss << "author: " << author << "\n";
	ss << "description: " << description << "\n";
	ss << "license: " << license;

	// const char*转换为std::u32string
	const auto& config_info_str = utf8::utf8to32(ss.str());

	const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
	window->set_content(
			mnew(mv_box)
			[
				mslot(mv_box)
				.horizontal_alignment(horizontal_alignment_t::left)
				+ mnew(mbutton)
				[
					mslot(mbutton)
					.margin({ 10 })
					.visibility(visibility_t::visible)
					[
						mnew(mtext_block,
						     .text(config_info_str)
						     .font_size(15)
								)
					]
				],

				mslot(mv_box)
				.horizontal_alignment(horizontal_alignment_t::right)
				+ mnew(mbutton)
				[
					mslot(mbutton)
					.margin({ 10 })
					.visibility(visibility_t::visible)
					[
						mnew(mtext_block,
						     .text(U"Hello, World! 你好，世界!\n换行测试1111，测试测试测试测试,测试测试😀🐵🙏 😃🐵🙏")
						     // .text(U"😀🐵🙏😀🐵🙏")
						     .font_size(15)
						     .warp_text(true)
							)
					]
				]
			]
			);

	const auto& window2 = mwindow::create({ 800, 600 }, L"Hello, World!");
	window2->set_content(
		mnew(moverlay)
		[
			mslot(moverlay)
			.h_alignment(horizontal_alignment_t::center)
			.v_alignment(vertical_alignment_t::center)
			+mnew(mbutton)
			[
				mslot(mbutton)
				.margin({ 10 })
				[
					mnew(mtext_block,
						.text(U"测试测试")
						.font_size(24))
				]
			],

			mslot(moverlay)
			.h_alignment(horizontal_alignment_t::center)
			.v_alignment(vertical_alignment_t::center)
			+mnew(mbutton)
			[
				mslot(mbutton)
				.margin({ 10 })
				[
					mnew(mtext_block,
						.text(U"测试测试21111")
						.font_size(15))
				]
			]
		]
	);

	mirage_app::get().run();
	return 0;
}
