local addonName, addon = ...

addon.UI = addon.UI or {}
local UI = addon.UI

local function CreateLabel(parent, text, x, y)
    local label = parent:CreateFontString(nil, "OVERLAY")
    label:SetFontObject(GameFontNormalSmall)
    label:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y or -12)
    label:SetText(text)
    label:SetTextColor(unpack(UI.COLORS.muted))
    return label
end

local function CreateSelector(parent, x, y, width, kind)
    local dropdown = UI:CreateBlizzardDropdown(
        parent,
        width,
        "SELECT",
        function(_, rootDescription)
            local entries = UI:GetLootSelectorEntries(kind) or {}
            for _, entry in ipairs(entries) do
                UI:AddBlizzardMenuEntry(rootDescription, entry)
            end
        end
    )
    dropdown:SetHeight(28)
    dropdown:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y or -28)
    dropdown.Kind = kind
    return dropdown
end

function UI:RefreshLootTrackerSelectors()
    if not self.LootTrackerFrame then
        return
    end

    self:EnsureLootTrackerSelection()

    local selectors = self.LootSelectors or {}
    for kind, button in pairs(selectors) do
        if button and self.GetLootSelectorText then
            self:SetBlizzardDropdownText(button, self:GetLootSelectorText(kind))
        end
    end

    local isRaid = self.LootSource == "RAIDS"
    local isCrafted = self.LootSource == "CRAFTED"

    if self.LootTrackerTitle then
        self.LootTrackerTitle:SetText(self.LootViewMode == "myloot"
            and self:T("MY_LOOT", "MY LOOT")
            or self:T("LOOT_BROWSER", "LOOT BROWSER"))
    end
    if self.LootTrackerSubtitle then
        if self.LootViewMode == "myloot" then
            self.LootTrackerSubtitle:SetText(self:T("MY_LOOT_SUBTITLE", "Review the items you selected from raids, Mythic+ and crafting"))
        else
            self.LootTrackerSubtitle:SetText(self:T("LOOT_BROWSER_SUBTITLE", "Browse available loot and add useful items to your personal list"))
        end
    end

    if self.LootDifficultyLabel then
        self.LootDifficultyLabel:SetShown(isRaid)
    end
    if selectors.difficulty then
        selectors.difficulty:SetShown(isRaid)
    end

    if self.LootMaterialLabel then
        self.LootMaterialLabel:SetShown(isCrafted)
    end
    if selectors.material then
        selectors.material:SetShown(isCrafted)
    end

    if self.LootTrackLabel then
        self.LootTrackLabel:SetShown(not isCrafted)
    end
    if selectors.track then
        selectors.track:SetShown(not isCrafted)
    end

end

