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

local DEFAULT_LOCALE = "enUS"

local translations = {
    enUS = {
        ADDON_TITLE = "Sky Mythic History",
        LOCALE_AUTO = "Automatic (%s)",
        LOCALE_ENUS = "English",
        LOCALE_PTBR = "Portuguese (Brazil)",
        TAB_RUNS = "Runs",
        TAB_PLAYERS = "Players",
        TAB_CHARACTERS = "Characters",
        TAB_STATS = "Stats",
        TAB_SETTINGS = "Settings",
        NAVIGATION = "Navigation",
        SAVED_RUNS = "Saved runs",
        INDEXED_PLAYERS = "Indexed players",
        NO_ACTIVE_RUN = "No active run",
        ACTIVE_RUN = "Active run: %s +%d",

        SETTINGS_GENERAL = "General",
        SETTINGS_CHAT_ALERTS = "Chat alerts",
        SETTINGS_CHAT_ALERTS_DESC = "Warn when someone from your history joins your group.",
        SETTINGS_MINIMAP = "Minimap button",
        SETTINGS_MINIMAP_DESC = "Show or hide the circular shortcut around the minimap.",
        SETTINGS_LOOT_HISTORY = "Show loot history",
        SETTINGS_LOOT_HISTORY_DESC = "Display dropped items in the run details and completion popup.",
        SETTINGS_COMPLETION_POPUP = "Completion popup",
        SETTINGS_POPUP_ENABLED = "Open summary automatically",
        SETTINGS_POPUP_ENABLED_DESC = "Show the final window with loot, damage, healing, interrupts, DPS, and deaths.",
        SETTINGS_POPUP_MODE = "Display mode",
        SETTINGS_COMPACT_AUTOHIDE = "Compact auto close",
        SETTINGS_COMPACT_AUTOHIDE_DESC = "Close the compact toast automatically after the selected duration.",
        SETTINGS_COMPACT_DURATION = "Compact duration (3-60s)",
        SETTINGS_OPEN_TIMING = "Open timing",
        SETTINGS_DELAY = "Delay (0-10s)",
        SETTINGS_SCALE = "Scale (0.7-1.4)",
        SETTINGS_SAVE = "Save",
        SETTINGS_APPLY = "Apply",
        SETTINGS_PREVIEW = "Open preview",
        SETTINGS_PREVIEW_COMPACT = "Compact preview",
        SETTINGS_HISTORY = "History",
        SETTINGS_CLEAR_HISTORY = "Clear history",
        SETTINGS_CLEAR_HISTORY_CONFIRM = "Clear every saved Sky Mythic History run?",
        SETTINGS_DELETE_RUN_CONFIRM = "Delete this run from Sky Mythic History?\n%s",
        SETTINGS_HISTORY_NOTE = "History is shared across every character on this account.",
        SETTINGS_SUMMARY = "Summary",
        SETTINGS_POPUP_STATE = "Popup: %s (%s, delay %ss, scale %.1f)",
        SETTINGS_SUMMARY_TEXT = "Saved runs: %d\nIndexed players: %d\n%s\n%s\nData source: Blizzard damage meter",
        SETTINGS_ENABLED = "enabled",
        SETTINGS_DISABLED = "disabled",

        POPUP_TRIGGER_LOOT_OPENED = "After opening the chest",
        POPUP_TRIGGER_COMPLETED = "When the run ends",
        POPUP_MODE_FULL = "Full summary window",
        POPUP_MODE_COMPACT = "Compact clickable toast",
        POPUP_MODE_OFF = "Disabled",

        MINIMAP_LEFT_CLICK = "Left click: open history",
        MINIMAP_RIGHT_CLICK = "Right click: open settings",

        PLAYER_HISTORY_TIMED = "%d of %d timed",
        PLAYER_HISTORY_NOTE = "Note: %s",
        PLAYER_CONTEXT_VIEW_HISTORY = "View player history",
        PLAYER_CONTEXT_EDIT_NOTE = "Edit note",
        PLAYER_CONTEXT_COPY_RAIDERIO = "Copy Raider.IO link",
        CHARACTER_OPEN_CURRENT = "Open current character",
        KNOWN_PLAYER_ALERT = "You already ran %d keys with %s (%d timed)",
        KNOWN_PLAYER_ALERT_TEMPLATE = "{{star}} You already ran [runs] keys with [player] ([timed] timed) | Note : [note]",

        SLASH_OPEN = "/smh - open / close the main window",
        SLASH_ME = "/smh me - open your current character history",
        SLASH_PLAYER = "/smh player Name-Realm - open a player's history",
        SLASH_TEST = "/smh test [full|compact] - preview completion popup",
        SLASH_DEBUG = "/smh debug - show debug logging status",
        SLASH_DEBUG_TOGGLE = "/smh debug on|off - enable / disable debug logging",
        SLASH_DEBUG_LAST = "/smh debug last - print the latest debug report",
        SLASH_DEBUG_CLEAR = "/smh debug clear - clear all saved debug reports",
        DEBUG_ENABLED = "Mythic+ debug logging enabled.",
        DEBUG_DISABLED = "Mythic+ debug logging disabled.",
        DEBUG_CLEARED = "Saved debug reports cleared.",
        DEBUG_STATUS = "Debug logging is %s. Saved reports: %d.",
        DEBUG_OPTIONS = "Type /smh debug on|off|last|clear for debug options.",
        UNKNOWN_DEBUG_SUBCOMMAND = "Unknown debug subcommand: %s",
        UNKNOWN_COMMAND = "Unknown command: %s",
        CHARACTER_NOT_FOUND = "Could not resolve your current character name.",
        HELP_HINT = "Type /smh help for a list of commands.",
    },
    ptBR = {
        ADDON_TITLE = "Sky Mythic History",
        LOCALE_AUTO = "Automatico (%s)",
        LOCALE_ENUS = "Ingles",
        LOCALE_PTBR = "Portugues (Brasil)",
        TAB_RUNS = "Runs",
        TAB_PLAYERS = "Jogadores",
        TAB_CHARACTERS = "Personagens",
        TAB_STATS = "Estatisticas",
        TAB_SETTINGS = "Configuracoes",
        NAVIGATION = "Navegacao",
        SAVED_RUNS = "Runs salvas",
        INDEXED_PLAYERS = "Jogadores indexados",
        NO_ACTIVE_RUN = "Nenhuma run ativa",
        ACTIVE_RUN = "Run ativa: %s +%d",

        SETTINGS_GENERAL = "Geral",
        SETTINGS_CHAT_ALERTS = "Alertas no chat",
        SETTINGS_CHAT_ALERTS_DESC = "Avise quando alguem do seu historico entrar no grupo.",
        SETTINGS_MINIMAP = "Botao no minimapa",
        SETTINGS_MINIMAP_DESC = "Mostra ou esconde o atalho circular ao redor do minimapa.",
        SETTINGS_LOOT_HISTORY = "Mostrar historico de loot",
        SETTINGS_LOOT_HISTORY_DESC = "Mostra itens dropados nos detalhes da run e no popup final.",
        SETTINGS_COMPLETION_POPUP = "Popup de conclusao",
        SETTINGS_POPUP_ENABLED = "Abrir resumo automaticamente",
        SETTINGS_POPUP_ENABLED_DESC = "Mostra a janela final com loot, dano, cura, interrupts, DPS e mortes.",
        SETTINGS_POPUP_MODE = "Modo de exibicao",
        SETTINGS_COMPACT_AUTOHIDE = "Fechar compacto automaticamente",
        SETTINGS_COMPACT_AUTOHIDE_DESC = "Fecha o toast compacto automaticamente apos a duracao escolhida.",
        SETTINGS_COMPACT_DURATION = "Duracao compacto (3-60s)",
        SETTINGS_OPEN_TIMING = "Momento de abertura",
        SETTINGS_DELAY = "Atraso (0-10s)",
        SETTINGS_SCALE = "Escala (0.7-1.4)",
        SETTINGS_SAVE = "Salvar",
        SETTINGS_APPLY = "Aplicar",
        SETTINGS_PREVIEW = "Abrir preview",
        SETTINGS_PREVIEW_COMPACT = "Preview compacto",
        SETTINGS_HISTORY = "Historico",
        SETTINGS_CLEAR_HISTORY = "Limpar historico",
        SETTINGS_CLEAR_HISTORY_CONFIRM = "Limpar todas as runs salvas do Sky Mythic History?",
        SETTINGS_DELETE_RUN_CONFIRM = "Apagar esta run do Sky Mythic History?\n%s",
        SETTINGS_HISTORY_NOTE = "O historico e compartilhado entre todos os personagens desta conta.",
        SETTINGS_SUMMARY = "Resumo",
        SETTINGS_POPUP_STATE = "Popup: %s (%s, atraso %ss, escala %.1f)",
        SETTINGS_SUMMARY_TEXT = "Runs salvas: %d\nJogadores indexados: %d\n%s\n%s\nFonte de dados: medidor de dano da Blizzard",
        SETTINGS_ENABLED = "ativado",
        SETTINGS_DISABLED = "desativado",

        POPUP_TRIGGER_LOOT_OPENED = "Depois de abrir o bau",
        POPUP_TRIGGER_COMPLETED = "Quando a run termina",
        POPUP_MODE_FULL = "Janela de resumo completa",
        POPUP_MODE_COMPACT = "Toast compacto clicavel",
        POPUP_MODE_OFF = "Desativado",

        MINIMAP_LEFT_CLICK = "Clique esquerdo: abrir historico",
        MINIMAP_RIGHT_CLICK = "Clique direito: abrir configuracoes",

        PLAYER_HISTORY_TIMED = "%d de %d no tempo",
        PLAYER_HISTORY_NOTE = "Nota: %s",
        PLAYER_CONTEXT_VIEW_HISTORY = "Ver historico do jogador",
        PLAYER_CONTEXT_EDIT_NOTE = "Editar nota",
        PLAYER_CONTEXT_COPY_RAIDERIO = "Copiar link Raider.IO",
        CHARACTER_OPEN_CURRENT = "Abrir personagem atual",
        KNOWN_PLAYER_ALERT = "Voce ja fez %d chaves com %s (%d no tempo)",
        KNOWN_PLAYER_ALERT_TEMPLATE = "{{star}} You already ran [runs] keys with [player] ([timed] timed) | Note : [note]",

        SLASH_OPEN = "/smh - abrir / fechar a janela principal",
        SLASH_ME = "/smh me - abrir historico do personagem atual",
        SLASH_PLAYER = "/smh player Name-Realm - abrir o historico de um jogador",
        SLASH_TEST = "/smh test [full|compact] - previsualizar popup de conclusao",
        SLASH_DEBUG = "/smh debug - mostrar status do debug",
        SLASH_DEBUG_TOGGLE = "/smh debug on|off - ativar / desativar logs de debug",
        SLASH_DEBUG_LAST = "/smh debug last - mostrar o ultimo relatorio de debug",
        SLASH_DEBUG_CLEAR = "/smh debug clear - limpar relatorios de debug salvos",
        DEBUG_ENABLED = "Log de debug Mythic+ ativado.",
        DEBUG_DISABLED = "Log de debug Mythic+ desativado.",
        DEBUG_CLEARED = "Relatorios de debug salvos foram limpos.",
        DEBUG_STATUS = "Debug esta %s. Relatorios salvos: %d.",
        DEBUG_OPTIONS = "Digite /smh debug on|off|last|clear para opcoes de debug.",
        UNKNOWN_DEBUG_SUBCOMMAND = "Subcomando de debug desconhecido: %s",
        UNKNOWN_COMMAND = "Comando desconhecido: %s",
        CHARACTER_NOT_FOUND = "Nao foi possivel identificar o personagem atual.",
        HELP_HINT = "Digite /smh help para ver a lista de comandos.",
    },
}

