use std::env;
use std::sync::OnceLock;

pub struct Lang {
    pub hud_cuques: &'static str,
    pub hud_fps: &'static str,
    /// Short form of the FPS/DPS acronym (no trademark, no expansion).
    /// Used in inline strings like "+5% FPS" / "+5% DPS" where the long
    /// form would be too noisy.
    pub fps_unit: &'static str,
    pub help_game: &'static str,
    pub help_stats: &'static str,
    pub help_ach: &'static str,
    pub fingerers_title: &'static str,
    pub owned: &'static str,
    pub cost: &'static str,
    pub fps_each: &'static str,
    pub fingerer_names: &'static [&'static str],
    pub stats_title: &'static str,
    pub stat_session_time: &'static str,
    pub stat_total_time: &'static str,
    pub stat_total_clicks: &'static str,
    pub stat_lifetime_cuques: &'static str,
    pub stat_best_fps: &'static str,
    pub stat_achievements: &'static str,
    pub stat_fingerers_owned: &'static str,
    pub stat_golden_caught: &'static str,
    pub stat_lucky_caught: &'static str,
    pub stat_frenzy_caught: &'static str,
    pub stat_buff_caught: &'static str,
    pub stat_green_coin_caught: &'static str,
    pub achievements_title: &'static str,
    pub ach_unlocked: &'static str,
    pub ach_locked: &'static str,
    pub ach_summary: &'static str,
    pub achievement_names: &'static [&'static str],
    pub achievement_descs: &'static [&'static str],
    pub upgrades_title: &'static str,
    pub upgrades_none: &'static str,
    pub upgrade_names: &'static [&'static str],
    pub upgrade_descs: &'static [&'static str],
    pub help_upgrades: &'static str,
    pub prestige_title: &'static str,
    pub prestige_currency: &'static str,
    pub prestige_owned_label: &'static str,
    pub prestige_bonus_label: &'static str,
    pub prestige_available_label: &'static str,
    pub prestige_lifetime_needed: &'static str,
    pub prestige_confirm_hint: &'static str,
    pub prestige_not_enough: &'static str,
    pub help_prestige: &'static str,
}

