-- ============================================================
-- RealNibba.ElvUISkin.lua
-- Uses S:AddCallbackForAddon so it fires after our frames exist.
-- Close buttons detected by .isCloseBtn flag (set at creation).
-- Dynamic list rows (routes, sessions) are re-skinned on refresh.
-- ============================================================
if not IsAddOnLoaded("ElvUI") then return end

local E, _, V, P, G = unpack(ElvUI)
local S = E:GetModule("Skins")

-- ── Recursive button skinner ─────────────────────────────────
local function SkinButtons(frame)
    if not frame then return end
    for _, child in ipairs({frame:GetChildren()}) do
        if child.GetObjectType and child:GetObjectType() == "Button" then
            if not child.isSkinned then
                if child.isCloseBtn then
                    -- UIPanelCloseButton: use dedicated handler, keeps correct size
                    S:HandleCloseButton(child)
                else
                    S:HandleButton(child, true)
                end
                child.isSkinned = true
            end
        end
        SkinButtons(child)
    end
end

-- ── Window skinner (backdrop + all buttons) ──────────────────
local function SkinWindow(frame)
    if not frame then return end
    if frame.SetTemplate then frame:SetTemplate("Transparent") end
    SkinButtons(frame)
end

-- ── Public: called from RefreshList / RefreshSessions ────────
-- so dynamically created row-buttons also get skinned
function _G.RNPackSkinFrame(frame)
    SkinButtons(frame)
end

-- ── Main skin callback ───────────────────────────────────────
local function SkinRealNibbaPack()
    local P = RNPack
    if not P then return end

    -- Farm window
    SkinWindow(P.farmFrame)
    local s1 = _G["RNPackFarmScroll"]
    if s1 then S:HandleScrollBar(s1) end

    -- Routes window
    SkinWindow(P.routesFrame)
    local s2 = _G["RNPackRtScroll"]
    if s2 then S:HandleScrollBar(s2) end
    local impBox = _G["RNPackImportBox"]
    if impBox then S:HandleEditBox(impBox) end
    local nameInput = _G["RNPackRouteNameInput"]
    if nameInput then S:HandleEditBox(nameInput) end
    for i = 1, 9 do
        local cb = _G["RNPackRTC"..i]
        if cb then S:HandleCheckBox(cb) end
    end

    -- Sessions popup (replaced old config frame)
    SkinWindow(_G["RNPackSessPopup"])
    SkinWindow(_G["RNPackSessDetail"])
    local s3 = _G["RNPackSessScroll"];       if s3 then S:HandleScrollBar(s3) end
    local s4 = _G["RNPackSessDetailScroll"]; if s4 then S:HandleScrollBar(s4) end

    -- Bag scanner
    SkinWindow(_G["RNPackBagPopup"])
    local s5 = _G["RNPackBagScroll"]; if s5 then S:HandleScrollBar(s5) end

    -- AH / Mail popups
    SkinWindow(_G["RNPackAHPopup"])
    SkinWindow(_G["RNPackMailPopup"])

    -- Price edit popup
    SkinWindow(_G["RNPackPriceEdit"])
    for _, n in ipairs({"RNPackPEGold","RNPackPESilver","RNPackPECopper"}) do
        local box = _G[n]; if box then S:HandleEditBox(box) end
    end

    -- Export popup
    SkinWindow(_G["RNPackExportPopup"])
    local expBox = _G["RNPackExportBox"]; if expBox then S:HandleEditBox(expBox) end

    -- Dropdowns (item right-click, price source)
    for _, n in ipairs({"RNPackItemDropdown","RNPackPriceDD"}) do
        local dd = _G[n]
        if dd and dd.SetTemplate then
            dd:SetTemplate("Transparent"); SkinButtons(dd)
        end
    end

    -- Minimap button
    local mmBtn = _G["RNPackMinimapBtn"]
    if mmBtn then
        if mmBtn.border then mmBtn.border:SetAlpha(0) end
        mmBtn:SetTemplate("Default"); mmBtn:StyleButton()
        mmBtn:SetSize(22,22)
        if mmBtn.icon then mmBtn.icon:SetTexCoord(0.05,0.95,0.05,0.95); mmBtn.icon:SetInside() end
    end

    -- AceConfigDialog is auto-skinned by ElvUI's Ace3 module
end

S:AddCallbackForAddon("RealNibba.Pack", "RealNibbaPack_Skin", SkinRealNibbaPack)