function UI:InitializeLootTracker()
    if self.LootTrackerFrame then
        return
    end

    local parent = self.PageHost or self.MainFrame
    if not parent then
        return
    end

    local frame = CreateFrame("Frame", "WhatsNextLootTrackerFrame", parent, "BackdropTemplate")
    self.LootTrackerFrame = frame

    frame:SetAllPoints(parent)
    frame:SetFrameLevel(parent:GetFrameLevel() + 1)
    self:ApplyPanelBackdrop(frame)
    if self.RegisterTypographyRoot then
        self:RegisterTypographyRoot(frame)
    end
    frame:Hide()

    local title = frame:CreateFontString(nil, "OVERLAY")
    title:SetFontObject(GameFontNormalLarge)
    title:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -12)
    title:SetText(self:T("LOOT_BROWSER", "LOOT BROWSER"))
    self.LootTrackerTitle = title
    title:SetTextColor(unpack(self.COLORS.gold))

    local subtitle = frame:CreateFontString(nil, "OVERLAY")
    subtitle:SetFontObject(GameFontHighlightSmall)
    subtitle:SetPoint("LEFT", title, "RIGHT", 12, 0)
    subtitle:SetPoint("RIGHT", frame, "RIGHT", -20, 0)
    subtitle:SetJustifyH("LEFT")
    subtitle:SetWordWrap(false)
    subtitle:SetText(self:T("LOOT_BROWSER_SUBTITLE", "Browse available loot and add useful items to your personal list"))
    self.LootTrackerSubtitle = subtitle
    subtitle:SetTextColor(unpack(self.COLORS.muted))

    local controls = CreateFrame("Frame", nil, frame, "BackdropTemplate")
    self.LootControls = controls
    controls:SetPoint("TOPLEFT", frame, "TOPLEFT", 12, -42)
    controls:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -12, -42)
    controls:SetHeight(154)
    controls:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8X8" })
    controls:SetBackdropColor(unpack(self.COLORS.panelAlt))

    self.LootSelectors = {}

    -- Row 1: character context. Keep identity separate from loot filters.
    local characterGroup = controls:CreateFontString(nil, "OVERLAY")
    characterGroup:SetFontObject(GameFontNormalSmall)
    characterGroup:SetPoint("TOPLEFT", controls, "TOPLEFT", 10, -8)
    characterGroup:SetText(self:T("CHARACTER_SPEC", "CHARACTER & SPECIALIZATION"))
    characterGroup:SetTextColor(unpack(self.COLORS.gold))

    CreateLabel(controls, self:T("CHARACTER", "CHARACTER"), 10, -28)
    self.LootSelectors.character = CreateSelector(controls, 10, -44, 190, "character")

    CreateLabel(controls, self:T("SPEC", "SPEC"), 208, -28)
    self.LootSelectors.spec = CreateSelector(controls, 208, -44, 160, "spec")

    -- Row 2: loot source filters.
    local filterGroup = controls:CreateFontString(nil, "OVERLAY")
    filterGroup:SetFontObject(GameFontNormalSmall)
    filterGroup:SetPoint("TOPLEFT", controls, "TOPLEFT", 10, -78)
    filterGroup:SetText(self:T("LOOT_FILTERS", "LOOT FILTERS"))
    filterGroup:SetTextColor(unpack(self.COLORS.gold))

    CreateLabel(controls, self:T("SOURCE", "SOURCE"), 10, -98)
    self.LootSelectors.source = CreateSelector(controls, 10, -114, 140, "source")

    CreateLabel(controls, self:T("SLOT", "SLOT"), 158, -98)
    self.LootSelectors.slot = CreateSelector(controls, 158, -114, 130, "slot")

    self.LootDifficultyLabel = CreateLabel(controls, self:T("DIFFICULTY", "DIFFICULTY"), 296, -98)
    self.LootSelectors.difficulty = CreateSelector(controls, 296, -114, 140, "difficulty")

    self.LootMaterialLabel = CreateLabel(controls, self:T("EQUIPMENT_TYPE", "EQUIPMENT TYPE"), 296, -98)
    self.LootSelectors.material = CreateSelector(controls, 296, -114, 140, "material")
    self.LootMaterialLabel:Hide()
    self.LootSelectors.material:Hide()

    self.LootTrackLabel = CreateLabel(controls, self:T("TARGET_ILVL_TRACK", "TARGET ILVL / TRACK"), 444, -98)
    self.LootSelectors.track = CreateSelector(controls, 444, -114, 210, "track")

    local scroll = CreateFrame(
        "ScrollFrame",
        "WhatsNextLootTrackerScrollFrame",
        frame,
        "UIPanelScrollFrameTemplate"
    )
    self.LootScrollFrame = scroll
    scroll:SetPoint("TOPLEFT", controls, "BOTTOMLEFT", 4, -10)
    scroll:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -30, 12)

    local content = CreateFrame("Frame", "WhatsNextLootTrackerContent", scroll)
    self.LootContent = content
    content:SetWidth(math.max(900, frame:GetWidth() - 50))
    content:SetHeight(100)
    scroll:SetScrollChild(content)

    frame:SetScript("OnHide", function()
        if self.CloseLootAnchoredMenu then
            self:CloseLootAnchoredMenu()
        end
        GameTooltip:Hide()
    end)

    self:EnsureLootTrackerSelection()
    self:RefreshLootTrackerSelectors()
end
