local addonName, ApiVault = ...

ApiVault.ExportPanel = ApiVault.ExportPanel or {}

local BUTTONS = {
	{ key = "api-summary", label = "Copy API summary" },
	{ key = "generated-snippet", label = "Copy generated snippet" },
	{ key = "module-summary", label = "Copy module summary" },
	{ key = "secure-warning", label = "Copy secure warning" },
	{ key = "audit-note", label = "Copy audit note" },
	{ key = "test-checklist", label = "Copy test checklist" },
	{ key = "api-checklist", label = "API usage checklist" },
	{ key = "secure-checklist", label = "Secure UI checklist" },
	{ key = "template-checklist", label = "Template usage checklist" },
	{ key = "toc-checklist", label = "TOC dependency checklist" },
	{ key = "event-checklist", label = "Event listener checklist" },
}

local function createFont(parent, template, justify)
	local font = parent:CreateFontString(nil, "OVERLAY", template or "GameFontNormal")
	font:SetJustifyH(justify or "LEFT")
	return font
end

local function setBackdrop(frame, r, g, b, a)
	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(r or 0.04, g or 0.04, b or 0.06, a or 0.96)
end

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

function ApiVault.ExportPanel:Create()
	if self.frame then
		return
	end

	local frame = createBackdropFrame("ApiVaultExportPanelFrame", UIParent)
	self.frame = frame
	frame:SetSize(980, 660)
	frame:SetPoint("CENTER", 70, -40)
	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(760, 500)
		elseif frame.SetMinResize then
			frame:SetMinResize(760, 500)
		end
	end
	setBackdrop(frame)
	if ApiVault.PrepareWindow then
		ApiVault:PrepareWindow(frame, "ExportPanel")
	end
	frame:Hide()

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

	local title = createFont(frame, "GameFontHighlightLarge")
	title:SetPoint("TOPLEFT", 18, -16)
	title:SetText("ApiVault - Developer Checklists & Export Panels")

	local sub = createFont(frame, "GameFontDisableSmall")
	self.contextText = sub
	sub:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
	sub:SetPoint("RIGHT", frame, "RIGHT", -20, 0)
	sub:SetText("Select an ApiVault item, then choose an export/checklist. Output is plain text; select and Ctrl+C.")

	local buttonColumn = createBackdropFrame(nil, frame)
	self.buttonColumn = buttonColumn
	buttonColumn:SetPoint("TOPLEFT", frame, "TOPLEFT", 18, -70)
	buttonColumn:SetSize(230, 540)
	setBackdrop(buttonColumn, 0.02, 0.02, 0.03, 0.82)

	local y = -14
	self.buttons = {}
	for index, config in ipairs(BUTTONS) do
		local button = CreateFrame("Button", nil, buttonColumn, "UIPanelButtonTemplate")
		button:SetSize(190, 24)
		button:SetPoint("TOPLEFT", buttonColumn, "TOPLEFT", 18, y)
		button:SetText(config.label)
		button:SetScript("OnClick", function()
			ApiVault.ExportPanel:SetOutput(config.key)
		end)
		self.buttons[config.key] = button
		y = y - 30
	end

	local closeOthers = CreateFrame("Button", nil, buttonColumn, "UIPanelButtonTemplate")
	closeOthers:SetSize(190, 24)
	closeOthers:SetPoint("BOTTOMLEFT", buttonColumn, "BOTTOMLEFT", 18, 18)
	closeOthers:SetText("Close other ApiVault windows")
	closeOthers:SetScript("OnClick", function()
		if ApiVault.CloseOtherWindows then
			ApiVault:CloseOtherWindows(frame)
		end
	end)

	local outputBg = createBackdropFrame(nil, frame)
	self.outputBg = outputBg
	outputBg:SetPoint("TOPLEFT", buttonColumn, "TOPRIGHT", 14, 0)
	outputBg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -18, 50)
	setBackdrop(outputBg, 0.025, 0.025, 0.035, 0.86)

	local outputTitle = createFont(outputBg, "GameFontNormal")
	self.outputTitle = outputTitle
	outputTitle:SetPoint("TOPLEFT", outputBg, "TOPLEFT", 14, -12)
	outputTitle:SetText("Export output")

	local outputScroll = CreateFrame("ScrollFrame", nil, outputBg, "UIPanelScrollFrameTemplate")
	self.outputScroll = outputScroll
	outputScroll:SetPoint("TOPLEFT", outputTitle, "BOTTOMLEFT", 0, -8)
	outputScroll:SetPoint("BOTTOMRIGHT", outputBg, "BOTTOMRIGHT", -30, 14)

	local output = CreateFrame("EditBox", nil, outputScroll)
	self.outputBox = output
	output:SetMultiLine(true)
	output:SetAutoFocus(false)
	output:SetFontObject(ChatFontNormal)
	output:SetWidth(660)
	output:SetTextInsets(4, 4, 4, 4)
	output:SetScript("OnEscapePressed", function(selfBox) selfBox:ClearFocus() end)
	outputScroll:SetScrollChild(output)

	local hint = createFont(frame, "GameFontDisableSmall")
	hint:SetPoint("BOTTOMLEFT", outputBg, "BOTTOMLEFT", 14, -28)
	hint:SetText("Copy support: click the text box, Ctrl+A, Ctrl+C. No clipboard API is used.")

	self:SetOutput("test-checklist")
end

function ApiVault.ExportPanel:RefreshContext()
	if self.contextText and ApiVault.ChecklistExport and ApiVault.ChecklistExport.GetContextLabel then
		self.contextText:SetText("Context: " .. tostring(ApiVault.ChecklistExport:GetContextLabel()) .. "  |  Select output, then Ctrl+A/Ctrl+C.")
	end
end

function ApiVault.ExportPanel:SetOutput(kind)
	self:Create()
	self.kind = kind or self.kind or "test-checklist"
	local text = "Checklist/export system unavailable."
	if ApiVault.ChecklistExport and ApiVault.ChecklistExport.Build then
		text = ApiVault.ChecklistExport:Build(self.kind)
	end
	self.outputTitle:SetText("Export output: " .. tostring(self.kind))
	self.outputBox:SetText(text or "")
	self.outputBox:SetCursorPosition(0)
	self:RefreshContext()
end

function ApiVault.ExportPanel:OpenWithContext(context, kind)
	if ApiVault.ChecklistExport and ApiVault.ChecklistExport.SetContext and context then
		ApiVault.ChecklistExport:SetContext(context)
	end
	self:Create()
	self.frame:Show()
	if ApiVault.RaiseWindow then
		ApiVault:RaiseWindow(self.frame)
	end
	self:SetOutput(kind or "test-checklist")
	self.outputBox:SetFocus()
	self.outputBox:HighlightText()
end

function ApiVault.ExportPanel:Toggle()
	self:Create()
	if self.frame:IsShown() then
		self.frame:Hide()
	else
		self.frame:Show()
		if ApiVault.RaiseWindow then
			ApiVault:RaiseWindow(self.frame)
		end
		self:SetOutput(self.kind or "test-checklist")
	end
end
