-- HDG_HT_W_TrophyShelf.lua  (extracted, B2 unchanged)
-- "Trophy shelf  X / Y" inline header + single-row strip of trophy icons on
-- a wood-sign background. Overflow scrolls horizontally via mouse wheel
-- (no scrollbar — clean shelf look).

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
privateDB.HDG_HT.Widgets = privateDB.HDG_HT.Widgets or {}
local W = { id = "trophyShelf", title = "Trophy Shelf" }
privateDB.HDG_HT.Widgets.TrophyShelf = W

local HEADER_H = 14
local SHELF_PAD_X = 6   -- inset between wood frame and first/last icon
local SHELF_PAD_Y = 4

function W.Create(parent, dims)
    local self = { iconBtns = {} }
    local Theme  = privateDB.HDG_HT.Theme
    local scheme = Theme.GetCurrentScheme()
    local iconSize = (dims and dims.iconSize) or 28
    local iconGap  = (dims and dims.iconGap)  or 2
    -- iconDrop controls how far the icon BOTTOM sits below the TOP of the
    -- wood plank. Default 2 = the icons appear to stand on the shelf.
    -- Higher values pull the icons further down into the wood.
    local iconDrop = (dims and dims.iconDrop) or 2
    -- showHeader: when false, skips the "Trophy shelf X/Y" header line +
    -- accent bar entirely. Used inside the DecoratorProfile composite.
    local showHeader = (dims == nil) or (dims.showHeader ~= false)

    -- Vertical accent bar + header (skipped when showHeader=false)
    if showHeader then
        self.accent = parent:CreateTexture(nil, "ARTWORK")
        self.accent:SetTexture("Interface\\Buttons\\WHITE8x8")
        self.accent:SetSize(2, 12)
        self.accent:SetPoint("TOPLEFT", parent, "TOPLEFT", 2, -1)
        self.accent:SetVertexColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, 0.8)

        self.header = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        self.header:SetPoint("LEFT", self.accent, "RIGHT", 6, 0)
        self.header:SetTextColor(scheme.accent.r, scheme.accent.g, scheme.accent.b)
        Theme.ApplyFont(self.header, scheme, "header")
    end

    -- Wood-shelf frame (same atlas as tab title bar / styles row backgrounds).
    -- Anchor TOP shifts down only when the header is present.
    self.shelfFrame = CreateFrame("Frame", nil, parent)
    if showHeader then
        self.shelfFrame:SetPoint("TOPLEFT", parent, "TOPLEFT", 2, -(2 + HEADER_H))
    else
        self.shelfFrame:SetPoint("TOPLEFT", parent, "TOPLEFT", 2, -2)
    end
    self.shelfFrame:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 0, 0)

    -- Wood plank: bottom half only — trophies appear to sit ON the shelf
    -- rather than be embedded in it. Anchored bottom-left / bottom-right
    -- so it stretches across the full width and follows resize.
    self.woodBg = self.shelfFrame:CreateTexture(nil, "BACKGROUND", nil, 1)
    self.woodBg:SetPoint("BOTTOMLEFT",  self.shelfFrame, "BOTTOMLEFT",  0, 0)
    self.woodBg:SetPoint("BOTTOMRIGHT", self.shelfFrame, "BOTTOMRIGHT", 0, 0)
    self.woodBg:SetHeight(1)  -- updated in Resize from current parent height
    self.woodBg:SetAtlas("housing-woodsign")
    self.woodBg:SetAlpha(0.65)
    -- Resize hook: keep wood = half of shelf height + reposition icons so
    -- they continue to sit on top of the wood as the panel grows/shrinks.
    self.shelfFrame:SetScript("OnSizeChanged", function(_, _, h)
        local woodH = math.max(1, math.floor((h or 0) / 2))
        self.woodBg:SetHeight(woodH)
        if self._layoutIcons then self:_layoutIcons() end
    end)

    -- Shelf-edge contact shadow — a thin dark strip running the full width
    -- of the shelf at the line where the trophies meet the wood. Sits
    -- BELOW the icons in z-order (icons are in scrollContent which renders
    -- above shelfFrame's BACKGROUND layer). Positioned in :_layoutIcons
    -- since its Y offset depends on shelf height.
    self.edgeShadow = self.shelfFrame:CreateTexture(nil, "BACKGROUND", nil, 1)
    self.edgeShadow:SetColorTexture(0, 0, 0, 0.45)

    -- ScrollFrame clips overflow horizontally; no visible scrollbar.
    self.scroll = CreateFrame("ScrollFrame", nil, self.shelfFrame)
    self.scroll:SetPoint("TOPLEFT",     self.shelfFrame, "TOPLEFT",     SHELF_PAD_X,  -SHELF_PAD_Y)
    self.scroll:SetPoint("BOTTOMRIGHT", self.shelfFrame, "BOTTOMRIGHT", -SHELF_PAD_X,  SHELF_PAD_Y)

    self.scrollContent = CreateFrame("Frame", nil, self.scroll)
    self.scrollContent:SetSize(1, iconSize)  -- width set in Refresh based on item count
    self.scroll:SetScrollChild(self.scrollContent)

    -- Mouse-wheel scroll. One icon-step per click, clamped to 0..max range.
    self.shelfFrame:EnableMouseWheel(true)
    self.shelfFrame:SetScript("OnMouseWheel", function(_, delta)
        local cur = self.scroll:GetHorizontalScroll()
        local max = self.scroll:GetHorizontalScrollRange() or 0
        local step = iconSize + iconGap
        local off = math.max(0, math.min(max, cur - delta * step))
        self.scroll:SetHorizontalScroll(off)
    end)

    -- Build/reuse icon buttons inside scrollContent.
    local function ensureIcon(i)
        if self.iconBtns[i] then return self.iconBtns[i] end
        local b = CreateFrame("Button", nil, self.scrollContent)
        b:SetSize(iconSize, iconSize)
        -- Contact shadow — soft oval that OVERLAPS the icon base by 1px so
        -- the shadow reads as the trophy resting ON the shelf, not casting
        -- through air. Width ~85% icon, height ~20%, moderate alpha so it
        -- darkens the base without overpowering it.
        b.shadow = b:CreateTexture(nil, "BACKGROUND", nil, 2)
        b.shadow:SetAtlas("groupfinder-eye-highlight")
        b.shadow:SetVertexColor(0, 0, 0, 0.65)
        b.shadow:SetBlendMode("BLEND")
        b.shadow:SetSize(iconSize * 0.85, math.max(3, math.floor(iconSize * 0.20)))
        b.shadow:SetPoint("BOTTOM", b, "BOTTOM", -3, -2)
        b.tex = b:CreateTexture(nil, "ARTWORK")
        b.tex:SetAllPoints()
        b.tex:SetTexCoord(0.08, 0.92, 0.08, 0.92)
        b:SetScript("OnEnter", function(slf)
            if slf._itemID then
                GameTooltip:SetOwner(slf, "ANCHOR_RIGHT")
                GameTooltip:SetItemByID(slf._itemID)
                GameTooltip:Show()
            end
        end)
        b:SetScript("OnLeave", function() GameTooltip:Hide() end)
        b:Hide()
        self.iconBtns[i] = b
        return b
    end

    -- Anchor icons BOTTOMLEFT of scrollContent at y = woodHeight - 2.
    -- Effect: icon bottoms rest just above the top edge of the wood plank,
    -- with a 2px overlap so they look like they're standing ON the shelf.
    function self:_layoutIcons()
        local shelfH = self.shelfFrame:GetHeight() or 0
        local woodH  = math.floor(shelfH / 2)
        local yOff   = math.max(0, woodH - iconDrop)
        -- Reposition the shelf-edge strip shadow to sit just below the
        -- icons' bottom line. scroll's bottom is SHELF_PAD_Y above the
        -- shelfFrame bottom, so icon BOTTOM in shelfFrame coords is
        -- SHELF_PAD_Y + yOff. Strip sits 1px below that.
        local stripY = math.max(0, SHELF_PAD_Y + yOff - 1)
        if self.edgeShadow then
            self.edgeShadow:ClearAllPoints()
            self.edgeShadow:SetPoint("BOTTOMLEFT",  self.shelfFrame, "BOTTOMLEFT",  SHELF_PAD_X,  stripY)
            self.edgeShadow:SetPoint("BOTTOMRIGHT", self.shelfFrame, "BOTTOMRIGHT", -SHELF_PAD_X, stripY)
            self.edgeShadow:SetHeight(2)
        end
        for i, b in ipairs(self.iconBtns) do
            if b:IsShown() then
                b:ClearAllPoints()
                b:SetPoint("BOTTOMLEFT", self.scrollContent, "BOTTOMLEFT",
                    (i - 1) * (iconSize + iconGap), yOff)
            end
        end
    end

    function self:Refresh(snap)
        local items     = (snap and snap.trophies) or {}
        if self.header then
            local collected = (snap and snap.trophiesCollected) or 0
            local total     = (snap and snap.trophiesTotal) or #items
            local cAccent, cDim = Theme.C_Accent(), Theme.C_Dim()
            self.header:SetText(string.format("%sTrophy shelf|r  %s%d / %d|r",
                cAccent, cDim, collected, total))
        end

        for i, item in ipairs(items) do
            local b = ensureIcon(i)
            b.tex:SetTexture(item.iconID or "Interface\\Icons\\INV_Misc_QuestionMark")
            b._itemID = item.itemID
            b:Show()
        end
        for i = #items + 1, #self.iconBtns do self.iconBtns[i]:Hide() end

        -- Size the scroll content so GetHorizontalScrollRange() reports
        -- a valid max (range = contentW - viewportW). scrollContent height
        -- matches the shelf so icons can bottom-anchor against the wood.
        local contentW = math.max(1, #items * iconSize + math.max(0, #items - 1) * iconGap)
        self.scrollContent:SetSize(contentW, math.max(iconSize, self.shelfFrame:GetHeight() or iconSize))
        self.scroll:SetHorizontalScroll(0)
        self:_layoutIcons()
    end

    function self:Resize(_, _) end  -- shelf fills parent via anchors; no manual sizing

    return self
end

return W
