local ADDON_NAME, ns = ...

-- RatedTracker artwork: scenic loading screens (Media/loadscreens) and
-- parchment battle maps (Media/maps), converted from the website assets.
local ART = "Interface\\AddOns\\RatedTrackerFieldManual\\Media\\"

local function screen(slug)
    return ART .. "loadscreens\\" .. slug .. ".tga"
end
local function mapArt(slug)
    return ART .. "maps\\" .. slug .. ".tga"
end

-- Resolve a website marker icon value to a bundled TGA or a game icon.
-- "assets/.../foo.png" -> Media\Markers\foo.tga; bare name -> Interface\Icons.
local MARKER_ART = ART .. "Markers\\"
function ns.ResolveMarkerIcon(icon)
    if type(icon) ~= "string" or icon == "" then
        return "Interface\\Icons\\INV_Misc_QuestionMark", false
    end
    local file = icon:match("([^/\\]+)%.png$")
    if file then
        return MARKER_ART .. file .. ".tga", true
    end
    if icon:find("[/\\]") then
        return "Interface\\Icons\\INV_Misc_QuestionMark", false
    end
    return "Interface\\Icons\\" .. icon, false
end

ns.MapInfo = {
    ["arathi-basin"] = { mapType = "Resource Map", loadingScreen = screen("arathi-basin"), mapImage = mapArt("arathi-basin") },
    ["battle-for-gilneas"] = { mapType = "Domination", loadingScreen = screen("battle-for-gilneas"), mapImage = mapArt("battle-for-gilneas") },
    ["deepwind-gorge"] = { mapType = "Resource Map", loadingScreen = screen("deepwind-gorge"), mapImage = mapArt("deepwind-gorge") },
    ["deephaul-ravine"] = { mapType = "Cart Map", loadingScreen = screen("deephaul-ravine"), mapImage = mapArt("deephaul-ravine") },
    ["eye-of-the-storm"] = { mapType = "Hybrid", loadingScreen = screen("eye-of-the-storm"), mapImage = mapArt("eye-of-the-storm") },
    ["silvershard-mines"] = { mapType = "Cart Map", loadingScreen = screen("silvershard-mines"), mapImage = mapArt("silvershard-mines") },
    ["temple-of-kotmogu"] = { mapType = "Orb Map", loadingScreen = screen("temple-of-kotmogu"), mapImage = mapArt("temple-of-kotmogu") },
    ["twin-peaks"] = { mapType = "Flag Map", loadingScreen = screen("twin-peaks"), mapImage = mapArt("twin-peaks") },
    ["warsong-gulch"] = { mapType = "Flag Map", loadingScreen = screen("warsong-gulch"), mapImage = mapArt("warsong-gulch") },
}

ns.FEATURED_SCREENS = {
    screen("arathi-basin"), screen("warsong-gulch"), screen("eye-of-the-storm"),
    screen("silvershard-mines"), screen("temple-of-kotmogu"), screen("deepwind-gorge"),
    screen("twin-peaks"), screen("battle-for-gilneas"), screen("deephaul-ravine"),
}

-- Home category card art, converted from the RatedTracker website assets.
ns.CardArt = {
    blitz = ART .. "cards\\blitz.tga",
    terms = ART .. "cards\\terms.tga",
    macros = ART .. "cards\\macros.tga",
    resources = ART .. "cards\\resources.tga",
}

function ns.GetCardArt(key)
    return ns.CardArt[key]
end

function ns.GetMapInfo(slug)
    return ns.MapInfo[slug]
end

function ns.GetLoadingScreen(slug)
    local info = ns.MapInfo[slug]
    return info and info.loadingScreen
end

function ns.GetMapImage(slug)
    local info = ns.MapInfo[slug]
    return info and info.mapImage
end

function ns.GetFeaturedScreen()
    local list = ns.FEATURED_SCREENS
    if not list or #list == 0 then return nil end
    local idx = ((math.floor(GetTime and GetTime() or 0) / 300) % #list) + 1
    idx = math.floor(idx)
    if idx < 1 then idx = 1 end
    if idx > #list then idx = #list end
    return list[idx]
end

function ns.DifficultyStars(n)
    n = tonumber(n) or 0
    n = math.max(0, math.min(5, math.floor(n + 0.5)))
    if n <= 0 then return "" end
    local lit = string.rep("|cFFFFD100*|r ", n)
    local dim = string.rep("|cFF555555*|r ", 5 - n)
    return (lit .. dim):gsub("%s+$", "")
end
