/*
@zgn_name 原生弹窗插件
@zgn_author ZGN
@zgn_version 1.2.0
@zgn_description 按 B 打开控制窗，逐个测试几种 SDK 原生弹窗。
@zgn_mode overlay
*/

#include "zgn_plugin_api.h"

#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

static const ZgnHostApi* g_host = NULL;
static int32_t g_window_open = 0;
static int g_b_was_down = 0;
static char g_status_buffer[256] = "";

static void plugin_log(const char* message)
{
	if (g_host && g_host->log)
	{
		g_host->log("NativePopupPlugin", message);
	}
}

static void set_status(const char* text)
{
	if (!text)
	{
		text = "";
	}

	snprintf(g_status_buffer, sizeof(g_status_buffer), "%s", text);
	plugin_log(text);
}

ZGN_PLUGIN_EXPORT uint32_t ZGN_PLUGIN_CALL ZgnPlugin_GetAbiVersion(void)
{
	return ZGN_PLUGIN_ABI_VERSION;
}

ZGN_PLUGIN_EXPORT const ZgnPluginInfo* ZGN_PLUGIN_CALL ZgnPlugin_GetInfo(void)
{
	static const ZgnPluginInfo info = {
		"原生弹窗插件",
		"ZGN",
		"1.2.0",
		"按 B 打开控制窗，逐个测试几种 SDK 原生弹窗。",
		ZGN_PLUGIN_ABI_VERSION
	};
	return &info;
}

ZGN_PLUGIN_EXPORT int32_t ZGN_PLUGIN_CALL ZgnPlugin_OnLoad(const ZgnHostApi* host_api)
{
	g_host = host_api;
	g_window_open = 0;
	g_b_was_down = 0;
	g_status_buffer[0] = '\0';
	set_status("插件已加载。按 B 打开控制窗。");
	return 1;
}

ZGN_PLUGIN_EXPORT void ZGN_PLUGIN_CALL ZgnPlugin_OnUpdate(void)
{
	const int b_down = (GetAsyncKeyState('B') & 0x8000) != 0;
	if (b_down && !g_b_was_down)
	{
		g_window_open = !g_window_open;
		set_status(g_window_open ? "已按 B 打开控制窗。" : "已按 B 关闭控制窗。");
	}
	g_b_was_down = b_down;
}

ZGN_PLUGIN_EXPORT void ZGN_PLUGIN_CALL ZgnPlugin_OnOverlay(void)
{
	if (!g_host || !g_window_open || !g_host->ui_begin_window_utf8 || !g_host->ui_end_window || !g_host->ui_text_utf8)
	{
		return;
	}

	if (g_host->ui_begin_window_utf8("原生弹窗插件", &g_window_open, 760.0f, 420.0f))
	{
		g_host->ui_text_utf8("这个窗口只是插件控制窗。");
		g_host->ui_text_utf8("你可以在这里逐个点击按钮，测试不同类型的 SDK 原生弹窗。");
		g_host->ui_text_utf8("");

		if (g_host->ui_button_utf8)
		{
			if (g_host->ui_button_utf8("RPG 提示框（单按钮）", 240.0f, 40.0f))
			{
				if (g_host->show_hint_utf8)
				{
					g_host->show_hint_utf8("这是从 C 源码插件里调用出来的游戏原生提示弹窗。");
					set_status("已调用 RPG 提示框（单按钮）。");
				}
				else
				{
					set_status("宿主没有提供原生告示弹窗接口。");
				}
			}

			if (g_host->ui_same_line)
			{
				g_host->ui_same_line();
			}

			if (g_host->ui_button_utf8("RPG 提示框（确定/取消）", 240.0f, 40.0f))
			{
				if (g_host->show_ok_cancel_hint_utf8)
				{
					g_host->show_ok_cancel_hint_utf8("这是 ConfirmDialogUtil 的确定/取消弹窗测试。");
					set_status("已调用 RPG 提示框（确定/取消）。");
				}
				else
				{
					set_status("宿主没有提供 RPG 确定/取消弹窗接口。");
				}
			}

			if (g_host->ui_button_utf8("MiHoYo SDK Confirm", 240.0f, 40.0f))
			{
				if (g_host->show_plugin_confirm_utf8)
				{
					g_host->show_plugin_confirm_utf8("这是 MiHoYo::SDK::Confirm::ShowPluginUI 测试。", "确认", "取消");
					set_status("已调用 MiHoYo SDK Confirm。");
				}
				else
				{
					set_status("宿主没有提供 MiHoYo SDK Confirm 接口。");
				}
			}

			if (g_host->ui_same_line)
			{
				g_host->ui_same_line();
			}

			if (g_host->ui_button_utf8("Joypad Confirm", 240.0f, 40.0f))
			{
				if (g_host->show_joypad_confirm_utf8)
				{
					g_host->show_joypad_confirm_utf8("这是 ConfirmWithJoypad 的原生弹窗测试。", "接受", "返回");
					set_status("已调用 Joypad Confirm。");
				}
				else
				{
					set_status("宿主没有提供 Joypad Confirm 接口。");
				}
			}

			if (g_host->ui_button_utf8("SDKCode 错误弹窗", 240.0f, 40.0f))
			{
				if (g_host->show_sdk_code_dialog)
				{
					g_host->show_sdk_code_dialog(0);
					set_status("已调用 SDKCode 错误弹窗。");
				}
				else
				{
					set_status("宿主没有提供 SDKCode 弹窗接口。");
				}
			}
		}

		g_host->ui_text_utf8("");
		g_host->ui_text_utf8("说明：上面这几个按钮分别对应 RPG.Client、MiHoYo.SDK 和 SDKCode 这几条弹窗链。");
		g_host->ui_text_utf8("最后一个 SDKCode 按钮仍然不是输入框，它只是错误码弹窗演示。");
		g_host->ui_text_utf8("通用原生输入弹窗在当前 SDK 里还没有找到一键入口，后续再继续补。");
		g_host->ui_text_utf8("");

		if (g_status_buffer[0] != '\0')
		{
			char summary_buffer[320];
			snprintf(summary_buffer, sizeof(summary_buffer), "当前状态：%s", g_status_buffer);
			g_host->ui_text_utf8(summary_buffer);
		}
	}

	g_host->ui_end_window();
}

ZGN_PLUGIN_EXPORT void ZGN_PLUGIN_CALL ZgnPlugin_OnUnload(void)
{
	plugin_log("原生弹窗插件已卸载。");
	g_window_open = 0;
	g_b_was_down = 0;
	g_status_buffer[0] = '\0';
	g_host = NULL;
}
