-- The purpose of this file is to import auras from WeakAuras.lua (i.e. auras "uploaded" to the addon)
-- Importing them entails deserializing/decoding them, and storing them in SavedVariables
-- Auras are only imported if they haven't been imported before, as determined by their version number

---@diagnostic disable: undefined-field, undefined-global
---@type string
local AddonName = ...
---@class Private
local Private = select(2, ...)
local wt = Private.Ace
---@class WTAPI
local WTAPI = WTAPI

local LibSerialize = LibStub("LibSerialize")
local LibDeflate = LibStub("LibDeflate")


-- Checks if we already have this aura installed (by display name)
-- If so, makes sure that the version of the aura we are importing matches its UID
-- This is called both on importing of auras (from Private.WeakAuras), as well as on updating an aura
-- The goal is to properly update/recognise installed versions of the auras, even if their UID is different
-- This function exists in the addon namespace because it's repeated just before auras get updated (for safety)
function Private:MatchInstalledUID(auraData)
    local displayName = auraData and auraData.id
    local installedAuraData = displayName and WeakAuras.GetData(displayName)

    if not installedAuraData then return end

    auraData.uid = installedAuraData.uid
end

-- Takes an auraInfo table as input, and deserializes the WeakAuras string, then stores the result in WildThingsSavedVars.WeakAuras
-- auraInfo tables are how auras are stored in WeakAuras.lua. They are structured as such:
-- auraInfo = {
--     displayName = <string>,
--     version = <number>,
--     data = <string>
-- }
-- This function only imports auras that have a version number higher than what we've imported before
-- Similarly, it does not import them if they are deemed irrelevant by IsRelevantAura
local function ImportAura(auraInfo)
    local displayName = auraInfo.displayName

    local version = auraInfo.version
    local importedVersion = WildThingsSavedVars.WeakAuras[displayName] and WildThingsSavedVars.WeakAuras[displayName].d and WildThingsSavedVars.WeakAuras[displayName].d.customWTVersion

    if importedVersion and importedVersion >= version then return end -- Do not import auras that we've imported before

    -- Deserialize the WeakAuras string
    local toDecode = auraInfo.data:match("!WA:2!(.+)")

    local decoded = LibDeflate:DecodeForPrint(toDecode)
    local decompressed = LibDeflate:DecompressDeflate(decoded)
    local _, data = LibSerialize:Deserialize(decompressed)

    -- Add a customWTVersion field for version checking
    -- This is what AuraUpdater checks against to detect if a newer version is available compared to what is installed
    data.d.customWTVersion = version
    data.d.WToptional = auraInfo.optional

    -- These fields are set to nil to prevent WeakAuras companion/Wago app from (mistakenly) suggesting there's updates available
    data.d.url = nil
    data.d.wagoID = nil

    if data.c then
        for _, childData in pairs(data.c) do
            childData.url = nil
            childData.wagoID = nil
        end
    end

    WildThingsSavedVars.WeakAuras[displayName] = data
end

-- Takes WeakAura strings from Private.WeakAuras, decodes them, and saves them to WildThingsSavedVars.WeakAuras
-- Only decodes new auras (or new versions of auras)
function Private:InitializeWeakAurasImporter()
    if not WildThingsSavedVars.WeakAuras then WildThingsSavedVars.WeakAuras = {} end

    for _, auraInfo in ipairs(Private.WeakAuras) do
        ImportAura(auraInfo, false)
    end

    -- Match imported aura UIDs to installed aura UIDs
    for _, auraData in pairs(WildThingsSavedVars.WeakAuras) do
        Private:MatchInstalledUID(auraData.d)
    end
end
