local addonName, NS = ...
local GR = NS.GR -- Reference the Core Frame

-- =========================================================================
-- UI: SHARED HELPERS
-- =========================================================================

local function CreateBg(f, r, g, b, a)
    local t = f:CreateTexture(nil, "BACKGROUND")
    t:SetAllPoints()
    t:SetColorTexture(r, g, b, a)
    f.bg = t
end

local function CreateBorder(f, r, g, b, a)
    -- Surgical Adjustment: Ghost Frame Implementation
    -- Prevents background overlap by using a transparent center child frame
    
    -- Cleanup legacy texture if present
    if f.border and not f.border.SetBackdrop then
        f.border:Hide()
        f.border = nil
    end

    if not f.border then
        local border = CreateFrame("Frame", nil, f, "BackdropTemplate")
        border:SetAllPoints()
        border:SetBackdrop({
            bgFile = nil,
            edgeFile = "Interface\\Buttons\\WHITE8x8",
            edgeSize = 1,
        })
        f.border = border
    end
    
    f.border:SetBackdropBorderColor(r, g, b, a)
end

-- [[ NEW: THICK BORDER HELPER (2px) ]]
local function CreateThickBorder(f, r, g, b, a, thickness)
    thickness = thickness or 2
    if f.thickBorder then 
        for _, t in ipairs(f.thickBorder) do t:Hide() end 
    end
    
    local lines = {}
    
    -- Top
    local t = f:CreateTexture(nil, "BORDER")
    t:SetPoint("TOPLEFT", 0, 0)
    t:SetPoint("TOPRIGHT", 0, 0)
    t:SetHeight(thickness)
    t:SetColorTexture(r,g,b,a)
    table.insert(lines, t)
    
    -- Bottom
    local b_line = f:CreateTexture(nil, "BORDER")
    b_line:SetPoint("BOTTOMLEFT", 0, 0)
    b_line:SetPoint("BOTTOMRIGHT", 0, 0)
    b_line:SetHeight(thickness)
    b_line:SetColorTexture(r,g,b,a)
    table.insert(lines, b_line)
    
    -- Left
    local l = f:CreateTexture(nil, "BORDER")
    l:SetPoint("TOPLEFT", 0, 0)
    l:SetPoint("BOTTOMLEFT", 0, 0)
    l:SetWidth(thickness)
    l:SetColorTexture(r,g,b,a)
    table.insert(lines, l)
    
    -- Right
    local ri = f:CreateTexture(nil, "BORDER")
    ri:SetPoint("TOPRIGHT", 0, 0)
    ri:SetPoint("BOTTOMRIGHT", 0, 0)
    ri:SetWidth(thickness)
    ri:SetColorTexture(r,g,b,a)
    table.insert(lines, ri)
    
    f.thickBorder = lines
end

local function SkinButton(btn)
    if not btn then return end
    
    btn:SetNormalTexture("")
    btn:SetHighlightTexture("")
    btn:SetPushedTexture("")
    btn:SetDisabledTexture("")
    
    if btn.Left then btn.Left:SetAlpha(0) end
    if btn.Middle then btn.Middle:SetAlpha(0) end
    if btn.Right then btn.Right:SetAlpha(0) end
    if btn.Top then btn.Top:SetAlpha(0) end
    if btn.Bottom then btn.Bottom:SetAlpha(0) end
    
    CreateBg(btn, 0.2, 0.2, 0.2, 1)
    CreateBorder(btn, 0, 0, 0, 1)
    
    btn:HookScript("OnEnter", function(s) 
        if s.bg then s.bg:SetColorTexture(0.3, 0.3, 0.3, 1) end 
    end)
    btn:HookScript("OnLeave", function(s) 
        if s.bg then s.bg:SetColorTexture(0.2, 0.2, 0.2, 1) end 
    end)
    btn:HookScript("OnMouseDown", function(s) 
        if s.bg then s.bg:SetColorTexture(0.1, 0.1, 0.1, 1) end 
    end)
    btn:HookScript("OnMouseUp", function(s) 
        if s.bg then s.bg:SetColorTexture(0.3, 0.3, 0.3, 1) end 
    end)
    
    local fs = btn:GetFontString()
    if fs then fs:SetTextColor(1, 1, 1) end
