local _AddonName, _EchoRaidTools = ...
local _Cooldowns = _EchoRaidTools:GetModule("cooldowns")

function _Cooldowns.Modernise(data, baseGroupData)
    local SV_version = data.version

    local MigrateLoadSettings = function(groupData)
        if groupData and groupData.loadSettings and (not groupData.loadSettings.combat) then
            local t = groupData.loadSettings

            t.combat = {}
            t.combat.inCombat = t.inCombat
            t.inCombat = nil
            t.combat.inEncounter = t.inEncounter
            t.inEncounter = nil
            t.combat.encounterID = t.encounterID
            t.encounterID = nil
            t.combat.InInstance = t.InInstance
            t.InInstance = nil

            t.group = {}
            t.group.solo = t.solo
            t.solo = nil
            t.group.inParty = t.inParty
            t.inParty = nil
            t.group.inRaid = t.inRaid
            t.inRaid = nil

            t.instance = {}
            t.instance.raidInstance = t.raidInstance
            t.raidInstance = nil
            t.instance.dungeonInstance = t.dungeonInstance
            t.dungeonInstance = nil
            t.instance.arenaInstance = t.arenaInstance
            t.arenaInstance = nil
            t.instance.battlegroundInstance = t.battlegroundInstance
            t.battlegroundInstance = nil

            t.difficulty = {}
            t.difficulty.raidLFR = t.raidLFR
            t.raidLFR = nil
            t.difficulty.raidNormal = t.raidNormal
            t.raidNormal = nil
            t.difficulty.raidHeroic = t.raidHeroic
            t.raidHeroic = nil
            t.difficulty.raidMythic = t.raidMythic
            t.raidMythic = nil

            t.role = {}
            t.role.roleDPS = t.roleDPS
            t.roleDPS = nil
            t.role.roleHealer = t.roleHealer
            t.roleHealer = nil
            t.role.roleTank = t.roleTank
            t.roleTank = nil
        end
    end
    local MigrateSettingsIntoSections = function(groupData)
        if groupData and not groupData.displaySettings then
            groupData.displaySettings = {}
            for key, _ in pairs(baseGroupData.displaySettings) do
                if groupData[key] ~= nil then
                    groupData.displaySettings[key] = groupData[key]
                    groupData[key] = nil
                end
            end
            groupData.layoutSettings = {}
            for key, _ in pairs(baseGroupData.layoutSettings) do
                if groupData[key] ~= nil then
                    groupData.layoutSettings[key] = groupData[key]
                    groupData[key] = nil
                end
            end
            groupData.filters.customFilters = {}
            for key, value in pairs(groupData.customFilters) do
                groupData.filters.customFilters[key] = value
                groupData.customFilters[key] = nil
            end
            groupData.customFilters = nil
            groupData.stateSettings.subGroups = {}
            for i = 1, 8 do
                if groupData["group"..i] ~= nil then
                    groupData.stateSettings.subGroups["group"..i] = groupData["group"..i]
                    groupData["group"..i] = nil
                end
            end
            local removedKeys = {"desaturateDead", "desaturateUnavailable", "hideExtraGroups", "hideDead", "hideUnavailable", "useHeaders"}
            for _, key in pairs(removedKeys) do
                if groupData[key] ~= nil then
                    groupData[key] = nil
                end
            end
        end
    end
    local FixBrokenFilterNames = function()
        for i, filterDetails in ipairs(data.customFilters) do
            if type(filterDetails.name) ~= "string" then
                local baseName = "New Filter"
                local name, i = baseName, 0
                local filterExists = function(name)
                    for _, filter in ipairs(data.customFilters) do
                        if filter.name == name then
                            return true
                        end
                    end
                end
                while filterExists(name) do
                    i = i + 1
                    name = format("%s %d", baseName, i)
                end
                filterDetails.name = name
            end
        end
    end
    if not SV_version or SV_version < 2 then
        for i, group in ipairs(data.groups) do
            MigrateLoadSettings(group.groupData)
            MigrateSettingsIntoSections(group.groupData)
        end
        FixBrokenFilterNames()
    end

    do
        local baseData = CopyTable(baseGroupData)
        local function Merge(base, group, IsGroupOverwriter)
            local Merged = CopyTable(base)

            for groupKey, groupValue in pairs(group) do
                local baseValue = base[groupKey]

                if type(baseValue) == "table" and type(groupValue) == "table" then
                    Merged[groupKey] = Merge(baseValue, groupValue, IsGroupOverwriter)
                    if Merged[groupKey].r and Merged[groupKey].g and Merged[groupKey].b and Merged[groupKey].a then
                        Merged[groupKey] = CreateColor(Merged[groupKey].r, Merged[groupKey].g, Merged[groupKey].b, Merged[groupKey].a)
                    end
                elseif Merged[groupKey] or IsGroupOverwriter then
                    Merged[groupKey] = groupValue
                end
            end

            return Merged
        end
        for i, group in ipairs(data.groups) do
            local groupData = group.groupData
            data.groups[i].groupData = Merge(baseData, groupData, true)
        end
    end

    data.version = 2
end