local _, namespace = ...

local WTUI = namespace.WildThingsUI
local Theme = namespace.Theme

function namespace:CreateSpellIcon(parent)
    local spellIcon = CreateFrame("Button", nil, parent)

    spellIcon.OnEnter = function() end
    spellIcon.OnLeave = function() end

    spellIcon:SetScript("OnEnter", function(_self) _self.OnEnter() end)
    spellIcon:SetScript("OnLeave", function(_self) _self.OnLeave() end)

    spellIcon:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square", "ADD")

    spellIcon.tex = spellIcon:CreateTexture(nil, "BACKGROUND")
    spellIcon.tex:SetAllPoints(spellIcon)
    spellIcon.tex:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    spellIcon.tex:SetSnapToPixelGrid(false)
    spellIcon.tex:SetTexelSnappingBias(0)

    -- Rounded icon: the mask has to clip the icon art AND Blizzard's square highlight
    -- texture, otherwise both keep 90-degree corners inside the rounded border below.
    WTUI:AddRoundedMask(spellIcon, Theme.RADIUS_SM)
    spellIcon.tex:AddMaskTexture(spellIcon.roundedMask)

    local highlight = spellIcon:GetHighlightTexture()
    if highlight then
        highlight:AddMaskTexture(spellIcon.roundedMask)
    end

    WTUI:AddRoundedBorder(spellIcon, Theme.RADIUS_SM, 1)
    WTUI:AddTooltip(spellIcon)

    function spellIcon:SetSpellID(spellID)
        spellID = tonumber(spellID)

        local spellInfo = spellID and WTUI.GetSpellInfo(spellID)

        if spellInfo then
			local serverTime = GetServerTime()
			local cachedSpellInfo = WTUI.spellDescriptionCache[spellID]

			if cachedSpellInfo and cachedSpellInfo.time > serverTime - 604800 then -- Cached spell info becomes stale after a week
				spellIcon.tooltipText = string.format("|cFFFFCC00%s|n%d|r|n|n%s", spellInfo.name, spellID, cachedSpellInfo.description)
			else
				local spell = Spell:CreateFromSpellID(spellID)

				spell:ContinueOnSpellLoad(
					function()
						local description = spell:GetSpellDescription()

						spellIcon.tooltipText = string.format("|cFFFFCC00%s|n%d|r", spellInfo.name, spellID)

						if description and description ~= "" then
							spellIcon.tooltipText = string.format("%s|n|n%s", spellIcon.tooltipText, description)

                            WTUI.spellDescriptionCache[spellID] = {
                                time = serverTime,
                                description = description
                            }
						end
					end
				)
			end

            spellIcon.tex:SetTexture(spellInfo.iconID)
        else
            spellIcon.tooltipText = "|cffffffffInvalid spell ID|r"
            spellIcon.tex:SetTexture(134400)
        end
    end

    spellIcon:SetSpellID()

    return spellIcon
end
