local addonName, ApiVault = ...

ApiVault.WidgetMethodBrowser = ApiVault.WidgetMethodBrowser or {}

local ROW_HEIGHT = 22
local ROW_COUNT = 24
local FILTERS = { "All", "Methods", "Scripts", "CreateFrame", "XMLWidgets", "Protected" }

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.WidgetMethodBrowser:Create()
	if self.frame then return end
	local frame = createBackdropFrame("ApiVaultWidgetMethodBrowserFrame", UIParent)
	self.frame = frame
	frame:SetSize(1120, 690)
	frame:SetPoint("CENTER", 200, -100)
	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, 540) elseif frame.SetMinResize then frame:SetMinResize(920, 540) end end
	setBackdrop(frame)
	if ApiVault.PrepareWindow then ApiVault:PrepareWindow(frame, "WidgetMethodBrowser") 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 - Frame / Widget / ScriptObject Method Explorer")
	local total, methods, scripts, createFrame, setScript = 0,0,0,0,0
	if ApiVault.Widgets and ApiVault.Widgets.GetStats then total, methods, scripts, createFrame, setScript = ApiVault.Widgets:GetStats() end
	local sub = createFont(frame, "GameFontDisableSmall")
	sub:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
	sub:SetText("entries "..tostring(total).." - methods/templates "..tostring(methods).." - XML scripts "..tostring(scripts).." - CreateFrame "..tostring(createFrame).." - SetScript/HookScript "..tostring(setScript).." | inspect-only")

	local search = CreateFrame("EditBox", nil, frame, "InputBoxTemplate")
	self.search = search
	search:SetSize(380, 24)
	search:SetPoint("TOPLEFT", frame, "TOPLEFT", 18, -72)
	search:SetAutoFocus(false)
	search:SetScript("OnTextChanged", function() self:Refresh() end)

	local label = createFont(frame, "GameFontDisableSmall")
	label:SetPoint("LEFT", search, "RIGHT", 12, 0)
	label:SetText("Search widget methods, frame types, templates, scripts and files")

	local last
	self.filterButtons = {}
	for _, filter in ipairs(FILTERS) do
		local button = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
		button:SetSize(filter == "CreateFrame" and 104 or 92, 22)
		if last then button:SetPoint("LEFT", last, "RIGHT", 6, 0) else button:SetPoint("TOPLEFT", search, "BOTTOMLEFT", 0, -12) end
		button:SetText(filter)
		button:SetScript("OnClick", function() self.filter = filter; self:Refresh() end)
		self.filterButtons[filter] = button
		last = button
	end

	local listFrame = createBackdropFrame(nil, frame)
	self.listFrame = listFrame
	listFrame:SetPoint("TOPLEFT", frame, "TOPLEFT", 18, -136)
	listFrame:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 18, 28)
	listFrame:SetWidth(450)
	setBackdrop(listFrame, 0.02, 0.02, 0.03, 0.72)

	local detail = CreateFrame("EditBox", nil, frame)
	self.detail = detail
	detail:SetMultiLine(true)
	detail:SetAutoFocus(false)
	detail:SetFontObject(ChatFontNormal)
	detail:SetPoint("TOPLEFT", listFrame, "TOPRIGHT", 20, -6)
	detail:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -28, 32)
	detail:SetText("Select a widget/method entry.")
	detail:EnableMouse(true)

	self.rows = {}
	for i=1, ROW_COUNT do
		local row = CreateFrame("Button", nil, listFrame)
		row:SetHeight(ROW_HEIGHT)
		row:SetPoint("LEFT", listFrame, "LEFT", 12, 0)
		row:SetPoint("RIGHT", listFrame, "RIGHT", -12, 0)
		if i == 1 then row:SetPoint("TOP", listFrame, "TOP", 0, -12) else row:SetPoint("TOP", self.rows[i-1], "BOTTOM", 0, 0) end
		row.text = createFont(row, "GameFontNormalSmall")
		row.text:SetPoint("LEFT")
		row.text:SetPoint("RIGHT")
		row:SetScript("OnClick", function(button)
			self.selected = button.entry
			if ApiVault.Widgets and ApiVault.Widgets.Describe then self.detail:SetText(ApiVault.Widgets:Describe(button.entry)) end
		end)
		self.rows[i] = row
	end
	self.filter = "All"
	self:Refresh()
end

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

function ApiVault.WidgetMethodBrowser:Refresh()
	if not self.frame then return end
	local query = self.search and self.search:GetText() or ""
	local filter = self.filter or "All"
	self.results = {}
	if ApiVault.Widgets and ApiVault.Widgets.Search then self.results = ApiVault.Widgets:Search(query, filter, ROW_COUNT) or {} end
	for i,row in ipairs(self.rows or {}) do
		local entry = self.results[i]
		row.entry = entry
		if entry then row.text:SetText(tostring(entry.display or "?")); row:Show() else row.text:SetText(""); row:Hide() end
	end
end

function ApiVault.WidgetMethodBrowser:Toggle()
	self:Create()
	if self.frame:IsShown() then self.frame:Hide() else self:Open(self.filter or "All") end
end
