"""Accessibility-Schalter über gsettings (user-level, kein root)."""
from __future__ import annotations

import subprocess

_IFACE = "org.gnome.desktop.interface"
_A11Y = "org.gnome.desktop.a11y.interface"
_BIG_TEXT = 1.25


def _get(schema: str, key: str) -> str:
    try:
        r = subprocess.run(["gsettings", "get", schema, key],
                          capture_output=True, text=True, check=False, timeout=5)
        return r.stdout.strip()
    except (FileNotFoundError, subprocess.TimeoutExpired):
        return ""


def _set(schema: str, key: str, value: str) -> None:
    try:
        subprocess.run(["gsettings", "set", schema, key, value],
                      check=False, timeout=5)
    except (FileNotFoundError, subprocess.TimeoutExpired):
        pass


def large_text() -> bool:
    try:
        return float(_get(_IFACE, "text-scaling-factor") or "1") > 1.1
    except ValueError:
        return False


def set_large_text(on: bool) -> None:
    _set(_IFACE, "text-scaling-factor", str(_BIG_TEXT if on else 1.0))


def high_contrast() -> bool:
    return _get(_A11Y, "high-contrast") == "true"


def set_high_contrast(on: bool) -> None:
    _set(_A11Y, "high-contrast", "true" if on else "false")


def animations() -> bool:
    return _get(_IFACE, "enable-animations") != "false"


def set_animations(on: bool) -> None:
    _set(_IFACE, "enable-animations", "true" if on else "false")
