local _, ns = ...
local MythicTools = ns.MythicTools

MythicTools.DEFAULT_SEASON = "midnight-season1"

MythicTools.SEASON_OPTIONS = {
    {value = "midnight-season1", label = "Midnight Season 1", expansion = "Midnight", season = 1},
}

MythicTools.LEGACY_SEASON_ALIASES = {
    season1 = "midnight-season1",
}

MythicTools.SEASON_DUNGEON_FALLBACKS = {
    ["midnight-season1"] = {
        "Magisters' Terrace",
        "Maisara Caverns",
        "Nexus-Point Xenas",
        "Windrunner Spire",
        "Algeth'ar Academy",
        "Pit of Saron",
        "Seat of the Triumvirate",
        "Skyreach",
    },
}

MythicTools.DUNGEON_BACKGROUND_FILES_BY_SLUG = {
    magisters_terrace = "magisters_terrace.tga",
    maisara_caverns = "maisara_caverns.tga",
    nexus_point_xenas = "nexus_point_xenas.tga",
    windrunner_spire = "windrunner_spire.tga",
    algethar_academy = "algethar_academy.tga",
    pit_of_saron = "pit_of_saron.tga",
    seat_of_the_triumvirate = "seat_of_the_triumvirate.tga",
    skyreach = "skyreach.tga",
}

MythicTools.DUNGEON_SLUG_BY_ALIAS = {
    ["magisters terrace"] = "magisters_terrace",
    ["terraco dos magisteres"] = "magisters_terrace",
    ["maisara caverns"] = "maisara_caverns",
    ["cavernas de maisara"] = "maisara_caverns",
    ["nexus point xenas"] = "nexus_point_xenas",
    ["nexo ponto xenas"] = "nexus_point_xenas",
    ["windrunner spire"] = "windrunner_spire",
    ["pico dos correventos"] = "windrunner_spire",
    ["algethar academy"] = "algethar_academy",
    ["academia algethar"] = "algethar_academy",
    ["pit of saron"] = "pit_of_saron",
    ["fosso de saron"] = "pit_of_saron",
    ["seat of the triumvirate"] = "seat_of_the_triumvirate",
    ["sede do triunvirato"] = "seat_of_the_triumvirate",
    ["skyreach"] = "skyreach",
    ["pico celeste"] = "skyreach",
    ["pinaculo celeste"] = "skyreach",
}

MythicTools.DUNGEON_BACKGROUND_SLUG_BY_CHALLENGE_ID = {
    [161] = "skyreach",
    [239] = "seat_of_the_triumvirate",
}

function MythicTools:GetDefaultSeason()
    local firstOption = self.SEASON_OPTIONS and self.SEASON_OPTIONS[1]
    return self.DEFAULT_SEASON or (firstOption and firstOption.value) or "midnight-season1"
end

function MythicTools:GetSeasonOptions()
    return self.SEASON_OPTIONS or {{value = self:GetDefaultSeason(), label = "Midnight Season 1"}}
end

function MythicTools:GetSeasonInfo(season)
    season = self:NormalizeSeason(season)
    for _, option in ipairs(self:GetSeasonOptions()) do
        if option.value == season then
            return option
        end
    end

    return self:GetSeasonOptions()[1]
end

function MythicTools:GetSeasonDisplayLabel(season)
    local info = self:GetSeasonInfo(season)
    return (info and info.label) or tostring(season or self:GetDefaultSeason())
end

function MythicTools:NormalizeSeason(season)
    if type(season) == "string" and season ~= "" then
        season = (self.LEGACY_SEASON_ALIASES and self.LEGACY_SEASON_ALIASES[season]) or season
        for _, option in ipairs(self:GetSeasonOptions()) do
            if option.value == season then
                return season
            end
        end
    end

    return self:GetDefaultSeason()
end

function MythicTools:GetSeasonDungeonFallbacks(season)
    season = self:NormalizeSeason(season)
    return (self.SEASON_DUNGEON_FALLBACKS and self.SEASON_DUNGEON_FALLBACKS[season]) or {}
end

function MythicTools:GetDungeonSlugByAlias(alias)
    if type(alias) ~= "string" or alias == "" then
        return nil
    end

    return self.DUNGEON_SLUG_BY_ALIAS and self.DUNGEON_SLUG_BY_ALIAS[alias] or nil
end

function MythicTools:GetDungeonBackgroundFileBySlug(slug)
    if type(slug) ~= "string" or slug == "" then
        return nil
    end

    return self.DUNGEON_BACKGROUND_FILES_BY_SLUG and self.DUNGEON_BACKGROUND_FILES_BY_SLUG[slug] or nil
end

function MythicTools:GetDungeonBackgroundSlugByChallengeID(mapChallengeModeID)
    mapChallengeModeID = tonumber(mapChallengeModeID)
    if not mapChallengeModeID then
        return nil
    end

    return self.DUNGEON_BACKGROUND_SLUG_BY_CHALLENGE_ID and self.DUNGEON_BACKGROUND_SLUG_BY_CHALLENGE_ID[mapChallengeModeID] or nil
end

function MythicTools:CacheDungeonBackgroundSlugForChallengeID(mapChallengeModeID, slug)
    mapChallengeModeID = tonumber(mapChallengeModeID)
    if not mapChallengeModeID or type(slug) ~= "string" or slug == "" then
        return
    end

    self.DUNGEON_BACKGROUND_SLUG_BY_CHALLENGE_ID = self.DUNGEON_BACKGROUND_SLUG_BY_CHALLENGE_ID or {}
    self.DUNGEON_BACKGROUND_SLUG_BY_CHALLENGE_ID[mapChallengeModeID] = slug
end
