from __future__ import annotations

# Tiny in-process translation table for the settings window - the overlay
# itself has no translatable text (just the title name from config and a
# generic "12.3s" countdown, which reads fine in either language).
#
# Deliberately not a full gettext/.po setup: two languages, one UI file,
# and this keeps the whole thing readable in one place without a build
# step or bundling extra locale files into the AppImage.

Lang = str  # "de" or "en"

_current_language: Lang = "de"

_STRINGS: dict[str, dict[Lang, str]] = {
    "settings_title": {"de": "Korin Timer - Einstellungen", "en": "Korin Timer - Settings"},
    "language_label": {"de": "Sprache:", "en": "Language:"},
    "profile_label": {"de": "Charakter:", "en": "Character:"},
    "profile_new": {"de": "Neu", "en": "New"},
    "profile_duplicate": {"de": "Duplizieren", "en": "Duplicate"},
    "profile_rename": {"de": "Umbenennen", "en": "Rename"},
    "general_header": {"de": "Allgemein", "en": "General"},
    "overlay_header": {"de": "Overlay", "en": "Overlay"},
    "dogma_mode": {"de": "Dogma-Modus", "en": "Dogma Mode"},
    "overlay_position": {"de": "Position", "en": "Position"},
    "margin_x": {"de": "Rand X", "en": "Margin X"},
    "margin_y": {"de": "Rand Y", "en": "Margin Y"},
    "scale": {"de": "Skalierung", "en": "Scale"},
    "reset_position": {
        "de": "Per Strg+Alt+Drag gesetzte Position zurücksetzen",
        "en": "Reset position set via Ctrl+Alt+Drag",
    },
    "titles_header": {"de": "Titel", "en": "Titles"},
    "col_id": {"de": "ID", "en": "ID"},
    "col_name": {"de": "Name", "en": "Name"},
    "col_hotkey": {"de": "Hotkey", "en": "Hotkey"},
    "col_cooldown": {"de": "Cooldown (s)", "en": "Cooldown (s)"},
    "add_title": {"de": "+ Titel hinzufügen", "en": "+ Add title"},
    "close": {"de": "Schließen", "en": "Close"},
    "save": {"de": "Speichern", "en": "Save"},
    "cancel": {"de": "Abbrechen", "en": "Cancel"},
    "ok": {"de": "OK", "en": "OK"},
    "delete": {"de": "Löschen", "en": "Delete"},
    "hotkey_tooltip": {
        "de": "Klicken, dann eine oder mehrere Tasten drücken und wieder loslassen "
        "(z.B. Strg+F1 für eine Kombination) - Esc = abbrechen",
        "en": "Click, then press and release one or more keys "
        "(e.g. Ctrl+F1 for a combo) - Esc = cancel",
    },
    "restart_on_repeat": {"de": "Neu triggerbar", "en": "Retriggerable"},
    "restart_on_repeat_tooltip": {
        "de": "Wenn aktiv: Haupttaste während laufendem Cooldown erneut drücken "
        "startet ihn neu (volle Dauer ab jetzt), statt ignoriert zu werden.",
        "en": "If enabled: pressing the main hotkey again while a cooldown is "
        "running restarts it (full duration from now) instead of being ignored.",
    },
    "remove_title": {"de": "Titel entfernen", "en": "Remove title"},
    "pre_activation_label": {"de": "Vor-Aktivierung:", "en": "Pre-activation:"},
    "pre_hotkey_tooltip": {
        "de": "Optional: zusätzliche Taste(nkombination), die nur Step 1 (Standby) "
        "auslöst - z.B. eine Taste, die im Spiel ohnehin gedrückt wird, damit "
        "die Haupttaste oben nur noch für Step 2 gebraucht wird.",
        "en": "Optional: an additional key (combo) that only triggers step 1 "
        "(standby) - e.g. a key already pressed during normal play, so the "
        "main hotkey above is only needed for step 2.",
    },
    "remove_pre_activation": {"de": "Vor-Aktivierung entfernen", "en": "Remove pre-activation"},
    "timeout_label": {"de": "Timeout (s):", "en": "Timeout (s):"},
    "standby_timeout_tooltip": {
        "de": "Wie lange nach Step 1 Zeit für Step 2 bleibt, bevor der Titel "
        "automatisch zurück auf Idle springt. 0 = kein Timeout (Standby "
        "bleibt bestehen, bis Step 2 gedrückt wird oder ein anderer Titel "
        "aktiviert wird).",
        "en": "How long after step 1 there is time for step 2 before the title "
        "automatically reverts to idle. 0 = no timeout (standby persists "
        "until step 2 is pressed or another title is activated).",
    },
    "remove_icon": {"de": "Icon entfernen", "en": "Remove icon"},
    "choose_key": {"de": "Taste wählen…", "en": "Choose key…"},
    "press_keys": {"de": "Taste(n) drücken…", "en": "Press key(s)…"},
    "none": {"de": "Keine", "en": "None"},
    "icon_button_label": {"de": "Icon…", "en": "Icon…"},
    "no_icon_selected": {"de": "Kein Icon ausgewählt", "en": "No icon selected"},
    "images_filter": {"de": "Bilder", "en": "Images"},
    "position_dragged": {
        "de": "Per Strg+Alt+Drag gesetzt: {x}, {y} (überschreibt Anker/Rand oben)",
        "en": "Set via Ctrl+Alt+Drag: {x}, {y} (overrides anchor/margin above)",
    },
    "position_follows_anchor": {
        "de": "Folgt Anker/Rand-Einstellung oben",
        "en": "Follows anchor/margin setting above",
    },
    "error_loading_profile": {
        "de": "Fehler beim Laden von '{name}': {error}",
        "en": "Error loading '{name}': {error}",
    },
    "new_character_title": {"de": "Neuer Charakter", "en": "New character"},
    "error_profile_exists": {
        "de": "Fehler: Charakter '{name}' existiert bereits.",
        "en": "Error: character '{name}' already exists.",
    },
    "error_generic": {"de": "Fehler: {error}", "en": "Error: {error}"},
    "profile_created": {"de": "Charakter '{name}' angelegt.", "en": "Character '{name}' created."},
    "duplicate_character_title": {"de": "Charakter duplizieren", "en": "Duplicate character"},
    "duplicate_name_suffix": {"de": "{name} Kopie", "en": "{name} copy"},
    "profile_duplicated": {
        "de": "Charakter '{name}' angelegt (Kopie).",
        "en": "Character '{name}' created (copy).",
    },
    "rename_character_title": {"de": "Charakter umbenennen", "en": "Rename character"},
    "profile_renamed": {"de": "Umbenannt zu '{name}'.", "en": "Renamed to '{name}'."},
    "error_cannot_delete_last": {
        "de": "Fehler: der letzte Charakter kann nicht gelöscht werden.",
        "en": "Error: the last character cannot be deleted.",
    },
    "delete_character_title": {"de": "Charakter löschen", "en": "Delete character"},
    "delete_character_confirm": {
        "de": "Charakter '{name}' und alle seine Titel/Hotkeys unwiderruflich löschen?",
        "en": "Permanently delete character '{name}' and all its titles/hotkeys?",
    },
    "profile_deleted": {"de": "Charakter '{name}' gelöscht.", "en": "Character '{name}' deleted."},
    "error_need_one_title": {
        "de": "Fehler: mindestens ein Titel nötig.",
        "en": "Error: at least one title is required.",
    },
    "error_empty_fields": {
        "de": "Fehler: id/Name/Hotkey dürfen nicht leer sein.",
        "en": "Error: id/name/hotkey must not be empty.",
    },
    "error_duplicate_id": {"de": "Fehler: doppelte Titel-id.", "en": "Error: duplicate title id."},
    "error_duplicate_hotkey": {
        "de": "Fehler: doppelte Hotkey-Zuordnung (Haupt- oder Vor-Aktivierungs-Taste).",
        "en": "Error: duplicate hotkey assignment (main or pre-activation key).",
    },
    "saved_to": {"de": "Gespeichert nach {path}", "en": "Saved to {path}"},
}


def set_language(lang: Lang) -> None:
    global _current_language
    if lang not in ("de", "en"):
        raise ValueError(f"unsupported language: {lang!r}")
    _current_language = lang


def get_language() -> Lang:
    return _current_language


def t(key: str, **kwargs) -> str:
    entry = _STRINGS[key]
    text = entry.get(_current_language, entry["de"])
    return text.format(**kwargs) if kwargs else text
