-- Portal Pro :: UI/Widgets/IconPicker.lua
-- Raid target icon and colour insertion for the ad/whisper editors, plus the preview
-- renderer that shows what chat will actually display.

local ADDON_NAME, PP = ...
local UI, T, U = PP.UI, PP.Theme, PP.U

-- {rt1}-{rt8} render on every client. The word forms ({star}, {skull}) are LOCALISED and
-- silently fail to render on non-English clients, so they're deliberately not offered.
local ICON_NAMES = { "Star", "Circle", "Diamond", "Triangle", "Moon", "Square", "Cross", "Skull" }

local SWATCHES = {
	{ hex = "4C9AFF", name = "Blue" },
	{ hex = "34D399", name = "Green" },
	{ hex = "FBBF24", name = "Amber" },
	{ hex = "F87171", name = "Red" },
	{ hex = "C084FC", name = "Purple" },
	{ hex = "F0C674", name = "Gold" },
	{ hex = "E8EAED", name = "White" },
}

--- @param onPick fun(token:string) receives the text to insert
function UI:IconPicker(parent, onPick)
	local f = CreateFrame("Frame", nil, parent)
	f:SetHeight(26)

	local x = 0
	for i = 1, 8 do
		local b = CreateFrame("Button", nil, f)
		b:SetSize(22, 22)
		b:SetPoint("LEFT", x, 0)

		local bg = b:CreateTexture(nil, "BACKGROUND")
		bg:SetAllPoints()
		bg:SetColorTexture(T:C("overlay"))

		local tex = b:CreateTexture(nil, "ARTWORK")
		tex:SetPoint("TOPLEFT", 3, -3)
		tex:SetPoint("BOTTOMRIGHT", -3, 3)
		tex:SetTexture(T:RaidIcon(i))

		b:SetScript("OnEnter", function() bg:SetColorTexture(T:C("hover")) end)
		b:SetScript("OnLeave", function() bg:SetColorTexture(T:C("overlay")) end)
		b:SetScript("OnClick", function() if onPick then onPick("{rt" .. i .. "}") end end)

		UI:Tooltip(b, ICON_NAMES[i], { "Inserts {rt" .. i .. "}", "Renders on all clients, unlike {" .. ICON_NAMES[i]:lower() .. "}" })
		x = x + 24
	end

	x = x + T.space.md

	for _, swatch in ipairs(SWATCHES) do
		local b = CreateFrame("Button", nil, f)
		b:SetSize(16, 16)
		b:SetPoint("LEFT", x, 0)

		local tex = b:CreateTexture(nil, "ARTWORK")
		tex:SetAllPoints()
		local r = tonumber(swatch.hex:sub(1, 2), 16) / 255
		local g = tonumber(swatch.hex:sub(3, 4), 16) / 255
		local bl = tonumber(swatch.hex:sub(5, 6), 16) / 255
		tex:SetColorTexture(r, g, bl, 1)

		UI:Border(b, "borderSubtle")
		b:SetScript("OnClick", function()
			if onPick then onPick("|cff" .. swatch.hex .. "") end
		end)
		UI:Tooltip(b, swatch.name, { "Inserts a colour code", "Close it with |r" })
		x = x + 18
	end

	local endColor = UI:Button(f, "|r", function()
		if onPick then onPick("|r") end
	end, "ghost", 26, 20)
	endColor:SetPoint("LEFT", x + 4, 0)
	UI:Tooltip(endColor, "End colour", { "Inserts |r, which closes a colour code" })

	f:SetWidth(x + 34)
	return f
end

--------------------------------------------------------------------------------

--- Turn an outgoing chat string into what the chat frame will actually show:
--- {rtN} becomes the icon texture. Colour codes already render natively in FontStrings.
function UI:RenderChatPreview(text, iconSize)
	if not text then return "" end
	return (text:gsub("{rt(%d)}", function(n)
		local index = tonumber(n)
		if index and index >= 1 and index <= 8 then
			return T:RaidIconInline(index, iconSize or 12)
		end
		return "{rt" .. n .. "}"
	end))
end