local wowLocaleToAddonLocale = {
    ptBR = "ptBR",
    ptPT = "ptBR",
}

local localeLabels = {
    auto = "LOCALE_AUTO",
    enUS = "LOCALE_ENUS",
    ptBR = "LOCALE_PTBR",
}

local function GetClientLocale()
    local locale = GetLocale and GetLocale() or DEFAULT_LOCALE
    return wowLocaleToAddonLocale[locale] or DEFAULT_LOCALE
end

function MythicTools:GetSupportedLocales()
    return translations
end

function MythicTools:GetDefaultLocalePreference()
    return DEFAULT_LOCALE
end

function MythicTools:NormalizeLocalePreference(locale)
    return DEFAULT_LOCALE
end

function MythicTools:GetEffectiveLocale()
    return DEFAULT_LOCALE
end

function MythicTools:L(key, ...)
    local locale = self:GetEffectiveLocale()
    local localeTable = translations[locale] or translations[DEFAULT_LOCALE]
    local text = localeTable[key] or translations[DEFAULT_LOCALE][key] or key
    if select("#", ...) > 0 then
        return text:format(...)
    end
    return text
end

function MythicTools:GetLocaleOptions()
    return {{value = DEFAULT_LOCALE, label = self:L(localeLabels.enUS)}}
end

function MythicTools:SetLocalePreference(locale)
    if not (self.db and self.db.settings) then
        return
    end

    self.db.settings.locale = nil
end
