#include <iostream>
#include <Windows.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <wrl/client.h>
#include "bkg.h"
#include "Utils/Utils.h"
#include "Utils/H264Encoder.h"

#include "ImGuiDatePicker.h"
#include "ImHelper.h"

#include "DXGIDuplicator.h"

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dcompiler.lib")

//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
ID3D11ShaderResourceView* image = NULL;
ID3D11Texture2D* texture2D = NULL;

DXGIDuplicator* duplicator = NULL;
std::string UserName = "";
enum class TabClass
{
	KeySerach,
	KeyCreater,
	KeySerachEx,
	KeyTypes,
	UserInfo,

};
#define _SL ImGui::SameLine()
#define u8s(x) reinterpret_cast<const char*>(u8##x)
HWND hwnd;
ID3D11Device* g_pd3dDevice = NULL;
ID3D11DeviceContext* g_pImmediateContext = NULL;
ID3D11RenderTargetView* g_pRenderTargetView = NULL;
IDXGISwapChain* g_pSwapChain = NULL;
void InitD3D(HWND hWnd) {
	DXGI_SWAP_CHAIN_DESC sd;
	ZeroMemory(&sd, sizeof(sd));
	sd.BufferCount = 1;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.OutputWindow = hWnd;
	sd.SampleDesc.Count = 1;
	sd.Windowed = TRUE;

	D3D_FEATURE_LEVEL featureLevel;
	const D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 };

	D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, featureLevels, 1,
		D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pImmediateContext);
}
void InitImGui() {
	IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGuiIO& io = ImGui::GetIO();
	(void)io;
	ImGui_ImplWin32_Init(hwnd);
	ImGui_ImplDX11_Init(g_pd3dDevice, g_pImmediateContext);
	ImGui_ImplDX11_CreateDeviceObjects();
	ImGui::GetIO().ImeWindowHandle = hwnd;
}
void CleanupImGui() {
	ImGui_ImplDX11_Shutdown();
	ImGui_ImplWin32_Shutdown();
	ImGui::DestroyContext();
}
TabClass CurrentTab = TabClass::KeySerach;
void LoginFrame(SIZE windowSize)
{
	HRESULT hr = duplicator->GetDesktopFrame(texture2D);
	if (SUCCEEDED(hr))
	{
		if (image)
		{
			image->Release();
			image = NULL;
		}
		//auto cnv = duplicator->ConvertFormat(texture2D, DXGI_FORMAT_B8G8R8A8_UNORM);
		//auto bytes = duplicator->GenerateBitmapFile(cnv);
		//File::WriteAllBytes("I:\\bmp.bmp", bytes);
		//cnv->Release();
		image = LoadImageFromTexture(texture2D);
		texture2D->Release();
	}
	if (image)
	{
		const int ln = 1;
		for (int x = 0; x < ln; x++)
		{
			for (int y = 0; y < ln; y++)
			{
				ImGui::SetCursorPos(ImVec2(((float)windowSize.cx / (float)ln) * (float)x, ((float)windowSize.cy / (float)ln) * (float)y));
				ImGui::Image(image, ImVec2((float)windowSize.cx / (float)ln, (float)windowSize.cy / (float)ln),
					ImVec2(0, 0),
					ImVec2(1, 1),
					ImVec4(1, 1, 1, 1),
					ImVec4(1, 0, 0, 1));
			}
		}
	}
	static char uname[0x1000]{};
	static char pwd[0x1000]{};

	const int input_width = 250;
	ImGui::SetCursorPosY(windowSize.cy * 0.3f);
	ImGui::SetCursorPosX((windowSize.cx - input_width) / 2.0f);
	ImGui::Text(u8s("账号"));

	ImGui::PushItemWidth((windowSize.cx - 15) * 0.5f);
	ImGui::SetCursorPosX((windowSize.cx - input_width) / 2.0f);
	ImGui::SetNextItemWidth(input_width);
	ImGui::InputText(u8s("##uname"), uname, 0x1000);

	ImGui::PopItemWidth();

	ImGui::SetCursorPosX((windowSize.cx - input_width) / 2.0f);
	ImGui::Text(u8s("密码"));

	ImGui::PushItemWidth((windowSize.cx - 15) * 0.5f);
	ImGui::SetCursorPosX((windowSize.cx - input_width) / 2.0f);
	ImGui::SetNextItemWidth(input_width);
	ImGui::InputText(u8s("##pwd"), pwd, 0x1000, ImGuiInputTextFlags_Password);

	ImGui::PopItemWidth();

	ImGui::SetCursorPosX((windowSize.cx - ImGui::CalcTextSize(u8s("登录")).x) / 2.0f);
	if (ImGui::Button(u8s("登录")))
	{
		UserName = uname;
	}
}
void KeyCreateFrame(SIZE windowSize)
{
	_SL;
	static int cur_create_cardType = 0;
	static int cur_create_card_num = 1;
	static char input_info[0x100]{};
	static std::vector<std::string> views_create_cardType =
	{
		u8s("测试1"),
		u8s("测试2"),
		u8s("测试3"),
	};
	if (cur_create_card_num < 1)
		cur_create_card_num = 1;
	ImGui::Text(u8s("类型"));
	ImGui::SetNextItemWidth(150);
	_SL;
	if (ImGui::BeginCombo("##create_cardType", views_create_cardType[cur_create_cardType].c_str()))
	{
		for (int n = 0; n < views_create_cardType.size(); n++) {
			bool is_selected = (cur_create_cardType == n);
			if (ImGui::Selectable(views_create_cardType[n].c_str(), is_selected)) {
				cur_create_cardType = n;
			}
			if (is_selected) {
				ImGui::SetItemDefaultFocus();
			}
		}
		ImGui::EndCombo();
	}
	_SL;
	ImGui::Text(u8s("数量"));
	_SL;
	ImGui::SetNextItemWidth(100);
	ImGui::InputInt("##input_create_card_num", &cur_create_card_num);
	_SL;
	ImGui::Text(u8s("备注"));
	_SL;
	ImGui::SetNextItemWidth(250);
	ImGui::InputText("##input_create_card_info", input_info, 0x100);
	_SL;
	if (ImGui::Button(u8s("生成##bt_create_card")))
	{
	}
	_SL;
	if (ImGui::Button(u8s("复制##bt_create_card_copy")))
	{
	}
	if (ImGui::BeginTable(u8s("##keyCreateTable"), 1, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable))
	{
		ImGui::TableSetupColumn(u8s("##table_card_create"), ImGuiTableColumnFlags_WidthFixed);
		for (int i = 0; i < 100; i++)
		{
			ImGui::TableNextRow();

			ImGui::TableSetColumnIndex(0);
			ImGui::Text("XXXXXX-XXXXXXXX-XXXXXXXX");
		}
		ImGui::EndTable();
	}
}
void KeySerachFrame(SIZE windowSize)
{
	static bool _checked = false;
	static char text[0x10000]{};
	ImGui::Text(u8s("卡密(一行一条)"));
	ImGui::InputTextMultiline(u8s("##l1"), text, 0x10000, ImVec2((windowSize.cx - 15), windowSize.cy * 0.25f));
	if (ImGui::Button(u8s("查询##serach"))) {
	}
	ImGui::BeginChild(u8s("##keyList"), ImVec2(windowSize.cx - 15, windowSize.cy * 0.5f), true, ImGuiWindowFlags_HorizontalScrollbar);
	{
		if (ImGui::BeginTable(u8s("##keySerachTable"), 12, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable))
		{
			ImGui::TableSetupColumn(u8s("##table_sel"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("卡密"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("类型"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("创建时间"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("启用时间"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("到期时间"), ImGuiTableColumnFlags_WidthFixed);

			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);

			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableHeadersRow();
			for (int i = 0; i < 100; i++)
			{
				ImGui::TableNextRow();

				ImGui::TableSetColumnIndex(0);
				ImGui::Checkbox(StringHelper::Format(u8s("##ck%d"), i).c_str(), &_checked);

				ImGui::TableSetColumnIndex(1);
				ImGui::Text(u8s("卡密"));

				ImGui::TableSetColumnIndex(2);
				ImGui::Text(u8s("周卡"));

				ImGui::TableSetColumnIndex(3);
				ImGui::Text(u8s("2024-8-24 17:45:05"));

				ImGui::TableSetColumnIndex(4);
				ImGui::Text(u8s("2024-8-24 17:45:05"));

				ImGui::TableSetColumnIndex(5);
				ImGui::Text(u8s("2024-8-24 17:45:05"));

				ImGui::TableSetColumnIndex(6);
				if (ImGui::Button(StringHelper::Format(u8s("复制##copy_%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}
				ImGui::TableSetColumnIndex(7);
				if (ImGui::Button(StringHelper::Format(u8s("复制一行##copy_line_%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}

				ImGui::TableSetColumnIndex(8);
				if (ImGui::Button(StringHelper::Format(u8s("解绑##unbind_%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}

				ImGui::TableSetColumnIndex(9);
				if (ImGui::Button(StringHelper::Format(u8s("禁用##disable_%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}

				ImGui::TableSetColumnIndex(10);
				if (ImGui::Button(StringHelper::Format(u8s("启用##enable_%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}

				ImGui::TableSetColumnIndex(11);
				if (ImGui::Button(StringHelper::Format(u8s("删除##delete_%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}
			}
			ImGui::EndTable();
		}

		ImGui::EndChild();
		if (ImGui::Button(u8s("全选##keyList_all_sel")))
		{
		}
		_SL;
		if (ImGui::Button(u8s("全不选##keyList_cl_sel")))
		{
		}
		static int addTime = 0;
		ImGui::SetNextItemWidth(120);
		ImGui::InputInt(u8s("##keyList_in_time"), &addTime);
		static int cur_unit = 1;
		const char* views[] =
		{
			u8s("分"),
			u8s("时"),
			u8s("天"),
		};
		_SL; ImGui::SetNextItemWidth(60);
		if (ImGui::BeginCombo("##add_time_unit", views[cur_unit])) {
			for (int n = 0; n < 3; n++) {
				bool is_selected = (cur_unit == n);
				if (ImGui::Selectable(views[n], is_selected)) {
					cur_unit = n;
				}
				if (is_selected) {
					ImGui::SetItemDefaultFocus();
				}
			}
			ImGui::EndCombo();
		}
		_SL;
		if (ImGui::Button(u8s("批量加时##keyList_add_time")))
		{
			uint64_t time_of_minutes = addTime * (
				cur_unit == 0 ? 1 :
				cur_unit == 1 ? 60 :
				cur_unit == 2 ? 60 * 24 :
				0
				);
		}
		if (ImGui::Button(u8s("启用##keyList_enable")))
		{
		}
		_SL;
		if (ImGui::Button(u8s("禁用##keyList_disable")))
		{
		}
		_SL;
		if (ImGui::Button(u8s("解绑##keyList_unbind")))
		{
		}
		_SL;
		if (ImGui::Button(u8s("删除##keyList_delete")))
		{
		}
	}
}
void KeySerachFrameEx(SIZE windowSize)
{
	static tm from_time;
	static tm to_time;
	if (from_time.tm_year == 0)
	{
		std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
		std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
		gmtime_s(&from_time, &currentTime);
		gmtime_s(&to_time, &currentTime);
	}
	ImGui::Text(u8s("时间范围:"));

	static int cur_sel_time_type = 0;
	static std::vector<std::string> views_sel_time_type =
	{
		u8s("不限"),
		u8s("创建时间"),
		u8s("启用时间"),
		u8s("到期时间"),
	};
	ImGui::SetNextItemWidth(100);
	_SL;
	if (ImGui::BeginCombo("##sel_time_type", views_sel_time_type[cur_sel_time_type].c_str()))
	{
		for (int n = 0; n < views_sel_time_type.size(); n++) {
			bool is_selected = (cur_sel_time_type == n);
			if (ImGui::Selectable(views_sel_time_type[n].c_str(), is_selected)) {
				cur_sel_time_type = n;
			}
			if (is_selected) {
				ImGui::SetItemDefaultFocus();
			}
		}
		ImGui::EndCombo();
	}


	_SL; ImGui::Text(u8s("从"));

	ImGui::SetNextItemWidth(200);
	_SL; ImGui::DatePicker("##pick_date_from", from_time);

	_SL; ImGui::Text(u8s("到"));

	ImGui::SetNextItemWidth(200);
	_SL; ImGui::DatePicker("##pick_date_to", to_time);

	{
		static int cur_sel_card_type = 0;
		static std::vector<std::string> views_sel_card_type =
		{
			u8s("全部"),
			u8s("周卡"),
			u8s("月卡"),
			u8s("年卡"),
		};
		ImGui::Text(u8s("卡密类型:"));
		ImGui::SetNextItemWidth(100);
		_SL;
		if (ImGui::BeginCombo("##sel_card_type", views_sel_card_type[cur_sel_card_type].c_str()))
		{
			for (int n = 0; n < views_sel_card_type.size(); n++) {
				bool is_selected = (cur_sel_card_type == n);
				if (ImGui::Selectable(views_sel_card_type[n].c_str(), is_selected)) {
					cur_sel_card_type = n;
				}
				if (is_selected) {
					ImGui::SetItemDefaultFocus();
				}
			}
			ImGui::EndCombo();
		}
	}
	{
		_SL;
		static int cur_sel_card_state = 0;
		static std::vector<std::string> views_sel_card_state =
		{
			u8s("全部"),
			u8s("启用"),
			u8s("禁用"),
		};
		ImGui::Text(u8s("禁用状态:"));
		ImGui::SetNextItemWidth(100);
		_SL;
		if (ImGui::BeginCombo("##sel_card_state", views_sel_card_state[cur_sel_card_state].c_str()))
		{
			for (int n = 0; n < views_sel_card_state.size(); n++) {
				bool is_selected = (cur_sel_card_state == n);
				if (ImGui::Selectable(views_sel_card_state[n].c_str(), is_selected)) {
					cur_sel_card_state = n;
				}
				if (is_selected) {
					ImGui::SetItemDefaultFocus();
				}
			}
			ImGui::EndCombo();
		}
	}
	{
		_SL;
		static int cur_sel_card_activate = 0;
		static std::vector<std::string> views_sel_card_activate =
		{
			u8s("全部"),
			u8s("已激活"),
			u8s("未激活"),
		};
		ImGui::Text(u8s("激活状态:"));
		ImGui::SetNextItemWidth(100);
		_SL;
		if (ImGui::BeginCombo("##sel_card_activate", views_sel_card_activate[cur_sel_card_activate].c_str()))
		{
			for (int n = 0; n < views_sel_card_activate.size(); n++) {
				bool is_selected = (cur_sel_card_activate == n);
				if (ImGui::Selectable(views_sel_card_activate[n].c_str(), is_selected)) {
					cur_sel_card_activate = n;
				}
				if (is_selected) {
					ImGui::SetItemDefaultFocus();
				}
			}
			ImGui::EndCombo();
		}
	}

	{
		static char text[0x10000]{};
		ImGui::Text(u8s("备注信息:"));
		ImGui::SetNextItemWidth(200);
		_SL; ImGui::InputText(u8s("##input_info"), text, 0x10000);

		static int cur_sel_card_owner = 0;
		static std::vector<std::string> views_sel_card_owner =
		{
			u8s("当前账户"),
			u8s("当前账户及所有下级"),
		};
		_SL; ImGui::Text(u8s("所属账户:"));
		ImGui::SetNextItemWidth(200);
		_SL;
		if (ImGui::BeginCombo("##sel_card_owner", views_sel_card_owner[cur_sel_card_owner].c_str()))
		{
			for (int n = 0; n < views_sel_card_owner.size(); n++) {
				bool is_selected = (cur_sel_card_owner == n);
				if (ImGui::Selectable(views_sel_card_owner[n].c_str(), is_selected)) {
					cur_sel_card_owner = n;
				}
				if (is_selected) {
					ImGui::SetItemDefaultFocus();
				}
			}
			ImGui::EndCombo();
		}

	}
	if (ImGui::Button(u8s("查询##serachex"))) {
	}
	if (ImGui::BeginTable(u8s("##keySerachTableEx"), 9, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable))
	{
		ImGui::TableSetupColumn(u8s("卡密"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("类型"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("创建人"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("创建时间"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("启用时间"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("到期时间"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("备注"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("复制"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableSetupColumn(u8s("复制一行"), ImGuiTableColumnFlags_WidthFixed);
		ImGui::TableHeadersRow();
		for (int i = 0; i < 100; i++)
		{
			ImGui::TableNextRow();

			ImGui::TableSetColumnIndex(0);
			ImGui::Text(u8s("卡密"));

			ImGui::TableSetColumnIndex(1);
			ImGui::Text(u8s("周卡"));

			ImGui::TableSetColumnIndex(2);
			ImGui::Text(u8s("测试"));

			ImGui::TableSetColumnIndex(3);
			ImGui::Text(u8s("2024-8-24 17:45:05"));

			ImGui::TableSetColumnIndex(4);
			ImGui::Text(u8s("2024-8-24 17:45:05"));

			ImGui::TableSetColumnIndex(5);
			ImGui::Text(u8s("2024-8-24 17:45:05"));

			ImGui::TableSetColumnIndex(6);
			ImGui::Text(u8s("测试"));

			ImGui::TableSetColumnIndex(7);
			if (ImGui::Button(StringHelper::Format(u8s("复制##ex_copy_%d"), i).c_str()))
			{
				MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
			}
			ImGui::TableSetColumnIndex(8);
			if (ImGui::Button(StringHelper::Format(u8s("复制一行##ex_copy_line_%d"), i).c_str()))
			{
				MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
			}

		}
		ImGui::EndTable();
	}

}
void KeyTypesFrame(SIZE windowSize)
{
	static bool _inAddCard = false;
	static char newTypeName[0x100]{};
	static char newTypepfx[0x100]{};
	static int newTypeTime = 24;
	if (_inAddCard)
	{
		ImGui::Text(u8s("名称:"));
		_SL;
		ImGui::SetNextItemWidth(100);
		ImGui::InputText("##input_add_type_name", newTypeName, 0x100);

		ImGui::Text(u8s("前缀:"));
		_SL;
		ImGui::SetNextItemWidth(100);
		ImGui::InputText("##input_add_type_pfx", newTypepfx, 0x100);

		ImGui::Text(u8s("时间:"));
		_SL;
		ImGui::SetNextItemWidth(100);
		ImGui::InputInt("##input_add_type_time", &newTypeTime); _SL; ImGui::Text(u8s("小时"));
		if (ImGui::Button(u8s("确定##create_type_ok")))
		{
			_inAddCard = false;
		}
		_SL;
		if (ImGui::Button(u8s("取消##create_type_cancel")))
		{
			_inAddCard = false;
		}
	}
	else
	{
		if (ImGui::Button(u8s("添加类型##create_type")))
		{
			ZeroMemory(newTypeName, 0x100);
			ZeroMemory(newTypepfx, 0x100);
			_inAddCard = true;
		}
		if (ImGui::BeginTable(u8s("##key_type_table"), 5, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable))
		{
			ImGui::TableSetupColumn(u8s("名称"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("前缀"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s("时长"), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableSetupColumn(u8s(" "), ImGuiTableColumnFlags_WidthFixed);
			ImGui::TableHeadersRow();
			for (int i = 0; i < 8; i++)
			{
				ImGui::TableNextRow();

				ImGui::TableSetColumnIndex(0);
				ImGui::Text(u8s("测试"));

				ImGui::TableSetColumnIndex(1);
				ImGui::Text(u8s("XX"));

				ImGui::TableSetColumnIndex(2);
				ImGui::Text(u8s("24"));

				ImGui::TableSetColumnIndex(3);
				if (ImGui::Button(StringHelper::Format(u8s("编辑##key_type_table_edit%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}
				ImGui::TableSetColumnIndex(4);
				if (ImGui::Button(StringHelper::Format(u8s("删除##key_type_table_del%d"), i).c_str()))
				{
					MessageBoxA(NULL, std::to_string(i).c_str(), std::to_string(i).c_str(), MB_OK);
				}

			}
			ImGui::EndTable();
		}
	}

}
void UserFrame(SIZE windowSize)
{

}
void CenterFrame(SIZE windowSize)
{
	float headWidth = 150.0f;
	ImGui::SetCursorPos(ImVec2(10, 10.0f));
	ImGui::BeginChild(u8s("##TabHeads"), ImVec2(headWidth, windowSize.cy - 20), true, ImGuiWindowFlags_HorizontalScrollbar);
	{
		if (ImGui::Button(u8s("卡密查询##tab_head_keyserach"), ImVec2(CurrentTab == TabClass::KeySerach ? 130 : 120, 24)))
			CurrentTab = TabClass::KeySerach;

		if (ImGui::Button(u8s("卡密生成##tab_head_keycreate"), ImVec2(CurrentTab == TabClass::KeyCreater ? 130 : 120, 24)))
			CurrentTab = TabClass::KeyCreater;

		if (ImGui::Button(u8s("高级查询##tab_head_keyserachex"), ImVec2(CurrentTab == TabClass::KeySerachEx ? 130 : 120, 24)))
			CurrentTab = TabClass::KeySerachEx;

		if (ImGui::Button(u8s("卡密类型##tab_head_keytypes"), ImVec2(CurrentTab == TabClass::KeyTypes ? 130 : 120, 24)))
			CurrentTab = TabClass::KeyTypes;

		if (ImGui::Button(u8s("用户信息##tab_head_userinfo"), ImVec2(CurrentTab == TabClass::UserInfo ? 130 : 120, 24)))
			CurrentTab = TabClass::UserInfo;

	}
	ImGui::EndChild();
	ImGui::SetCursorPos(ImVec2(headWidth + 10, 10.0f));
	ImGui::BeginChild(StringHelper::Format(u8s("##Frames_%d"), CurrentTab).c_str(), ImVec2(windowSize.cx - (headWidth + 25), windowSize.cy - 20), true, ImGuiWindowFlags_HorizontalScrollbar);
	{
		switch (CurrentTab)
		{
		case TabClass::KeySerach:
			KeySerachFrame({ int(windowSize.cx - (headWidth + 28)),windowSize.cy - 15 });
			break;
		case TabClass::KeyCreater:
			KeyCreateFrame({ int(windowSize.cx - (headWidth + 28)),windowSize.cy - 15 });
			break;
		case TabClass::KeySerachEx:
			KeySerachFrameEx({ int(windowSize.cx - (headWidth + 28)),windowSize.cy - 15 });
			break;
		case TabClass::KeyTypes:
			KeyTypesFrame({ int(windowSize.cx - (headWidth + 28)),windowSize.cy - 15 });
			break;
		case TabClass::UserInfo:
			UserFrame({ int(windowSize.cx - (headWidth + 28)),windowSize.cy - 15 });
			break;
		default:
			break;
		}
	}
	ImGui::EndChild();
}
void GuiRender(SIZE windowSize)
{
	if (UserName.size() <= 0)
		LoginFrame(windowSize);
	else
		CenterFrame(windowSize);
}
void RenderImGui() {

	int width, height;
	RECT rect;
	GetClientRect(hwnd, &rect);
	width = rect.right - rect.left;
	height = rect.bottom - rect.top;

	ImGui_ImplDX11_NewFrame();
	ImGui_ImplWin32_NewFrame();
	ImGui::NewFrame();
	ImGui::SetNextWindowSize(ImVec2((float)width, (float)height));
	ImGui::SetNextWindowPos(ImVec2(0, 0));
	ImGui::Begin(u8s("##main_window"), nullptr,
		ImGuiWindowFlags_NoTitleBar |
		ImGuiWindowFlags_NoResize |
		ImGuiWindowFlags_NoMove |
		ImGuiWindowFlags_NoScrollbar |
		ImGuiWindowFlags_NoScrollWithMouse);
	{
		GuiRender({ width, height });
	}
	ImGui::End();
	ImGui::EndFrame();
	ImGui::Render();
	ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}
void CreateRenderTarget() {
	ID3D11Texture2D* pBackBuffer = nullptr;
	g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));

	D3D11_RENDER_TARGET_VIEW_DESC desc;
	ZeroMemory(&desc, sizeof(desc));
	desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;

	g_pd3dDevice->CreateRenderTargetView(pBackBuffer, &desc, &g_pRenderTargetView);
	pBackBuffer->Release();
}
void Render() {
	g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, NULL);
	float black[] = { 0.0f, 0.0f, 0.0f, 0.0f };
	g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, black);
	RenderImGui();
	g_pSwapChain->Present(0, 0);
}
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	ImGui_ImplWin32_WndProcHandler(hWnd, message, wParam, lParam);
	switch (message) {
	case WM_DESTROY:
		exit(0);
		break;
	case WM_SIZE:
		if (g_pSwapChain)
		{
			ImGui::GetIO().DisplaySize = ImVec2((float)LOWORD(lParam), (float)HIWORD(lParam));
			if (g_pRenderTargetView) {
				g_pRenderTargetView->Release();
				g_pRenderTargetView = nullptr;
			}
			ID3D11Texture2D* pBackBuffer = nullptr;
			HRESULT hr = g_pSwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
			if (SUCCEEDED(hr)) {
				hr = g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
				if (SUCCEEDED(hr)) {
					g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_pRenderTargetView);
					pBackBuffer->Release();
				}
			}
			Render();
		}
		break;
	case WM_MOVE:
		if (g_pSwapChain)
		{
			Render();
		}
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
void embraceTheDarkness()
{
	ImGuiStyle& style = ImGui::GetStyle();
	style.Alpha = 1.0;
	//style.WindowFillAlphaDefault = 0.83;
	//style.ChildWindowRounding = 3;
	style.WindowRounding = 3;
	style.GrabRounding = 1;
	style.GrabMinSize = 20;
	style.FrameRounding = 3;


	style.Colors[ImGuiCol_Text] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.00f, 0.40f, 0.41f, 1.00f);
	style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
	//style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
	style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 1.00f, 1.00f, 0.65f);
	style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
	style.Colors[ImGuiCol_FrameBg] = ImVec4(0.44f, 0.80f, 0.80f, 0.18f);
	style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.44f, 0.80f, 0.80f, 0.27f);
	style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.44f, 0.81f, 0.86f, 0.66f);
	style.Colors[ImGuiCol_TitleBg] = ImVec4(0.14f, 0.18f, 0.21f, 0.73f);
	style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.54f);
	style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.00f, 1.00f, 1.00f, 0.27f);
	style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.20f);
	style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.22f, 0.29f, 0.30f, 0.71f);
	style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.00f, 1.00f, 1.00f, 0.44f);
	style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.00f, 1.00f, 1.00f, 0.74f);
	style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	//style.Colors[ImGuiCol_ComboBg] = ImVec4(0.16f, 0.24f, 0.22f, 0.60f);
	style.Colors[ImGuiCol_CheckMark] = ImVec4(0.00f, 1.00f, 1.00f, 0.68f);
	style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.00f, 1.00f, 1.00f, 0.36f);
	style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.00f, 1.00f, 1.00f, 0.76f);
	style.Colors[ImGuiCol_Button] = ImVec4(0.00f, 0.65f, 0.65f, 0.46f);
	style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.01f, 1.00f, 1.00f, 0.43f);
	style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.00f, 1.00f, 1.00f, 0.62f);
	style.Colors[ImGuiCol_Header] = ImVec4(0.00f, 1.00f, 1.00f, 0.33f);
	style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.00f, 1.00f, 1.00f, 0.42f);
	style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.00f, 1.00f, 1.00f, 0.54f);
	//style.Colors[ImGuiCol_Column] = ImVec4(0.00f, 0.50f, 0.50f, 0.33f);
	//style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.00f, 0.50f, 0.50f, 0.47f);
	//style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.00f, 0.70f, 0.70f, 1.00f);
	style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 1.00f, 1.00f, 0.54f);
	style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.00f, 1.00f, 1.00f, 0.74f);
	style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	//style.Colors[ImGuiCol_CloseButton] = ImVec4(0.00f, 0.78f, 0.78f, 0.35f);
	//style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.00f, 0.78f, 0.78f, 0.47f);
	//style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.00f, 0.78f, 0.78f, 1.00f);
	style.Colors[ImGuiCol_PlotLines] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.00f, 1.00f, 1.00f, 1.00f);
	style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 1.00f, 1.00f, 0.22f);
	//style.Colors[ImGuiCol_TooltipBg] = ImVec4(0.00f, 0.13f, 0.13f, 0.90f);
	//style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.04f, 0.10f, 0.09f, 0.51f);
}
#include "EncryptedPak.h"
struct TestSubStruct
{
	const char name[0x10];
	double v;
	ULONG64 h;
};
struct TestStruct
{
	const char name[0x10];
	double v;
	int h;
	TestSubStruct sub_;
};

int main()
{
	WNDCLASSEX wc = {};
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = GetModuleHandleA(NULL);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszClassName = L"KNWindowClass";
	RegisterClassEx(&wc);
	int width = 1600;
	int height = 960;
	hwnd = CreateWindow(L"KNWindowClass", L"KNClient", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, GetModuleHandleA(NULL), NULL);

	ShowWindow(hwnd, true);
	InitD3D(hwnd);
	CreateRenderTarget();

	duplicator = new DXGIDuplicator();
	if (!duplicator->InitD3D11Device(g_pd3dDevice, g_pImmediateContext))
	{
		return 1;
	}

	if (!duplicator->InitDuplication())
	{
		return 2;
	}

	InitImGui();
	embraceTheDarkness();

	MSG msg = {};
	bool running = true;
	while (running) {
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			if (msg.message == WM_QUIT)
				running = false;
			else
			{
				Render();
			}
		}
		else
		{
			static LONGLONG lastRender = 0;
			LONGLONG now = GetTickCount64();
			if (now - lastRender > 10)
			{
				lastRender = now;
				Render();
			}
			Sleep(1);
		}
	}
	g_pRenderTargetView->Release();
	g_pSwapChain->Release();
	g_pImmediateContext->Release();
	g_pd3dDevice->Release();
	return NULL;
}