-- ApiVault Event Lab UI. Added in v1.0.0.
local addonName, ApiVault = ...

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

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

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

	local frame = createBackdropFrame("ApiVaultEventLabPanel", UIParent)
	self.frame = frame
	frame:SetSize(980, 640)
	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(820, 520) elseif frame.SetMinResize then frame:SetMinResize(820, 520) end
	end
	setBackdrop(frame)
	if ApiVault.PrepareWindow then ApiVault:PrepareWindow(frame, "EventLab") 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 - Event Lab + Payload Recorder")

	local note = createText(frame, "GameFontDisableSmall")
	note:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
	note:SetText("Manual listener only. Payload log is capped. No auto-register-all.")

	local eventBox = CreateFrame("EditBox", nil, frame, "InputBoxTemplate")
	self.eventBox = eventBox
	eventBox:SetSize(260, 24)
	eventBox:SetPoint("TOPLEFT", 18, -66)
	eventBox:SetAutoFocus(false)
	eventBox:SetText("BAG_UPDATE")
	eventBox:SetScript("OnEnterPressed", function(box)
		box:ClearFocus()
		Panel:Start()
	end)

	local listen = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
	listen:SetSize(70, 22)
	listen:SetPoint("LEFT", eventBox, "RIGHT", 10, 0)
	listen:SetText("Listen")
	listen:SetScript("OnClick", function() Panel:Start() end)

	local stop = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
	stop:SetSize(70, 22)
	stop:SetPoint("LEFT", listen, "RIGHT", 6, 0)
	stop:SetText("Stop")
	stop:SetScript("OnClick", function() Panel:Stop() end)

	local stopAll = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
	stopAll:SetSize(82, 22)
	stopAll:SetPoint("LEFT", stop, "RIGHT", 6, 0)
	stopAll:SetText("Stop All")
	stopAll:SetScript("OnClick", function()
		if ApiVault.EventRecorder then ApiVault.EventRecorder:StopAll() end
		Panel:Refresh()
	end)

	local clear = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
	clear:SetSize(70, 22)
	clear:SetPoint("LEFT", stopAll, "RIGHT", 6, 0)
	clear:SetText("Clear")
	clear:SetScript("OnClick", function()
		if ApiVault.EventRecorder then ApiVault.EventRecorder:Clear() end
		Panel:Refresh()
	end)

	local snippet = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
	snippet:SetSize(116, 22)
	snippet:SetPoint("LEFT", clear, "RIGHT", 6, 0)
	snippet:SetText("Copy Snippet")
	snippet:SetScript("OnClick", function() Panel:ShowSnippet() end)

	local holder = createBackdropFrame(nil, frame)
	holder:SetPoint("TOPLEFT", frame, "TOPLEFT", 18, -102)
	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(900)
	output:SetText("")
	output:SetScript("OnEscapePressed", function(box) box:ClearFocus() end)
	scroll:SetScrollChild(output)

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

function Panel:Start()
	self:Create()
	local eventName = self.eventBox and self.eventBox:GetText() or ""
	if ApiVault.EventRecorder then
		local ok, message = ApiVault.EventRecorder:Start(eventName)
		ApiVault:Print(message)
		ApiVault.EventRecorder.onUpdate = function() Panel:Refresh() end
	end
	self:Refresh()
end

function Panel:Stop()
	local eventName = self.eventBox and self.eventBox:GetText() or ""
	if ApiVault.EventRecorder then
		local ok, message = ApiVault.EventRecorder:Stop(eventName)
		ApiVault:Print(message)
	end
	self:Refresh()
end

function Panel:ShowSnippet()
	self:Create()
	local eventName = self.eventBox and self.eventBox:GetText() or ""
	if ApiVault.EventRecorder then
		self.output:SetText(ApiVault.EventRecorder:BuildListenerSnippet(eventName))
		self.output:SetCursorPosition(0)
	end
end

function Panel:Refresh()
	self:Create()
	if ApiVault.EventRecorder then
		self.output:SetText(ApiVault.EventRecorder:FormatLog())
		self.output:SetCursorPosition(0)
	else
		self.output:SetText("EventRecorder unavailable.")
	end
end

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

function Panel:Toggle()
	self:Create()
	if self.frame:IsShown() then self.frame:Hide() else self:Open() end
end