end

-- =========================================================================
-- SHARED SCROLLBAR STYLING (Matches Raid Roster / CreateRobustVerticalScrollbar)
-- Usage: NS.StyleScrollBar(scrollFrame) where scrollFrame is a UIPanelScrollFrameTemplate
--    or: NS.StyleScrollBar(scrollBar)   where scrollBar is a raw ScrollBar/Slider frame
-- =========================================================================
local function StyleScrollBar(scrollFrameOrBar)
    if not scrollFrameOrBar then return end
    
    -- Accept either a ScrollFrame (which has .ScrollBar) or a raw ScrollBar
    local scrollBar = scrollFrameOrBar.ScrollBar or scrollFrameOrBar
    if not scrollBar.GetThumbTexture then return end -- Safety: not a scrollbar
    
    -- 1. Width
    scrollBar:SetWidth(18)
    
    -- 2. Dark track background
    if not scrollBar._styledBg then
        local bg = scrollBar:CreateTexture(nil, "BACKGROUND")
        bg:SetAllPoints()
        bg:SetColorTexture(0.1, 0.1, 0.1, 1)
        scrollBar._styledBg = bg
    end
    
    -- 3. Thumb: 14x24 gray, gold on drag
    local thumb = scrollBar:GetThumbTexture()
    if thumb then
        thumb:SetSize(14, 24)
        thumb:SetColorTexture(0.5, 0.5, 0.5, 1)
        
        -- Gold highlight on drag (matches Raid Roster)
        if not scrollBar._thumbHooked then
            scrollBar:HookScript("OnValueChanged", function()
                if thumb then thumb:SetColorTexture(0.8, 0.6, 0, 1) end
            end)
            scrollBar:HookScript("OnMouseUp", function()
                if thumb then thumb:SetColorTexture(0.5, 0.5, 0.5, 1) end
            end)
            scrollBar._thumbHooked = true
        end
    end
    
    -- 4. Style up/down arrow buttons (dark theme with ▲/▼ glyphs)
    local function StyleArrowBtn(btn, glyph)
        if not btn then return end
        btn:SetAlpha(1); btn:SetSize(18, 18)
        btn:SetNormalTexture(""); btn:SetPushedTexture(""); btn:SetHighlightTexture(""); btn:SetDisabledTexture("")
        if not btn._styledBg then
            local bg = btn:CreateTexture(nil, "BACKGROUND")
            bg:SetAllPoints(); bg:SetColorTexture(0.15, 0.15, 0.15, 1)
            btn._styledBg = bg
        end
        if not btn._arrowText then
            local fs = btn:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
            fs:SetPoint("CENTER", 0, 0); fs:SetText(glyph); fs:SetTextColor(0.7, 0.7, 0.7)
            btn._arrowText = fs
        end
        btn:HookScript("OnEnter", function(s) if s._styledBg then s._styledBg:SetColorTexture(0.3, 0.3, 0.3, 1) end; if s._arrowText then s._arrowText:SetTextColor(1, 0.82, 0) end end)
        btn:HookScript("OnLeave", function(s) if s._styledBg then s._styledBg:SetColorTexture(0.15, 0.15, 0.15, 1) end; if s._arrowText then s._arrowText:SetTextColor(0.7, 0.7, 0.7) end end)
    end
    local scrollBarName = scrollBar:GetName()
    if scrollBarName then
        StyleArrowBtn(_G[scrollBarName .. "ScrollUpButton"], "▲")
        StyleArrowBtn(_G[scrollBarName .. "ScrollDownButton"], "▼")
    end
    if scrollBar.ScrollUpButton then StyleArrowBtn(scrollBar.ScrollUpButton, "▲") end
    if scrollBar.ScrollDownButton then StyleArrowBtn(scrollBar.ScrollDownButton, "▼") end
end

-- Export helpers to NS for global access
NS.CreateBg = CreateBg
NS.CreateBorder = CreateBorder
NS.CreateThickBorder = CreateThickBorder
NS.SkinButton = SkinButton
NS.StyleScrollBar = StyleScrollBar