---@diagnostic disable: undefined-field, undefined-global
---@class Private
local Private = select(2, ...)

local TOOLTIP_NAME_COLOR = "98f8fa" -- Color of the addon/aura name in grid cell tooltips
local guidToVersionsTable = {}

-- Displays the addon/weakaura versions for all group members
function Private:CreateAuraCheckGrid(parent)
    local checkGrid = Private:CreateCheckGrid(parent)

    -- Set the column titles
    -- These are sorted alphabetically
    -- We want WildThings (the addon version) to show first, which works out this way
    local titles = {"WildThings"}

    for _, wa in ipairs(Private.WeakAuras) do
        if wa.optional == false then
            local displayName = wa.displayName
            if WildThingsSavedVars.WeakAuras and WildThingsSavedVars.WeakAuras[displayName] then
                table.insert(titles, displayName)
            end
        end
    end
    -- for displayName in pairs(WildThingsSavedVars.WeakAuras) do
    --     table.insert(titles, displayName)
    -- end

    table.sort(titles)

    checkGrid:SetTitles(titles)

    local function GenerateTooltip(displayName, versions)
        if versions <= 0 then
            return string.format("|cff%s%s|r is up to date", TOOLTIP_NAME_COLOR, displayName)
        else
            return string.format(
                "|cff%s%s|r is |cff%s%d|r version(s) behind",
                TOOLTIP_NAME_COLOR,
                displayName,
                Private.gs.visual.colorStrings.red,
                versions
            )
        end
    end

    -- Takes in a versions table, and returns a formatted table that the check grid can interpret
    local function FormatVersionsTable(versionsTable)
        local data = {}

        local highestSeenAddOnVersion = Private:GetHighestSeenAddOnVersion()
        local highestSeenAuraVersions = Private:GetHighestSeenAuraVersions()

        -- AddOn version
        local addOnValue = Private:CompareAddOnVersions(highestSeenAddOnVersion, (versionsTable.addOn or "v0.0.0"))--highestSeenAddOnVersion - (versionsTable.addOn or 0)
        local addOnTooltip = GenerateTooltip("WildThings", addOnValue)

        data.WildThings = {
            value = addOnValue,
            tooltip = addOnTooltip
        }

        -- Aura versions
        for displayName, version in pairs(versionsTable.auras) do
            local value = (highestSeenAuraVersions[displayName] or 0) - version
            local tooltip = GenerateTooltip(displayName, value)

            data[displayName] = {
                value = value,
                tooltip = tooltip
            }
        end

        return data
    end

    -- This receives the versions table (exactly as broadcast) from a unit, and transforms it such that the grid can display it
    -- This function does not check if the versionsTable for this unit actually changed (TODO somewhere else)
    function checkGrid:UpdateVersionsTableForUnit(unit, versionsTable)
        -- Save the versionsTable so that we can reference it in FullRebuild
        local GUID = UnitGUID(unit)

        guidToVersionsTable[GUID] = versionsTable

        -- Update data
        local data = FormatVersionsTable(versionsTable)

        checkGrid:UpdateDataForUnit(unit, data)
        checkGrid:PositionRows()
    end

    -- Fully rebuilds every row in the grid, then re-positions them
    -- This typically happens whenever a new highest addon/aura version is seen
    function checkGrid:FullRebuild()
        for GUID, versionsTable in pairs(guidToVersionsTable) do
            if checkGrid:ContainsGUID(GUID) then
                local data = FormatVersionsTable(versionsTable)

                checkGrid:UpdateDataForGUID(GUID, data)
            else
                guidToVersionsTable[GUID] = nil
            end
        end

        checkGrid:PositionRows()
    end

    return checkGrid
end