pub const EN: Lang = Lang {
    hud_cuques: "Cuques",
    hud_fps: "FPS (Fingers per Second®)",
    fps_unit: "FPS",
    help_game: "[Space/Click] finger  [Click/1-0] buy  [Shift] x10  [Alt/RClick] max  [-/+/Wheel] zoom\n[u] upgrades  [p] prestige  [s] stats  [a] achievements  [g] grab golden/powerup  [q] quit",
    help_stats: "[s/Esc] back to game  [q] quit",
    help_ach: "[a/Esc] back to game  [q] quit",
    fingerers_title: " Fingerers ",
    owned: "owned",
    cost: "cost",
    fps_each: "FPS each",
    fingerer_names: &[
        "Index Finger",
        "Whole Hand",
        "Latex Glove",
        "Greek Kiss",
        "Robotic Finger",
        "Tentacle",
        "Finger Vortex",
        "Dimensional Hole",
        "Cosmic Finger",
        "Hand of God",
    ],
    stats_title: " Stats ",
    stat_session_time: "Session time",
    stat_total_time: "Total play time",
    stat_total_clicks: "Total fingerings",
    stat_lifetime_cuques: "Lifetime cuques",
    stat_best_fps: "Best FPS",
    stat_achievements: "Achievements",
    stat_fingerers_owned: "Fingerers owned",
    stat_golden_caught: "Powerups caught",
    stat_lucky_caught: "  ↳ Golden",
    stat_frenzy_caught: "  ↳ Frenzy",
    stat_buff_caught: "  ↳ Buff",
    stat_green_coin_caught: "  ↳ Green Coin",
    achievements_title: " Achievements ",
    ach_unlocked: "[X]",
    ach_locked: "[ ]",
    ach_summary: "unlocked",
    achievement_names: &[
        "First Finger",
        "Warming Up",
        "Seasoned Fingerer",
        "Cuque Mogul",
        "Automation",
        "Factory of Fingers",
        "Latex Enjoyer",
        "Golden Touch",
        "Golden Hoarder",
        "Rise of the Machines",
    ],
    achievement_descs: &[
        "Finger the cuque for the first time",
        "Accumulate 100 cuques",
        "Accumulate 10,000 cuques",
        "Accumulate 1,000,000 cuques",
        "Buy your first fingerer",
        "Own 10 Whole Hands",
        "Own 10 Latex Gloves",
        "Catch your first Golden Cuque",
        "Catch 10 Golden Cuques",
        "Own a Robotic Finger",
    ],
    upgrades_title: " Upgrades ",
    upgrades_none: "No upgrades available yet.\nKeep fingering!",
    upgrade_names: &[
        "Licked Finger",
        "Greased Palm",
        "Calloused Tip",
        "Sharper Nail",
        "Reinforced Index",
        "Chrome Phalanx",
        "Full Grip",
        "Whole Fist",
        "Double Hand",
        "Slicker Latex",
        "Medical Grade",
        "Industrial Latex",
        "Finer Joints",
        "Quantum Servo",
        "Singularity Core",
        "Holy Lubrication",
        "Barbed Suckers",
        "Kraken Coupling",
        "Deep Sea Mastery",
        "Spin Doubler",
        "Hurricane Spin",
        "Galaxy Rotator",
        "Wider Aperture",
        "Rift Stabilizer",
        "Wormhole Loom",
        "Cosmic Attunement",
        "Astral Projection",
        "Heat Death Bypass",
        "Divine Manicure",
        "Miracle Protocol",
        "Omnipotent Touch",
        "Wetter Tongue",
        "Breath Training",
        "Olympic Technique",
    ],
    upgrade_descs: &[
        "Clicks are twice as effective",
        "Clicks are twice as effective again",
        "Clicks are twice as effective once more",
        "Index Fingers produce twice as much",
        "Index Fingers produce twice as much (again)",
        "Index Fingers produce twice as much (a third time)",
        "Whole Hands produce twice as much",
        "Whole Hands produce twice as much (again)",
        "Whole Hands produce twice as much (a third time)",
        "Latex Gloves produce twice as much",
        "Latex Gloves produce twice as much (again)",
        "Latex Gloves produce twice as much (a third time)",
        "Robotic Fingers produce twice as much",
        "Robotic Fingers produce twice as much (again)",
        "Robotic Fingers produce twice as much (a third time)",
        "All fingerers are 50% more effective",
        "Tentacles produce twice as much",
        "Tentacles produce twice as much (again)",
        "Tentacles produce twice as much (a third time)",
        "Finger Vortices produce twice as much",
        "Finger Vortices produce twice as much (again)",
        "Finger Vortices produce twice as much (a third time)",
        "Dimensional Holes produce twice as much",
        "Dimensional Holes produce twice as much (again)",
        "Dimensional Holes produce twice as much (a third time)",
        "Cosmic Fingers produce twice as much",
        "Cosmic Fingers produce twice as much (again)",
        "Cosmic Fingers produce twice as much (a third time)",
        "Hands of God produce twice as much",
        "Hands of God produce twice as much (again)",
        "Hands of God produce twice as much (a third time)",
        "Greek Kisses produce twice as much",
        "Greek Kisses produce twice as much (again)",
        "Greek Kisses produce twice as much (a third time)",
    ],
    help_upgrades: "[u/Esc] back  [Click/1-9,0] buy upgrade  [q] quit",
    prestige_title: " Prestige ",
    prestige_currency: "Tissue Paper",
    prestige_owned_label: "Currently owned",
    prestige_bonus_label: "FPS bonus",
    prestige_available_label: "Available to claim",
    prestige_lifetime_needed: "Next papel at",
    prestige_confirm_hint: "Press [r] to reset and claim",
    prestige_not_enough: "Earn more lifetime cuques to claim.\nFormula: sqrt(lifetime / 1,000,000)",
    help_prestige: "[p/Esc] back  [r] reset & claim  [q] quit",
};

