local addonName, addon = ...

local UI = addon.UI
local LT = addon.LootTracker
local LootDB = LT.DB
local Query = LT.Query
local Data = LT.Data
local UpgradeTracks = LT.UpgradeTracks
local Upgrade = LT.Upgrade

local SOURCE_KEYS = "KEYS"
local SOURCE_RAIDS = "RAIDS"
local SOURCE_CRAFTED = "CRAFTED"

local PRIORITY_TEXT = {
    WANTED = "WANTED",
    HIGH = "HIGH",
    BIS = "BIS",
}

local function CharacterDisplay(character)
    if not character then
        return UI:T("NO_CHARACTER", "NO CHARACTER")
    end

    if character.realm and character.realm ~= "" then
        return (character.name or "?") .. " - " .. character.realm
    end

    return character.name or "?"
end

local function SetDropdownText(dropdown, text)
    UIDropDownMenu_SetText(dropdown, text or UI:T("NONE", "NONE"))
end

local function ClearTooltip()
    GameTooltip:Hide()
end

------------------------------------------------------------
-- ANCHORED CONTEXT MENU
--
-- Repeated UIDropDownMenuTemplate frames inside ScrollFrames can
-- calculate odd screen positions. Loot Tracker uses one shared
-- menu anchored directly to the clicked control instead.
------------------------------------------------------------

function UI:CloseLootAnchoredMenu()
    -- Blizzard_Menu manages native menu lifetime.
    GameTooltip:Hide()
end

function UI:OpenLootAnchoredMenu(anchor, entries, width)
    -- Compatibility wrapper: native Blizzard context menu only.
    self:OpenBlizzardContextMenu(anchor, entries)
end

function UI:SetupLootItemButton(
    button,
    itemID,
    sourceData,
    bossData
)
    local itemInfo =
        Query:GetItemInfo(
            itemID
        )

    local tracked =
        self:GetLootTrackedState(
            itemID
        )

    local trackData =
        self:GetSelectedLootTrackData()

    local itemLink =
        Upgrade:BuildPreviewItemLink(
            itemID,
            self.LootSpecID,
            trackData
            and trackData.entry
            or nil
        )

    button.ItemID = itemID

    button.Icon:SetTexture(
        itemInfo.icon
    )

    button.Border:SetColorTexture(
        unpack(
            tracked
            and self.COLORS.gold
            or self.COLORS.border
        )
    )

    button.Badge:SetText(
        tracked
        and (
            tracked.priority ==
                LootDB.PRIORITY_BIS
            and "B"
            or (
                tracked.priority ==
                    LootDB.PRIORITY_HIGH
                and "H"
                or "W"
            )
        )
        or ""
    )

    button:SetScript(
        "OnEnter",
        function()
            self:ShowLootItemTooltip(
                button,
                itemLink,
                tracked,
                trackData
                and trackData.entry
                or nil
            )
        end
    )

    button:SetScript(
        "OnLeave",
        ClearTooltip
    )

    button:SetScript(
        "OnClick",
        function()
            self:OpenLootPriorityMenu(
                button,
                itemID,
                sourceData,
                bossData,
                trackData
            )
        end
    )
end

function UI:CreateLootIconButton(
    parent,
    x,
    y
)
    local button =
        CreateFrame(
            "Button",
            nil,
            parent
        )

    button:SetSize(
        38,
        38
    )

    button:SetPoint(
        "TOPLEFT",
        parent,
        "TOPLEFT",
        x,
        y
    )

    local border =
        button:CreateTexture(
            nil,
            "BACKGROUND"
        )

    button.Border = border

    border:SetPoint(
        "TOPLEFT",
        button,
        "TOPLEFT",
        -1,
        1
    )

    border:SetPoint(
        "BOTTOMRIGHT",
        button,
        "BOTTOMRIGHT",
        1,
        -1
    )

    local icon =
        button:CreateTexture(
            nil,
            "ARTWORK"
        )

    button.Icon = icon

    icon:SetPoint(
        "TOPLEFT",
        button,
        "TOPLEFT",
        2,
        -2
    )

    icon:SetPoint(
        "BOTTOMRIGHT",
        button,
        "BOTTOMRIGHT",
        -2,
        2
    )

    icon:SetTexCoord(
        0.08,
        0.92,
        0.08,
        0.92
    )

    local badge =
        button:CreateFontString(
            nil,
            "OVERLAY"
        )

    button.Badge = badge

    badge:SetFontObject(
        GameFontNormalSmall
    )

    badge:SetPoint(
        "BOTTOMRIGHT",
        button,
        "BOTTOMRIGHT",
        -2,
        2
    )

    badge:SetTextColor(
        unpack(self.COLORS.gold)
    )

    return button
end

