#include <windows.h>                // For HWND, MessageBox, SendMessage, SetWindowText, GetDlgItem, BOOL, etc.
#include <string>                   // For std::string, std::wstring
#include <codecvt>                  // For std::wstring_convert, std::codecvt_utf8_utf16
#include "LiftoffPrivacy.h"
#include "LiftoffInterstitialAds.h"
#include "LiftoffWin32DesktopTestApp.h" // For global variables like hTabPages, g_sdkInstance, control IDs

void UpdateCcpaStatusLabel() {
	CcpaConsentStatus status = LiftoffAds::GetCcpaStatus();
	LPCWSTR statusText = L"Unknown";
	switch (status) {
	case CcpaConsentStatus::OptedIn:
		statusText = L"Opted In";
		break;
	case CcpaConsentStatus::OptedOut:
		statusText = L"Opted Out";
		break;
	}
	SetWindowText(GetDlgItem(hTabPages[3], ID_LABEL_CCPA_STATUS), statusText);
}

void SetCcpaStatusFromCombo(HWND hWnd) {
	HWND hComboCcpa = GetDlgItem(hTabPages[3], ID_COMBOBOX_CCPA);
	int sel = (int)SendMessage(hComboCcpa, CB_GETCURSEL, 0, 0);
	if (sel == CB_ERR) {
		MessageBox(hWnd, L"Please select a CCPA value before setting.", L"Error", MB_OK | MB_ICONERROR);
		return;
	}
	// 0 = Opted In, 1 = Opted Out
	CcpaConsentStatus status = (sel == 0) ? CcpaConsentStatus::OptedIn : CcpaConsentStatus::OptedOut;
	LiftoffAds::SetCcpaStatus(status);

	LPCWSTR statusText = (status == CcpaConsentStatus::OptedIn) ? L"Opted In" : L"Opted Out";
	SetWindowText(GetDlgItem(hTabPages[3], ID_LABEL_CCPA_STATUS), statusText);
}

void UpdateCoppaStatusLabel() {
	BOOL coppaStatus = LiftoffAds::GetCoppaStatus();
	LPCWSTR coppaText = coppaStatus ? L"True" : L"False";
	SetWindowText(GetDlgItem(hTabPages[3], ID_LABEL_COPPA_STATUS), coppaText);
}

void SetCoppaStatusFromCombo(HWND hWnd) {
	HWND hComboCoppa = GetDlgItem(hTabPages[3], ID_COMBOBOX_COPPA);
	int sel = (int)SendMessage(hComboCoppa, CB_GETCURSEL, 0, 0);
	if (sel == CB_ERR) {
		MessageBox(hWnd, L"Please select a COPPA value before setting.", L"Error", MB_OK | MB_ICONERROR);
		return;
	}
	// 0 = True, 1 = False
	BOOL coppaStatus = (sel == 0) ? TRUE : FALSE;
	LiftoffAds::SetCoppaStatus(coppaStatus);
	LPCWSTR coppaText = coppaStatus ? L"True" : L"False";
	SetWindowText(GetDlgItem(hTabPages[3], ID_LABEL_COPPA_STATUS), coppaText);
}

void UpdateGdprStatusLabel() {
	GdprConsentStatus status = LiftoffAds::GetGdprConsentStatus();
	LPCWSTR statusText = L"Unknown";
	switch (status) {
		case GdprConsentStatus::ConsentAccepted:
			statusText = L"Opted In";
			break;
		case GdprConsentStatus::ConsentDenied:
			statusText = L"Opted Out";
			break;
	}
	SetWindowText(GetDlgItem(hTabPages[3], ID_LABEL_GDPR_STATUS), statusText);
	std::string version = LiftoffAds::GetGdprConsentMessageVersion();
	if (!version.empty()) {
		std::wstring wVersion = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(version);
		SetWindowText(GetDlgItem(hTabPages[3], ID_EDIT_GDPR_CONSENT_MSG_VER), wVersion.c_str());
	}
}

void SetGdprStatusFromCombo(HWND hWnd) {
	HWND hComboGdpr = GetDlgItem(hTabPages[3], ID_COMBOBOX_GDPR);
	int sel = (int)SendMessage(hComboGdpr, CB_GETCURSEL, 0, 0);
	if (sel == CB_ERR) {
		MessageBox(hWnd, L"Please select a GDPR value before setting.", L"Error", MB_OK | MB_ICONERROR);
		return;
	}
	// Map combo box selection to GdprConsentStatus
	GdprConsentStatus status = GdprConsentStatus::ConsentAccepted;
	switch (sel) {
	default:
	case 0: status = GdprConsentStatus::ConsentAccepted; break; // Opted In
	case 1: status = GdprConsentStatus::ConsentDenied; break;   // Opted Out
	}
	// Get consent message version from edit box
	WCHAR wVersion[128] = { 0 };
	GetWindowText(GetDlgItem(hTabPages[3], ID_EDIT_GDPR_CONSENT_MSG_VER), wVersion, 128);
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
	std::string versionStr = converter.to_bytes(wVersion);

	LiftoffAds::SetGdprConsentStatus(status, versionStr);

	// Update status label
	LPCWSTR statusText = L"Unknown";
	switch (status) {
	case GdprConsentStatus::ConsentAccepted: statusText = L"Opted In"; break;
	case GdprConsentStatus::ConsentDenied:   statusText = L"Opted Out"; break;
	}
	SetWindowText(GetDlgItem(hTabPages[3], ID_LABEL_GDPR_STATUS), statusText);
}