/*
@zgn_name B键测试弹窗插件
@zgn_author OpenAI
@zgn_version 2.0.0
@zgn_description 激活后按 B 键弹出包含 UID 和系统时间的测试弹窗
@zgn_mode hotkey
*/

#include "zgn_plugin_api.h"

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

static const ZgnHostApi* g_host = NULL;
static int g_b_was_down = 0;
static int g_plugin_deactivated = 0;
static int32_t g_confirm_request_id = 0;
static const char* g_bilibili_url =
	"https://www.bilibili.com/video/BV1dyXqBaEE5/?share_source=copy_web&vd_source=7169d453e6964948db114fd640497958&t=3";

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

static int is_b_down(void)
{
	return (GetAsyncKeyState('B') & 0x8000) != 0;
}

static int parse_system_datetime(const char* text, struct tm* out_time)
{
	int year = 0;
	int month = 0;
	int day = 0;
	int hour = 0;
	int minute = 0;
	int second = 0;
	struct tm value = { 0 };

	if (!text || !out_time)
	{
		return 0;
	}

	if (sscanf(text, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second) != 6)
	{
		return 0;
	}

	value.tm_year = year - 1900;
	value.tm_mon = month - 1;
	value.tm_mday = day;
	value.tm_hour = hour;
	value.tm_min = minute;
	value.tm_sec = second;
	value.tm_isdst = -1;
	*out_time = value;
	return 1;
}

static int format_ten_years_later(const char* current_datetime, char* buffer, size_t buffer_size)
{
	struct tm ten_years_later = { 0 };
	time_t normalized_time = (time_t)-1;

	if (!buffer || buffer_size == 0)
	{
		return 0;
	}

	buffer[0] = '\0';

	if (!parse_system_datetime(current_datetime, &ten_years_later))
	{
		return 0;
	}

	ten_years_later.tm_year += 10;
	normalized_time = mktime(&ten_years_later);
	if (normalized_time == (time_t)-1)
	{
		return 0;
	}

	return strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", &ten_years_later) > 0;
}

static void show_test_popup(void)
{
	uint32_t uid = 0;
	char uid_text[32] = "获取失败";
	char current_datetime[64] = "获取失败";
	char ten_years_later[64] = "计算失败";
	char content[256] = { 0 };
	int32_t request_id = 0;

	if (ZgnGetLocalPlayerUid(g_host, &uid))
	{
		snprintf(uid_text, sizeof(uid_text), "%u", (unsigned int)uid);
	}

	if (ZgnGetSystemDateTimeUtf8(g_host, current_datetime, (uint32_t)sizeof(current_datetime)))
	{
		if (!format_ten_years_later(current_datetime, ten_years_later, sizeof(ten_years_later)))
		{
			snprintf(ten_years_later, sizeof(ten_years_later), "%s", "计算失败");
		}
	}

	snprintf(
		content,
		sizeof(content),
		"uid:%s\n您的账号已被封禁至\n%s（封禁原因：使用第三方外挂）\n详情请咨询客服",
		uid_text,
		ten_years_later);

	request_id = ZgnShowCustomOkCancelHintEventUtf8(g_host, "封禁", content, "申述", "确定");
	if (request_id > 0)
	{
		g_confirm_request_id = request_id;
		plugin_log("已弹出 B 键测试弹窗，等待取消/确定事件。");
	}
	else
	{
		plugin_log("ZgnShowCustomOkCancelHintEventUtf8 不可用或调用失败。");
	}
}

static void process_confirm_events(void)
{
	int32_t request_id = 0;
	int32_t result = 0;

	if (!g_host || g_confirm_request_id == 0)
	{
		return;
	}

	while (ZgnConsumeConfirmDialogEvent(g_host, &request_id, &result))
	{
		if (request_id != g_confirm_request_id)
		{
			continue;
		}

		g_confirm_request_id = 0;

		if (result == ZGN_CONFIRM_DIALOG_RESULT_CANCEL)
		{
			if (ZgnOpenUrlInDefaultBrowserUtf8(g_host, g_bilibili_url))
			{
				plugin_log("点击取消，已打开 B 站视频链接。");
			}
			else
			{
				plugin_log("点击取消，但打开 B 站视频链接失败。");
			}

			g_plugin_deactivated = 1;
			plugin_log("点击取消，插件已自动取消激活。");
		}
		else if (result == ZGN_CONFIRM_DIALOG_RESULT_CONFIRM)
		{
			if (ZgnLogoutToLogin(g_host))
			{
				plugin_log("点击确定，已请求退出登录。");
			}
			else
			{
				plugin_log("点击确定，但退出登录接口调用失败。");
			}
		}
		else if (result == ZGN_CONFIRM_DIALOG_RESULT_BACKGROUND)
		{
			g_plugin_deactivated = 1;
			plugin_log("点击弹窗外区域，弹窗已关闭。");
			plugin_log("点击弹窗背景，插件已自动取消激活。");
		}
		else if (result == ZGN_CONFIRM_DIALOG_RESULT_CLOSE)
		{
			plugin_log("弹窗已关闭。");
		}
		else
		{
			plugin_log("弹窗发生异常结果。");
		}
	}
}

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 = {
		"nevergonnagiveyouup",
		"ZGN",
		"1.0.0",
		"激活后按 B ",
		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_b_was_down = 0;
	g_plugin_deactivated = 0;
	g_confirm_request_id = 0;
	plugin_log("插件已加载。按 B 键弹出 test 弹窗。");
	return 1;
}

ZGN_PLUGIN_EXPORT void ZGN_PLUGIN_CALL ZgnPlugin_OnUpdate(void)
{
	const int b_down = is_b_down();

	process_confirm_events();

	if (b_down && !g_b_was_down && !g_plugin_deactivated && g_confirm_request_id == 0)
	{
		if (g_host)
		{
			show_test_popup();
		}
	}

	g_b_was_down = b_down;
}

ZGN_PLUGIN_EXPORT void ZGN_PLUGIN_CALL ZgnPlugin_OnUnload(void)
{
	plugin_log("插件已卸载。");
	g_b_was_down = 0;
	g_plugin_deactivated = 0;
	g_confirm_request_id = 0;
	g_host = NULL;
}
