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

-- =========================================================================
-- UI: SHARED HELPERS (TBC Port)
-- =========================================================================

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

-- [[ SCROLLBAR STYLING HELPER ]]
-- Usage: NS.StyleScrollBar(scrollFrame) where scrollFrame is a UIPanelScrollFrameTemplate
-- or FauxScrollFrameTemplate based scroll frame. Applies the canonical Raid Roster design.
local function StyleScrollBar(scrollFrameOrBar)
    if not scrollFrameOrBar then return end
    
    local scrollBar = scrollFrameOrBar.ScrollBar or scrollFrameOrBar
    if not scrollBar.GetThumbTexture then return end 
    
    scrollBar:SetWidth(18)
    
    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
    
    local thumb = scrollBar:GetThumbTexture()
    if thumb then
        thumb:SetSize(14, 24)
        thumb:SetColorTexture(0.5, 0.5, 0.5, 1)
        
        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
    
    local scrollBarName = scrollBar:GetName()
    if scrollBarName then
        local upBtn = _G[scrollBarName .. "ScrollUpButton"]
        local downBtn = _G[scrollBarName .. "ScrollDownButton"]
        if upBtn then upBtn:SetAlpha(0); upBtn:SetSize(1, 1) end
        if downBtn then downBtn:SetAlpha(0); downBtn:SetSize(1, 1) end
    end
    
    if scrollBar.ScrollUpButton then scrollBar.ScrollUpButton:SetAlpha(0); scrollBar.ScrollUpButton:SetSize(1, 1) end
    if scrollBar.ScrollDownButton then scrollBar.ScrollDownButton:SetAlpha(0); scrollBar.ScrollDownButton:SetSize(1, 1) end
end

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