-- ApiVault Developer Debug Dashboard UI. Added in v1.1.5.
local addonName, ApiVault = ...

ApiVault.DeveloperDebugDashboardPanel = ApiVault.DeveloperDebugDashboardPanel or {}
local Panel = ApiVault.DeveloperDebugDashboardPanel

local function createBackdropFrame(name, parent)
	return CreateFrame("Frame", name, parent, BackdropTemplateMixin and "BackdropTemplate" or nil)
end

local function setBackdrop(frame)
	if not frame.SetBackdrop then return end
	frame:SetBackdrop({
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
		edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
		tile = true,
		tileSize = 32,
		edgeSize = 32,
		insets = { left = 8, right = 8, top = 8, bottom = 8 },
	})
	frame:SetBackdropColor(0.04, 0.04, 0.06, 0.96)
end

local function createText(parent, template)
	local fs = parent:CreateFontString(nil, "OVERLAY", template or "GameFontNormal")
	fs:SetJustifyH("LEFT")
	return fs
end

local function makeButton(parent, text, width, onClick)
	local b = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
	b:SetSize(width or 90, 22)
	b:SetText(text)
	b:SetScript("OnClick", onClick)
	return b
end

function Panel:Create()
	if self.frame then return end
	local frame = createBackdropFrame("ApiVaultDeveloperDebugDashboardPanel", UIParent)
	self.frame = frame
	frame:SetSize(1100, 720)
	frame:SetPoint("CENTER", 0, 0)
	frame:SetFrameStrata("FULLSCREEN_DIALOG")
	frame:SetMovable(true)
	frame:EnableMouse(true)
	frame:RegisterForDrag("LeftButton")
	frame:SetScript("OnDragStart", frame.StartMoving)
	frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
	if frame.SetResizable then
		frame:SetResizable(true)
		if frame.SetResizeBounds then frame:SetResizeBounds(920, 600) elseif frame.SetMinResize then frame:SetMinResize(920, 600) end
	end
	setBackdrop(frame)
	if ApiVault.PrepareWindow then ApiVault:PrepareWindow(frame, "DeveloperDebugDashboard") end
	frame:Hide()

	local close = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
	close:SetPoint("TOPRIGHT", -6, -6)

	local title = createText(frame, "GameFontHighlightLarge")
	title:SetPoint("TOPLEFT", 18, -18)
	title:SetText("ApiVault - Developer Debug Dashboard")

	local note = createText(frame, "GameFontDisableSmall")
	note:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
	note:SetText("Public API debug data dashboard. All data is advisory and supplied by registered addons.")

	local addonBox = CreateFrame("EditBox", nil, frame, "InputBoxTemplate")
	self.addonBox = addonBox
	addonBox:SetSize(250, 24)
	addonBox:SetPoint("TOPLEFT", 18, -66)
	addonBox:SetAutoFocus(false)
	addonBox:SetScript("OnEnterPressed", function(box) box:ClearFocus(); Panel:Refresh() end)

	local current = addonBox
	local buttons = {
		{ "Overview", 82, "overview" }, { "Timeline", 82, "all" }, { "Errors", 70, "errors" },
		{ "API", 60, "apis" }, { "Events", 70, "events" }, { "Secure", 74, "secure" },
		{ "SV", 54, "savedvariables" }, { "Sessions", 82, "sessions" }, { "Export", 72, "export" },
	}
	self.view = "overview"
	for _, spec in ipairs(buttons) do
		local b = makeButton(frame, spec[1], spec[2], function() Panel.view = spec[3]; Panel:Refresh() end)
		b:SetPoint("LEFT", current, "RIGHT", 6, 0)
		current = b
	end

	local hint = createText(frame, "GameFontDisableSmall")
	hint:SetPoint("TOPLEFT", addonBox, "BOTTOMLEFT", 0, -4)
	hint:SetText("Type an addon name for addon-specific views, or leave blank for first registered addon/overview.")

	local holder = createBackdropFrame(nil, frame)
	holder:SetPoint("TOPLEFT", frame, "TOPLEFT", 18, -106)
	holder:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -18, 18)
	setBackdrop(holder)

	local scroll = CreateFrame("ScrollFrame", nil, holder, "UIPanelScrollFrameTemplate")
	scroll:SetPoint("TOPLEFT", holder, "TOPLEFT", 12, -12)
	scroll:SetPoint("BOTTOMRIGHT", holder, "BOTTOMRIGHT", -30, 12)

	local output = CreateFrame("EditBox", nil, scroll)
	self.output = output
	output:SetMultiLine(true)
	output:SetAutoFocus(false)
	output:SetFontObject(ChatFontNormal)
	output:SetWidth(1000)
	output:SetText("")
	output:SetScript("OnEscapePressed", function(box) box:ClearFocus() end)
	scroll:SetScrollChild(output)

	frame:SetScript("OnSizeChanged", function()
		if output then output:SetWidth(math.max(650, holder:GetWidth() - 48)) end
	end)
end

function Panel:Refresh()
	self:Create()
	local addon = self.addonBox and self.addonBox:GetText() or ""
	if ApiVault.DeveloperDebugDashboard and ApiVault.DeveloperDebugDashboard.FormatDashboard then
		self.output:SetText(ApiVault.DeveloperDebugDashboard:FormatDashboard(addon, self.view or "overview"))
	else
		self.output:SetText("DeveloperDebugDashboard unavailable.")
	end
	self.output:SetCursorPosition(0)
end

function Panel:Open(view)
	self:Create()
	if view then self.view = view end
	self.frame:Show()
	if ApiVault.RaiseWindow then ApiVault:RaiseWindow(self.frame) end
	self:Refresh()
end

function Panel:Toggle(view)
	self:Create()
	if self.frame:IsShown() and (not view or view == self.view) then self.frame:Hide() else self:Open(view) end
end