pub const PT_BR: Lang = Lang {
    hud_cuques: "Cuques",
    hud_fps: "DPS (Dedadas por Segundo®)",
    fps_unit: "DPS",
    help_game: "[Espaço/Clique] dedar  [Clique/1-0] comprar  [Shift] x10  [Alt/Bot.Dir.] max  [-/+/Roda] zoom\n[u] melhorias  [p] prestígio  [s] stats  [a] conquistas  [g] pegar dourado/bônus  [q] sair",
    help_stats: "[s/Esc] voltar ao jogo  [q] sair",
    help_ach: "[a/Esc] voltar ao jogo  [q] sair",
    fingerers_title: " Dedadores ",
    owned: "possui",
    cost: "custo",
    fps_each: "DPS cada",
    fingerer_names: &[
        "Dedo Indicador",
        "Mão Inteira",
        "Luva de Látex",
        "Beijo Grego",
        "Dedo Robótico",
        "Tentáculo",
        "Vórtice de Dedos",
        "Buraco Dimensional",
        "Dedo Cósmico",
        "Mão de Deus",
    ],
    stats_title: " Estatísticas ",
    stat_session_time: "Tempo de sessão",
    stat_total_time: "Tempo total de jogo",
    stat_total_clicks: "Total de dedadas",
    stat_lifetime_cuques: "Cuques na vida",
    stat_best_fps: "Melhor DPS",
    stat_achievements: "Conquistas",
    stat_fingerers_owned: "Dedadores possuídos",
    stat_golden_caught: "Powerups pegos",
    stat_lucky_caught: "  ↳ Dourado",
    stat_frenzy_caught: "  ↳ Frenzy",
    stat_buff_caught: "  ↳ Buff",
    stat_green_coin_caught: "  ↳ Moeda Verde",
    achievements_title: " Conquistas ",
    ach_unlocked: "[X]",
    ach_locked: "[ ]",
    ach_summary: "desbloqueadas",
    achievement_names: &[
        "Primeiro Dedo",
        "Esquentando",
        "Dedador Experiente",
        "Magnata do Cuque",
        "Automação",
        "Fábrica de Dedos",
        "Amante de Látex",
        "Toque Dourado",
        "Acumulador Dourado",
        "Ascensão das Máquinas",
    ],
    achievement_descs: &[
        "Dedar o cuque pela primeira vez",
        "Acumular 100 cuques",
        "Acumular 10.000 cuques",
        "Acumular 1.000.000 de cuques",
        "Comprar o primeiro dedador",
        "Possuir 10 Mãos Inteiras",
        "Possuir 10 Luvas de Látex",
        "Pegar o primeiro Cuque Dourado",
        "Pegar 10 Cuques Dourados",
        "Possuir um Dedo Robótico",
    ],
    upgrades_title: " Melhorias ",
    upgrades_none: "Nenhuma melhoria disponível.\nContinue dedando!",
    upgrade_names: &[
        "Dedo Lambido",
        "Palma Lubrificada",
        "Ponta Calejada",
        "Unha Afiada",
        "Indicador Reforçado",
        "Falange de Cromo",
        "Pegada Firme",
        "Punho Inteiro",
        "Mão Dupla",
        "Látex Escorregadio",
        "Grau Médico",
        "Látex Industrial",
        "Juntas Refinadas",
        "Servo Quântico",
        "Núcleo de Singularidade",
        "Lubrificação Sagrada",
        "Ventosas Farpadas",
        "Acoplamento Kraken",
        "Maestria Abissal",
        "Giro Duplo",
        "Giro Furacão",
        "Rotator Galáctico",
        "Abertura Ampliada",
        "Estabilizador de Fenda",
        "Tear de Buraco de Minhoca",
        "Sintonia Cósmica",
        "Projeção Astral",
        "Bypass da Morte Térmica",
        "Manicure Divina",
        "Protocolo Milagroso",
        "Toque Onipotente",
        "Língua Molhada",
        "Treino de Respiração",
        "Técnica Olímpica",
    ],
    upgrade_descs: &[
        "Dedadas valem o dobro",
        "Dedadas valem o dobro de novo",
        "Dedadas valem o dobro mais uma vez",
        "Dedos Indicadores produzem o dobro",
        "Dedos Indicadores produzem o dobro (de novo)",
        "Dedos Indicadores produzem o dobro (terceira vez)",
        "Mãos Inteiras produzem o dobro",
        "Mãos Inteiras produzem o dobro (de novo)",
        "Mãos Inteiras produzem o dobro (terceira vez)",
        "Luvas de Látex produzem o dobro",
        "Luvas de Látex produzem o dobro (de novo)",
        "Luvas de Látex produzem o dobro (terceira vez)",
        "Dedos Robóticos produzem o dobro",
        "Dedos Robóticos produzem o dobro (de novo)",
        "Dedos Robóticos produzem o dobro (terceira vez)",
        "Todos os dedadores 50% mais eficientes",
        "Tentáculos produzem o dobro",
        "Tentáculos produzem o dobro (de novo)",
        "Tentáculos produzem o dobro (terceira vez)",
        "Vórtices de Dedos produzem o dobro",
        "Vórtices de Dedos produzem o dobro (de novo)",
        "Vórtices de Dedos produzem o dobro (terceira vez)",
        "Buracos Dimensionais produzem o dobro",
        "Buracos Dimensionais produzem o dobro (de novo)",
        "Buracos Dimensionais produzem o dobro (terceira vez)",
        "Dedos Cósmicos produzem o dobro",
        "Dedos Cósmicos produzem o dobro (de novo)",
        "Dedos Cósmicos produzem o dobro (terceira vez)",
        "Mãos de Deus produzem o dobro",
        "Mãos de Deus produzem o dobro (de novo)",
        "Mãos de Deus produzem o dobro (terceira vez)",
        "Beijos Gregos produzem o dobro",
        "Beijos Gregos produzem o dobro (de novo)",
        "Beijos Gregos produzem o dobro (terceira vez)",
    ],
    help_upgrades: "[u/Esc] voltar  [Clique/1-9,0] comprar melhoria  [q] sair",
    prestige_title: " Prestígio ",
    prestige_currency: "Papel de Seda",
    prestige_owned_label: "Possui atualmente",
    prestige_bonus_label: "Bônus de DPS",
    prestige_available_label: "Disponível para resgatar",
    prestige_lifetime_needed: "Próximo papel em",
    prestige_confirm_hint: "Pressione [r] para resetar e resgatar",
    prestige_not_enough: "Acumule mais cuques totais para resgatar.\nFórmula: raiz(total / 1.000.000)",
    help_prestige: "[p/Esc] voltar  [r] resetar & resgatar  [q] sair",
};

static LANG: OnceLock<&'static Lang> = OnceLock::new();

pub fn init() {
    let _ = LANG.set(detect());
}

pub fn t() -> &'static Lang {
    LANG.get().copied().unwrap_or(&EN)
}

fn detect() -> &'static Lang {
    let raw = env::var("LANG").unwrap_or_default().to_lowercase();
    let code = raw.split('.').next().unwrap_or("");
    if code.starts_with("pt") { &PT_BR } else { &EN }
}
