--[[
	PreBid - Test Frame for Loot Browsing
	A simple experiment to isolate loot fetching and display.
]]--

local AddonName = "PreBid"
local PreBid = LibStub("AceAddon-3.0"):GetAddon(AddonName)
local AceGUI = LibStub("AceGUI-3.0")

local TestFrame = {}
PreBid.modules.TestFrame = TestFrame

local frame

-- Hardcoded test case
local TEST_INSTANCE_ID = 1302 -- Manaforge Omega
local TEST_ENCOUNTER_ID = 2684 -- Plexus Sentinel
local TEST_DIFFICULTY_ID = 16  -- Mythic

function TestFrame:Show()
	if frame and frame:IsShown() then
		frame:Hide()
		frame = nil
		return
	end

	PreBid:Print("Starting loot display experiment...")

	-- Create a new frame
	frame = AceGUI:Create("Frame")
	frame:SetTitle("Loot Test: Plexus Sentinel (Mythic)")
	frame:SetLayout("Flow")
	frame:SetWidth(600)
	frame:SetHeight(400)

	frame:SetCallback("OnClose", function(widget)
		frame = nil
	end)

	local statusLabel = AceGUI:Create("Label")
	statusLabel:SetText("Requesting loot data...")
	statusLabel:SetFullWidth(true)
	frame:AddChild(statusLabel)

	-- Use the LootBrowser's request function, but with our own simple callback
	PreBid.modules.LootBrowser:RequestBossLoot(TEST_INSTANCE_ID, TEST_ENCOUNTER_ID, TEST_DIFFICULTY_ID, function(loot)
		statusLabel:SetText(string.format("Callback received with %d loot items.", #loot))

		if #loot == 0 then
			PreBid:Print("|cffff0000[TEST]|r Callback received, but no loot was found.")
			return
		end

		PreBid:Print(string.format("|cff00ff00[TEST]|r Callback received with %d items. Creating icons.", #loot))

		-- Display loot items
		for i, item in ipairs(loot) do
			if item and item.icon and item.link then
				local icon = AceGUI:Create("Icon")
				icon:SetImage(item.icon)
				icon:SetLabel(item.name)
				icon:SetImageSize(40, 40)
				icon:SetWidth(150) -- Give it some space

				-- Tooltip handling
				icon:SetCallback("OnEnter", function(widget)
					GameTooltip:SetOwner(widget.frame, "ANCHOR_RIGHT")
					GameTooltip:SetHyperlink(item.link)
					GameTooltip:Show()
				end)
				icon:SetCallback("OnLeave", function(widget)
					GameTooltip:Hide()
				end)

				frame:AddChild(icon)
			else
				PreBid:Print(string.format("|cffff0000[TEST]|r Item #%d in loot table was invalid.", i))
			end
		end
		PreBid:Print("[TEST] Finished processing loot.")
	end)
end

return TestFrame
