local addonName, NS = ...
local GR = _G["GRS_MainFrame"]

-- =========================================================================
-- UI HELPER FUNCTIONS (Shared across RaidRoster modules)
-- =========================================================================

-- Import Constants
local CONST = GR.RR.CONST

-- =========================================================================
-- VISUAL HELPERS
-- =========================================================================

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

function GR.RR.CreateBorder(f, r, g, b, a)
    if f.border then f.border:Hide() end
    local border = f:CreateTexture(nil, "BORDER")
    border:SetPoint("TOPLEFT", -1, 1)
    border:SetPoint("BOTTOMRIGHT", 1, -1)
    border:SetColorTexture(r, g, b, a)
    f.border = border
    return border
end

function GR.RR.SkinButton(btn)
    if not btn then return end
    btn:SetNormalTexture("")
    btn:SetHighlightTexture("")
    btn:SetPushedTexture("")
    btn:SetDisabledTexture("")
    GR.RR.CreateBg(btn, 0.2, 0.2, 0.2, 1) 
    GR.RR.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)
    local fs = btn:GetFontString()
    if fs then fs:SetTextColor(1, 1, 1) end
end

function GR.RR.CreateCustomScrollbar(parent, targetScrollFrame)
    local slider = CreateFrame("Slider", nil, parent)
    slider:SetWidth(12)
    slider:SetOrientation("VERTICAL")
    local track = slider:CreateTexture(nil, "BACKGROUND")
    track:SetAllPoints()
    track:SetColorTexture(0, 0, 0, 0.5)
    local thumb = slider:CreateTexture(nil, "ARTWORK")
    thumb:SetSize(12, 24)
    thumb:SetColorTexture(0.4, 0.4, 0.4, 1) 
    slider:SetThumbTexture(thumb)
    slider:SetScript("OnValueChanged", function(s, v)
        targetScrollFrame:SetVerticalScroll(v)
    end)
    return slider
end

-- =========================================================================
-- DATE HELPERS
-- =========================================================================

function GR.RR.ParseDate(dateStr)
    if not dateStr then return nil end
    local d, m, y = string.match(dateStr, "(%d+)[%p](%d+)[%p](%d+)")
    if not d then return nil end
    return time({year=y, month=m, day=d, hour=12, min=0, sec=0})
end

function GR.RR.FormatDate(timestamp)
    return date("%d-%m-%Y", timestamp)
end

function GR.RR.Sanitize(str)
    if not str then return "" end
    str = string.gsub(str, "%^", "") 
    return str
end

-- =========================================================================
-- REGION/FACTION HELPERS
-- =========================================================================

function GR.RR.GetRegionName()
    local regionID = GetCurrentRegion()
    if regionID == 1 then return "us"
    elseif regionID == 2 then return "kr"
    elseif regionID == 3 then return "eu"
    elseif regionID == 4 then return "tw"
    elseif regionID == 5 then return "cn"
    else return "us" end 
end

function GR.RR.GetFactionID()
    local factionGroup = UnitFactionGroup("player")
    if factionGroup == "Alliance" then return 1 else return 2 end 
end

function GR.RR.GetFullPlayerName()
    local name, realm = UnitFullName("player")
    if not realm then realm = GetRealmName() end
    return name .. "-" .. realm
end

function GR.RR.GetCanonicalName(fullName)
    if not fullName then return "Unknown" end
    return Ambiguate(fullName, "none")
end

-- =========================================================================
-- COLOR HELPERS
-- =========================================================================

function GR.RR.ScoreColor(score)
    local n = tonumber(score) or 0
    if n >= 3000 then return "|cffff8000"
    elseif n >= 2500 then return "|cffa335ee"
    elseif n >= 2000 then return "|cff0070dd"
    elseif n >= 1500 then return "|cff1eff00"
    else return "|cffffffff" end
end

function GR.RR.RaidProgressColor(str)
    if not str or str == "-" then return "|cff808080" end
    if string.find(str, "(%d+)/(%d+)") then
        local k, t = string.match(str, "(%d+)/(%d+)")
        if k == t then return "|cff1eff00" end 
    end
    return "|cffffffff" 
end

-- Export to NS for backward compatibility
NS.RR_CreateBg = GR.RR.CreateBg
NS.RR_CreateBorder = GR.RR.CreateBorder
NS.RR_SkinButton = GR.RR.SkinButton
