-----------------------------
-- Saved Variables Updater --
-----------------------------

--- Example Use
-----------------------------

--local updates = {
--    {
--        version = "v-0.9.25-alpha",
--        details = "Added Updates",
--        db = {
--            apply = function()
--            end,
--            revert = function()
--            end,
--        },
--        profile = {
--            apply = function()
--            end,
--            revert = function()
--            end,
--        },
--    },
--};

local AddOnName, nsVars = ...
local ss = nsVars.SS

local updates = {
  {-- 1
    version = "v-0.9.25-alpha",
    details = "Pet and additional resource tracking are now account wide",
    global = {
      apply = function()
        if ss.db.track then
          ss.global.track.additionalResource = ss.global.track.additionalResourc or ss.db.track.additionalResource
          ss.global.track.pet                = ss.global.track.pet or ss.db.track.pet
        end
      end,
      revert = function()
        ss.global.track = nil
      end,
    },
  },
  {-- 2
    version = "v-0.9.25-alpha",
    details = "Pet and additional resource tracking are now account wide",
    db = {
      apply = function()
        ss.db.track = nil
      end,
      revert = function()
        if ss.global.track then
          ss.db.track                    = ss.db.track or {}
          ss.db.track.additionalResource = ss.global.track.additionalResource
          ss.db.track.pet                = ss.global.track.pet
        end
      end,
    },
  },
  {--3
    version = "v-0.9.39-alpha",
    details = "Moved Swirl Configuration Into Modes.",
    global = {
      apply = function()
        for dbn, db in pairs(ss.global) do
          local profileStart = { string.split("_", dbn) };
          
          if profileStart[1] == "SSProfile" then
            for k, v in pairs(db) do
              if type(v) == "table" and v.swirl then
                local swirl   = v.swirl
                swirl.groupId = "swirl"
                swirl.id      = v.swirl.id or "galaxy"
                swirl.speed   = v.swirl.speed or 5
                for kk, vv in pairs(v.modes) do
                  --Specs
                  for kkk, vvv in pairs(vv) do
                    --modes
                    vvv.swirl = swirl
                  end
                end
                v.swirl = nil
              end
            end
          end
        end
      end,
      revert = function()
        for dbn, db in pairs(ss.global) do
          local profileStart = { string.split("_", dbn) };
          
          if profileStart[1] == "SSProfile" then
            for k, v in pairs(db) do
              if type(v) == "table" and v.modes and not v.swirl then
                for kk, vv in pairs(v.modes) do
                  --Specs
                  for kkk, vvv in pairs(vv) do
                    --modes
                    v.swirl   = v.swirl or vvv.swirl
                    vvv.swirl = nil
                  end
                end
              end
            end
          end
        end
      end,
    },
  },
  {
    --4
    version = "v-0.9.41-alpha",
    details = "Added notification for texture packs.",
    global = {
      apply = function()
        ss:AddPendingTask("notification", function()
          ss.Notification:Notify("New Textures Available!", "Install \"Seamore Spheres Technology Textures\" to try them out. (No configuration required)")
        end)
      end,
      revert = function()
      end,
    },
  },
};

local update = function(db, latest)
  -- defaults to 0 to ensure new characters include all updates.
  local currentDB = ss[db].latestUpdate or 0
  
  if currentDB < latest then
    -- perform updates
    for v = currentDB + 1, latest do
      if updates[v] and updates[v][db] then
        updates[v][db].apply()
        ss[db].latestUpdate = v
      end
    end
  elseif currentDB > latest then
    -- perform reverts
    for v = currentDB, latest + 1, -1 do
      if updates[v] and updates[v][db] then
        updates[v][db].revert()
        ss[db].latestUpdate = v - 1
      end
    end
  end
  
  if not ss[db].latestUpdate then
    -- Ensure this value gets initialized.
    ss[db].latestUpdate = latest
  end
end

local mtUpdater = {
  update = function()
    -- Update to target versions.
    local version = 4
    update("db", version)
    update("global", version)
    update("profile", version)
    update("private", version)
  end,
}

ss["Updater"] = setmetatable({}, { __index = mtUpdater });
