"""Settings for Bayan GlassCaption (Windows). Stored as JSON next to the app
so David can tweak by hand if needed, but everything important is adjustable
in the UI. Keys are identical to the Mac build; only the default fonts differ
(Tahoma has the best Arabic coverage on stock Windows)."""

import json
import os

APP_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_PATH = os.path.join(APP_DIR, "settings.json")

DEFAULTS = {
    # --- appearance ---
    "font_candidates": ["Tahoma", "Segoe UI", "Arial"],
    "font_candidates_latin": ["Segoe UI", "Tahoma", "Arial"],
    "font_size": 44,
    "fill_color": "#C9A227",      # BBS school gold (dark gold, not yellow)
    "outline_color": "#FFFFFF",   # prominent white outline
    "outline_width_pct": 9.0,     # stroke width as % of font size
    "max_lines": 2,
    "width_pct": 0.90,            # caption strip width as fraction of screen
    "bottom_margin": 56,          # px from bottom of screen
    "show_english": False,        # also show the spoken-language transcript
                                  # above the caption (name kept for compat)

    # --- audio / recognition ---
    "direction": "en2ar",         # "en2ar" (speak EN, show AR) or "ar2en"
    "mic_device": None,           # None = system default input
    "sample_rate": 16000,
    "model_size": "small.en",     # faster-whisper model for EN speech
    "model_size_ar": "medium",    # multilingual model for AR speech
                                  # (~1.5 GB; best live accuracy. "small" =
                                  # faster/lighter, "large-v3" = max accuracy
                                  # but likely too slow for live captions)
    "silence_ms": 350,            # pause length that ends a clause
    "min_speech_ms": 280,         # ignore blips shorter than this
    "max_segment_s": 5.5,         # force a caption even if no pause
    "pre_roll_ms": 240,           # audio kept from just before speech starts

    # --- behaviour ---
    "caption_linger_s": 6.0,      # clear caption after this much silence

    # --- translation engines (all optional; free endpoint used otherwise) ---
    # Google Cloud: just drop the service-account .json in the app folder.
    "azure_translator_key": "",     # Azure Translator F0 = 2M chars/mo free
    "azure_translator_region": "",  # e.g. "qatarcentral"; empty for global
}


def load():
    cfg = dict(DEFAULTS)
    try:
        with open(CONFIG_PATH, "r", encoding="utf-8") as f:
            cfg.update(json.load(f))
    except (OSError, ValueError):
        pass
    return cfg


def save(cfg):
    keep = {k: cfg[k] for k in DEFAULTS if k in cfg}
    try:
        with open(CONFIG_PATH, "w", encoding="utf-8") as f:
            json.dump(keep, f, indent=2, ensure_ascii=False)
    except OSError:
        pass
