-- MythicMate shared datagrid component: the LFG-style themed, sortable grid look, reusable by
-- every tab so all datagrids match the LFG finder. Provides an accent header strip + underline,
-- clickable sort headers with sort arrows, and per-row striping + a left accent bar + accent hover.
--
-- Split file so its locals get a fresh Lua chunk (MythicMate.lua is at the 200-local limit).
-- The main list keeps its own FontString headers (the HeaderFilters module drives them via
-- :SetText), so this component augments existing widgets instead of replacing them:
--   Grid.AttachHeaderSkin(headerRow)         -> accent bar strip + underline behind a header row
--   Grid.RefreshHeaderSkin(headerRow)        -> re-tint that skin when the accent changes
--   Grid.MakeHeaderSortable(fs, key, onClick)-> enable click + attach a sort arrow to a header FS
--   Grid.UpdateSortArrows(headers, key, asc) -> show the arrow on the active sort column
--   Grid.SkinRow(row, index)                 -> stripe + left accent bar + accent hover on a row
-- Exposed as MythicMateNS.Grid and _G.MythicMate_Grid.

local MythicMateNS = _G.MythicMate
if not MythicMateNS then return end

local Grid = {}

-- Active accent color (class color or preset), with the same violet fallback the rest of the UI
-- uses. Read live so switching the accent re-tints everything on the next refresh.
local function accentRGB()
    if type(_G.MythicMate_GetLFGAccent) == "function" then
        local r, g, b = _G.MythicMate_GetLFGAccent()
        if r then return r, g, b end
    end
    return 0.66, 0.40, 1.0
end
Grid.AccentRGB = accentRGB

------------------------------------------------------------
-- Header skin: a faint accent strip behind the column headers + an accent underline, matching the
-- LFG grid's headerBar/hdrLine. Stored on the frame so RefreshHeaderSkin can re-tint on accent
-- change. insets let callers nudge the strip to line up with their header layout.
------------------------------------------------------------
function Grid.AttachHeaderSkin(headerRow, insets)
    if not headerRow then return nil end
    if headerRow._wlGridSkin then return headerRow._wlGridSkin end
    insets = insets or {}
    local skin = {}

    local bar = headerRow:CreateTexture(nil, "BACKGROUND")
    bar:SetPoint("TOPLEFT", headerRow, "TOPLEFT", insets.left or -4, insets.top or 2)
    bar:SetPoint("BOTTOMRIGHT", headerRow, "BOTTOMRIGHT", insets.right or 4, insets.bottom or 0)
    bar:SetColorTexture(1, 1, 1, 0.05)
    skin.bar = bar

    local line = headerRow:CreateTexture(nil, "ARTWORK")
    line:SetPoint("BOTTOMLEFT", headerRow, "BOTTOMLEFT", 0, -2)
    line:SetPoint("BOTTOMRIGHT", headerRow, "BOTTOMRIGHT", 0, -2)
    line:SetHeight(1)
    line:SetColorTexture(0.4, 0.4, 0.55, 0.6)
    skin.line = line

    headerRow._wlGridSkin = skin
    Grid.RefreshHeaderSkin(headerRow)
    return skin
end

function Grid.RefreshHeaderSkin(headerRow)
    local skin = headerRow and headerRow._wlGridSkin
    if not skin then return end
    local r, g, b = accentRGB()
    if skin.bar then skin.bar:SetColorTexture(r, g, b, 0.16) end
    if skin.line then skin.line:SetColorTexture(r, g, b, 0.65) end
end

------------------------------------------------------------
-- Sortable headers: keep the caller's FontString, enable mouse, attach a sort arrow, and route
-- clicks to onClick(key, mouseButton) so the caller owns the sort state. Left-click is the natural
-- sort gesture; callers may still use the mouseButton arg to keep a secondary action on right-click.
------------------------------------------------------------
function Grid.MakeHeaderSortable(fs, key, onClick)
    if not fs then return nil end
    fs:EnableMouse(true)
    fs._sortKey = key
    if not fs.arrow then
        local parent = (fs.GetParent and fs:GetParent()) or fs
        fs.arrow = parent:CreateTexture(nil, "OVERLAY")
        fs.arrow:SetSize(10, 10)
    end
    fs.arrow:Hide()
    fs:SetScript("OnMouseUp", function(self, button)
        if onClick then onClick(key, button) end
    end)
    -- LFG-exact header hover: brighten on enter, restore on leave.
    fs:SetScript("OnEnter", function(self) if self.SetTextColor then self:SetTextColor(1, 1, 1) end end)
    fs:SetScript("OnLeave", function(self) if self.SetTextColor then self:SetTextColor(0.72, 0.72, 0.85) end end)
    return fs
end

-- Show the up/down arrow on the active sort column, hide it on the rest. headers = { key = fs }.
function Grid.UpdateSortArrows(headers, activeKey, asc)
    if type(headers) ~= "table" then return end
    for _, fs in pairs(headers) do
        if fs and fs.arrow then
            if fs._sortKey ~= nil and fs._sortKey == activeKey then
                fs.arrow:SetTexture(asc and "Interface\\Buttons\\Arrow-Up-Up"
                                        or "Interface\\Buttons\\Arrow-Down-Up")
                -- Anchor the arrow just past the header text (clamped to the column width).
                local textW = (fs.GetStringWidth and fs:GetStringWidth()) or 0
                local colW = (fs.GetWidth and fs:GetWidth()) or 60
                fs.arrow:ClearAllPoints()
                fs.arrow:SetPoint("LEFT", fs, "LEFT", math.min(textW, colW) + 2, 0)
                fs.arrow:Show()
            else
                fs.arrow:Hide()
            end
        end
    end
end

------------------------------------------------------------
-- Row skin: alternating stripe + a left accent bar + accent hover, applied to an existing pooled
-- row. Expects row.bg (BACKGROUND) and optionally row.highlight (HIGHLIGHT). Creates the accent
-- bar once and re-tints it from the active accent each call, so it's safe to call every render.
------------------------------------------------------------
function Grid.SkinRow(row, index)
    if not row then return end
    local even = (tonumber(index) or 0) % 2 == 0
    if row.bg then
        if row.bg.SetTexture then row.bg:SetTexture("Interface\\Buttons\\WHITE8x8") end
        -- LFG-exact alternating stripe.
        row.bg:SetVertexColor(1, 1, 1, even and 0.05 or 0.015)
    end
    if not row._wlAccentBar then
        -- LFG-exact left accent bar: x=6, width 3, 1px inset top/bottom.
        local bar = row:CreateTexture(nil, "ARTWORK")
        bar:SetPoint("TOPLEFT", row, "TOPLEFT", 6, -1)
        bar:SetPoint("BOTTOMLEFT", row, "BOTTOMLEFT", 6, 1)
        bar:SetWidth(3)
        row._wlAccentBar = bar
    end
    local r, g, b = accentRGB()
    row._wlAccentBar:SetColorTexture(r, g, b, 0.9)
    if row.highlight then
        -- LFG-exact hover: additive listbox highlight.
        if row.highlight.SetTexture then row.highlight:SetTexture("Interface\\Buttons\\UI-Listbox-Highlight") end
        if row.highlight.SetBlendMode then row.highlight:SetBlendMode("ADD") end
        row.highlight:SetVertexColor(1, 1, 1, 1)
    end
end

MythicMateNS.Grid = Grid
_G.MythicMate_Grid = Grid
