local _, GF = ...

GF.DebugPanel = {}
local DP = GF.DebugPanel

local STATUS_COLOR_ON = { 0.1, 0.95, 0.22, 1 }
local STATUS_COLOR_OFF = { 1, 0.18, 0.12, 1 }
local STATUS_COLOR_SCROLL = { 1, 0.82, 0, 1 }
local STATUS_COLOR_NORMAL = { 0.92, 0.90, 0.84, 1 }

local function getScenarioLabel(scenario)
	local L = GF.L or {}
	if scenario == "basic" then
		return L.DEBUG_SCENARIO_BASIC or "Basic"
	end
	if scenario == "scroll" then
		return L.DEBUG_SCENARIO_SCROLL or "Scroll stress"
	end
	return L.DEBUG_SCENARIO_FULL or "Full"
end

local function getStateText(enabled)
	local L = GF.L or {}
	return enabled and (L.DEBUG_STATE_ON or "On")
		or (L.DEBUG_STATE_OFF or "Off")
end

local function setStatusValue(value, text, color)
	if not value then
		return
	end
	value:SetText(text or "")
	color = color or STATUS_COLOR_NORMAL
	value:SetTextColor(color[1], color[2], color[3], color[4])
end

function DP:Init(parent)
	if self.root then
		return
	end
	self.parent = parent
	local surface = GF.DebugSurface:Create(parent, {
		onAfterAction = function()
			DP:Refresh()
		end,
	})
	self.surface = surface
	self.root = surface.root
	self.scroll = surface.scroll
	self.scrollBar = surface.scrollBar
	self.body = surface.body

	local section = surface:CreateSection(
		"DEBUG_STATUS_SECTION",
		"Debug status")
	local statusValues = surface:AddStatusGrid(section, {
		left = {
			{
				id = "debugMode",
				localeKey = "DEBUG_MODE_STATE",
				fallback = "Debug tab",
			},
			{
				id = "testData",
				localeKey = "DEBUG_TESTDATA_STATE",
				fallback = "Applicant test data",
			},
			{
				id = "extraRows",
				localeKey = "DEBUG_TESTDATA_EXTRA_ROWS",
				fallback = "Extra scroll rows",
			},
		},
		right = {
			{
				id = "scenario",
				localeKey = "DEBUG_TESTDATA_SCENARIO",
				fallback = "Current scenario",
			},
			{
				id = "mplusTestData",
				localeKey = "MPLUS_DEBUG_TEST_DATA_SECTION",
				fallback = "Mythic+ test data",
			},
		},
	})
	self.debugModeValue = statusValues.debugMode
	self.testDataValue = statusValues.testData
	self.scenarioValue = statusValues.scenario
	self.extraRowsValue = statusValues.extraRows
	self.mplusTestDataValue = statusValues.mplusTestData
	surface:FinishSection(section)

	section = surface:CreateSection(
		"DEBUG_MEETING_STONE_TEST_DATA_SECTION",
		"Premade Groups tests")
	surface:AddButtonRow(
		section,
		"DEBUG_APPLICANT_TEST_ROW",
		"Applicant list test",
		{
			{
				localeKey = "DEBUG_ACTION_ENABLE_TESTDATA",
				fallback = "Enable applicant test",
				actionID = "applicantsOn",
			},
			{
				localeKey = "DEBUG_ACTION_DISABLE_TESTDATA",
				fallback = "Disable applicant test",
				actionID = "applicantsOff",
			},
			{
				localeKey = "DEBUG_ACTION_REFRESH_APPLICANTS",
				fallback = "Refresh applicants",
				actionID = "refreshApplicants",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_APPLICANT_SCENARIO_ROW",
		"Applicant list scenario",
		{
			{
				localeKey = "DEBUG_ACTION_SCENARIO_BASIC",
				fallback = "Basic sample",
				actionID = "scenarioBasic",
			},
			{
				localeKey = "DEBUG_ACTION_SCENARIO_FULL",
				fallback = "Full sample",
				actionID = "scenarioFull",
			},
			{
				localeKey = "DEBUG_ACTION_SCENARIO_SCROLL",
				fallback = "Scroll stress",
				actionID = "scenarioScroll",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_PENDING_LISTING_ROW",
		"Pending listing",
		{
			{
				localeKey = "DEBUG_CENSORED_ACTIVE_UNKNOWN",
				fallback = "Simulate pending",
				actionID = "censoredActiveUnknown",
			},
			{
				localeKey = "DEBUG_CENSORED_ACTIVE_OFF",
				fallback = "Clear pending",
				actionID = "censoredActiveOff",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_PENDING_DIALOG_ROW",
		"Pending dialog",
		{
			{
				localeKey = "DEBUG_CENSORED_ACTIVE_DIALOG",
				fallback = "Preview pending dialog",
				actionID = "censoredActiveDialog",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_HIDDEN_GROUP_ROW",
		"Hidden group row",
		{
			{
				localeKey = "DEBUG_CENSORED_BROWSE_PREVIEW",
				fallback = "Preview hidden group",
				actionID = "censoredBrowsePreview",
			},
		})
	surface:FinishSection(section)

	section = surface:CreateSection(
		"DEBUG_MPLUS_TEST_SECTION",
		"Mythic+ tests")
	surface:AddButtonRow(
		section,
		"DEBUG_MPLUS_DATA_ROW",
		"Mythic+ test data",
		{
			{
				localeKey = "MPLUS_DEBUG_ENABLE",
				fallback = "Enable Mythic+ test",
				actionID = "mplusTestData",
			},
			{
				localeKey = "MPLUS_DEBUG_CLEAR",
				fallback = "Clear Mythic+ test",
				actionID = "mplusTestDataOff",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_MPLUS_CARPOOL_ROW",
		"Carpool sample size",
		{
			{
				localeKey = "MPLUS_DEBUG_ENABLE_GROUP_1",
				fallback = "Show 1 group",
				actionID = "mplusTestData1",
			},
			{
				localeKey = "MPLUS_DEBUG_ENABLE_GROUP_2",
				fallback = "Show 2 groups",
				actionID = "mplusTestData2",
			},
			{
				localeKey = "MPLUS_DEBUG_ENABLE_GROUP_3",
				fallback = "Show 3 groups",
				actionID = "mplusTestData3",
			},
		})
	surface:FinishSection(section)

	section = surface:CreateSection(
		"DEBUG_DIAGNOSTICS_SECTION",
		"Diagnostics")
	surface:AddButtonRow(
		section,
		"DEBUG_ACTIVITY_MENU_AUDIT_ROW",
		"LFG activity menu",
		{
			{
				localeKey = "DEBUG_ACTION_LFG_COVERAGE_AUDIT",
				fallback = "Audit activity menu",
				actionID = "lfgCoverageAudit",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_TELEPORT_MAPPING_ROW",
		"Teleport mapping",
		{
			{
				localeKey = "MPLUS_DEBUG_DUMP_MAPPING",
				fallback = "Export teleport map",
				actionID = "mplusTeleportDump",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_COMMAND_HELP_ROW",
		"Debug commands",
		{
			{
				localeKey = "DEBUG_ACTION_PRINT_HELP",
				fallback = "Show debug commands",
				actionID = "help",
			},
		})
	surface:FinishSection(section)

	section = surface:CreateSection(
		"DEBUG_INTERFACE_PREVIEW_SECTION",
		"Interface preview")
	surface:AddButtonRow(
		section,
		"DEBUG_INTERFACE_LOCALE_ROW",
		"Interface language",
		{
			{
				fixedText = "ENGLISH",
				actionID = "forceLocaleEnUS",
			},
			{
				fixedText = "中文（简体）",
				actionID = "forceLocaleZhCN",
			},
			{
				fixedText = "中文（繁體）",
				actionID = "forceLocaleZhTW",
			},
		})
	surface:AddButtonRow(
		section,
		"DEBUG_VERSION_NOTICE_ROW",
		"Version update notice",
		{
			{
				localeKey = "DEBUG_ACTION_TEST_NEW_VERSION",
				fallback = "Preview version notice",
				actionID = "testNewVersion",
			},
		})
	surface:AddInputActionRow(
		section,
		"DEBUG_TELEPORT_DIALOG_ROW",
		"Teleport dialog",
		"MPLUS_DEBUG_TELEPORT_MAP_ID_PLACEHOLDER",
		"mapID",
		{
			localeKey = "MPLUS_DEBUG_PREVIEW_DIALOG",
			fallback = "Preview teleport dialog",
			actionID = "mplusTeleportDialog",
		})
	surface:FinishSection(section)

	surface:Finalize()
	self.bodyH = surface.bodyH
	self:Refresh()
end

function DP:Refresh()
	if not self.root then
		return
	end
	local status = GF.Debug and GF.Debug.GetApplicantTestDataStatus
		and GF.Debug:GetApplicantTestDataStatus() or {}
	local debugEnabled = GF.Debug and GF.Debug.IsDebugModeEnabled
		and GF.Debug:IsDebugModeEnabled()
	if self.debugModeValue then
		setStatusValue(
			self.debugModeValue,
			getStateText(debugEnabled),
			debugEnabled and STATUS_COLOR_ON or STATUS_COLOR_OFF)
	end
	if self.testDataValue then
		setStatusValue(
			self.testDataValue,
			getStateText(status.enabled == true),
			status.enabled == true and STATUS_COLOR_ON or STATUS_COLOR_OFF)
	end
	if self.scenarioValue then
		local scenarioColor = status.scenario == "scroll"
			and STATUS_COLOR_SCROLL or STATUS_COLOR_NORMAL
		setStatusValue(
			self.scenarioValue,
			getScenarioLabel(status.scenario),
			scenarioColor)
	end
	if self.extraRowsValue then
		setStatusValue(
			self.extraRowsValue,
			tostring(status.extraRows or 0),
			STATUS_COLOR_NORMAL)
	end
	if self.mplusTestDataValue then
		local service = GF.MythicPlusDebugService
		local enabled = service and service.IsEnabled
			and service:IsEnabled() == true
		setStatusValue(
			self.mplusTestDataValue,
			getStateText(enabled),
			enabled and STATUS_COLOR_ON or STATUS_COLOR_OFF)
	end
	self:UpdateScroll()
end

function DP:RefreshLocale()
	if not self.surface then
		return
	end
	self.surface:RefreshLocale()
	self:Refresh()
end

function DP:UpdateScroll()
	if self.surface then
		self.surface:UpdateScroll()
		self.bodyH = self.surface.bodyH
	end
end

function DP:Show()
	if self.surface then
		self.surface:Show()
		self:Refresh()
	end
end

function DP:Hide()
	if self.surface then
		self.surface:Hide()
	end
end
