local NS = rawget(_G, "MCDVoiceCooldownNS") or {}
_G.MCDVoiceCooldownNS = NS

NS.UI = NS.UI or {}
NS.UI.SavedListRows = NS.UI.SavedListRows or {}

local Rows = NS.UI.SavedListRows
local Utils = NS.Utils or {}
local L = NS.L or function(key, ...)
    if select("#", ...) > 0 then
        return string.format(tostring(key), ...)
    end
    return tostring(key)
end

local function TrimText(value)
    if Utils.TrimText then
        return Utils.TrimText(value)
    end
    value = tostring(value or "")
    value = value:gsub("^%s+", ""):gsub("%s+$", "")
    return value
end

local function GetEntryTypeText(entry)
    local typeText = TrimText(entry and entry.typeText)
    if typeText ~= "" then
        return typeText
    end

    local entryType = tostring((entry and entry.entryType) or "cooldown")
    if entryType == "cast" then
        return L("ENTRY_TYPE_CAST")
    elseif entryType == "bloodlust" then
        return L("ENTRY_TYPE_BLOODLUST")
    end
    return L("ENTRY_TYPE_COOLDOWN")
end

function Rows.GetEntryTypeText(entry)
    return GetEntryTypeText(entry)
end

function Rows.BuildEntryRowText(entry, includeScopeText)
    if type(entry) == "table" then
        local cacheField = includeScopeText and "_mcdRowTextWithScope" or "_mcdRowText"
        if type(entry[cacheField]) == "string" then
            return entry[cacheField]
        end
    end
    local parts = {}
    local spellId = tonumber(entry and entry.spellId) or 0
    local index = tonumber(entry and entry.index) or 0
    local spellName = TrimText(entry and entry.spellName)

    parts[#parts + 1] = string.format("|cffffd24a%s|r", GetEntryTypeText(entry))
    if spellName ~= "" then
        parts[#parts + 1] = spellName
    elseif spellId > 0 then
        parts[#parts + 1] = tostring(spellId)
    else
        parts[#parts + 1] = L("ENTRY_UNNAMED")
    end

    if spellId > 0 then
        local objectType = tostring(entry and entry.objectType or "spell")
        local idLabel = (objectType == "item") and L("ENTRY_OBJECT_ITEM_ID") or L("ENTRY_OBJECT_SPELL_ID")
        parts[#parts + 1] = string.format("|cff808080%s:%d|r", idLabel, spellId)
    end
    if index > 0 then
        parts[#parts + 1] = string.format("|cff808080#%d|r", index)
    end
    local soundDetail = TrimText(entry and entry.soundDetail)
    if soundDetail ~= "" then
        parts[#parts + 1] = string.format("|cff808080%s|r", soundDetail)
    elseif TrimText(entry and entry.modeText) ~= "" then
        parts[#parts + 1] = string.format("|cff808080%s|r", TrimText(entry and entry.modeText))
    end
    if tostring(entry and entry.entryType or "") == "cast" and entry.delayEnabled == true and (tonumber(entry.delaySeconds) or 0) > 0 then
        parts[#parts + 1] = string.format("|cff808080%s|r", L("SAVED_CAST_DELAY", tonumber(entry.delaySeconds) or 0))
    end
    if entry and entry.checkTalent == true and (tonumber(entry.talentId) or 0) > 0 then
        local talentText = TrimText(entry.talentName)
        if talentText == "" then
            talentText = tostring(entry.talentId)
        end
        local talentCD = tonumber(entry.talentCD) or 0
        if talentCD > 0 then
            parts[#parts + 1] = string.format("|cff808080%s|r", L("TALENT_ROW", talentText, talentCD))
        else
            parts[#parts + 1] = string.format("|cff808080%s|r", L("TALENT_ROW_NO_CD", talentText))
        end
    end
    if includeScopeText and TrimText(entry and entry.scopeText) ~= "" then
        parts[#parts + 1] = string.format("|cff808080%s|r", TrimText(entry and entry.scopeText))
    end

    local result = table.concat(parts, "  ")
    if type(entry) == "table" then
        local cacheField = includeScopeText and "_mcdRowTextWithScope" or "_mcdRowText"
        entry[cacheField] = result
    end
    return result
end

function Rows.BuildGroupRowText(entry)
    if type(entry) == "table" and type(entry._mcdGroupRowText) == "string" then
        return entry._mcdGroupRowText
    end
    local name = TrimText(entry and entry.name)
    if name == "" then
        name = L("COLLECTION_UNNAMED")
    end
    local count = tonumber(entry and entry.count) or 0
    local countText
    if count <= 0 then
        countText = L("COLLECTION_EMPTY_COUNT")
    else
        countText = L("COLLECTION_COUNT", count)
    end
    local result = string.format("|cffffd24a%s|r  %s  |cff808080%s|r", L("COLLECTION_LABEL"), name, countText)
    if type(entry) == "table" then
        entry._mcdGroupRowText = result
    end
    return result
end
