local addonTable = select(2, ...)

local MythicSeason = addonTable.MythicSeason or {}
addonTable.MythicSeason = MythicSeason

local CURRENT_SEASON_DUNGEONS = {
    {
        mapID = 560,
        lfgGroupID = 400,
        lfgActivityID = 1764,
        teleportSpellID = 1254559,
        fallbackName = "Maisara Caverns",
    },
    {
        mapID = 559,
        lfgGroupID = 401,
        lfgActivityID = 1768,
        teleportSpellID = 1254563,
        fallbackName = "Nexus-Point Xenas",
    },
    {
        mapID = 558,
        lfgGroupID = 399,
        lfgActivityID = 1760,
        teleportSpellID = 1254572,
        fallbackName = "Magisters' Terrace",
    },
    {
        mapID = 557,
        lfgGroupID = 370,
        lfgActivityID = 1542,
        teleportSpellID = 1254400,
        fallbackName = "Windrunner Spire",
    },
    {
        mapID = 402,
        lfgGroupID = 302,
        lfgActivityID = 1160,
        teleportSpellID = 393273,
        fallbackName = "Algeth'ar Academy",
    },
    {
        mapID = 556,
        lfgGroupID = 133,
        lfgActivityID = 486,
        teleportSpellID = 1254555,
        fallbackName = "Pit of Saron",
    },
    {
        mapID = 161,
        lfgGroupID = 9,
        lfgActivityID = 182,
        teleportSpellID = 159898,
        fallbackName = "Skyreach",
        fallbackTexture = {
            texture = 1042064,
            texCoords = { 0.0, 0.96, 0.0, 0.96 },
            source = "fallback",
            zoom = 1.0,
        },
    },
    {
        mapID = 239,
        lfgGroupID = 52,
        lfgActivityID = 1770,
        teleportSpellID = 1254551,
        fallbackName = "Seat of the Triumvirate",
    },
}

local DUNGEON_BY_MAP_ID = {}
local DUNGEON_BY_LFG_GROUP_ID = {}
local DUNGEON_BY_LFG_ACTIVITY_ID = {}
local MAP_ID_BY_TELEPORT_SPELL_ID = {}

local function CopyArray(source)
    local target = {}
    if type(source) ~= "table" then
        return target
    end

    for index, value in ipairs(source) do
        target[index] = value
    end
    return target
end

local function CopyTexture(textureData)
    if type(textureData) ~= "table" then
        return nil
    end

    return {
        texture = textureData.texture,
        texCoords = CopyArray(textureData.texCoords),
        source = textureData.source,
        zoom = textureData.zoom,
    }
end

local function CopyDungeon(dungeon)
    if type(dungeon) ~= "table" then
        return nil
    end

    return {
        mapID = dungeon.mapID,
        lfgGroupID = dungeon.lfgGroupID,
        lfgActivityID = dungeon.lfgActivityID,
        teleportSpellID = dungeon.teleportSpellID,
        fallbackName = dungeon.fallbackName,
        fallbackTexture = CopyTexture(dungeon.fallbackTexture),
    }
end

for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
    if dungeon.mapID then
        DUNGEON_BY_MAP_ID[dungeon.mapID] = dungeon
    end
    if dungeon.lfgGroupID then
        DUNGEON_BY_LFG_GROUP_ID[dungeon.lfgGroupID] = dungeon
    end
    if dungeon.lfgActivityID then
        DUNGEON_BY_LFG_ACTIVITY_ID[dungeon.lfgActivityID] = dungeon
    end
    if dungeon.teleportSpellID and dungeon.mapID then
        MAP_ID_BY_TELEPORT_SPELL_ID[dungeon.teleportSpellID] = dungeon.mapID
    end
end

MythicSeason.CURRENT_SEASON_DUNGEONS = CURRENT_SEASON_DUNGEONS

function MythicSeason.GetCurrentSeasonDungeons()
    local dungeons = {}
    for index, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        dungeons[index] = CopyDungeon(dungeon)
    end
    return dungeons
end

function MythicSeason.GetCurrentSeasonMapIDs()
    local mapIDs = {}
    for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        if dungeon.mapID then
            mapIDs[#mapIDs + 1] = dungeon.mapID
        end
    end
    return mapIDs
end

function MythicSeason.GetCurrentSeasonLFGGroupIDs()
    local groupIDs = {}
    for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        if dungeon.lfgGroupID then
            groupIDs[#groupIDs + 1] = dungeon.lfgGroupID
        end
    end
    return groupIDs
end

function MythicSeason.GetCurrentSeasonLFGActivityIDs()
    local activityIDs = {}
    for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        if dungeon.lfgActivityID then
            activityIDs[#activityIDs + 1] = dungeon.lfgActivityID
        end
    end
    return activityIDs
end

function MythicSeason.GetDungeonByMapID(mapID)
    return CopyDungeon(DUNGEON_BY_MAP_ID[tonumber(mapID or 0)])
end

function MythicSeason.GetDungeonByLFGGroupID(groupID)
    return CopyDungeon(DUNGEON_BY_LFG_GROUP_ID[tonumber(groupID or 0)])
end

function MythicSeason.GetDungeonByLFGActivityID(activityID)
    return CopyDungeon(DUNGEON_BY_LFG_ACTIVITY_ID[tonumber(activityID or 0)])
end

function MythicSeason.GetTeleportSpellID(mapID)
    local dungeon = DUNGEON_BY_MAP_ID[tonumber(mapID or 0)]
    return dungeon and dungeon.teleportSpellID or nil
end

function MythicSeason.GetTeleportSpellMap()
    local spellMap = {}
    for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        if dungeon.mapID and dungeon.teleportSpellID then
            spellMap[dungeon.mapID] = { spellID = dungeon.teleportSpellID }
        end
    end
    return spellMap
end

function MythicSeason.GetMapIDByTeleportSpellID(spellID)
    return MAP_ID_BY_TELEPORT_SPELL_ID[tonumber(spellID or 0)]
end

function MythicSeason.GetFallbackDungeonName(mapID)
    local dungeon = DUNGEON_BY_MAP_ID[tonumber(mapID or 0)]
    return dungeon and dungeon.fallbackName or nil
end

function MythicSeason.GetFallbackDungeonNames()
    local names = {}
    for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        if dungeon.mapID and dungeon.fallbackName then
            names[dungeon.mapID] = dungeon.fallbackName
        end
    end
    return names
end

function MythicSeason.GetFallbackDungeonTexture(mapID)
    local dungeon = DUNGEON_BY_MAP_ID[tonumber(mapID or 0)]
    return dungeon and CopyTexture(dungeon.fallbackTexture) or nil
end

function MythicSeason.GetFallbackDungeonTextures()
    local textures = {}
    for _, dungeon in ipairs(CURRENT_SEASON_DUNGEONS) do
        if dungeon.mapID and dungeon.fallbackTexture then
            textures[dungeon.mapID] = CopyTexture(dungeon.fallbackTexture)
        end
    end
    return textures
end

function MythicSeason.GetTestDungeonMapIDs()
    return MythicSeason.GetCurrentSeasonMapIDs()
end
