#!/usr/bin/env bash
# tv-tweaks.sh – Anzeige-Einstellungen: Animationen, Bildschirmschoner, Auflösung, Dichte
#
#   ./tv-tweaks.sh show                 # aktuelle Werte anzeigen
#   ./tv-tweaks.sh animations 0         # Animationen aus (0), halb (0.5) oder normal (1)
#   ./tv-tweaks.sh screensaver 600      # Bildschirmschoner nach 600 s (system: screen_off_timeout)
#   ./tv-tweaks.sh sleep 3600           # Ruhezustand nach 3600 s; „never“ = nie (secure: sleep_timeout)
#   ./tv-tweaks.sh stay-awake on|off    # „Aktiv lassen“ (global: stay_on_while_plugged_in)
#   ./tv-tweaks.sh size 1280x720        # Auflösung der Oberfläche überschreiben
#   ./tv-tweaks.sh density 240          # Anzeigedichte überschreiben
#   ./tv-tweaks.sh reset                # Auflösung, Dichte und Animationen zurücksetzen
#
# Vorsicht bei size/density: Eine unpassende Angabe macht das Bild unlesbar. „reset“ funktioniert
# auch blind – der Befehl braucht den Bildschirm nicht. Welche Zeitschlüssel die Oberfläche des
# Herstellers wirklich auswertet, ist geräteabhängig.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=tv-lib.sh
. "$SCRIPT_DIR/tv-lib.sh"

usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; exit "${1:-0}"; }

set_animations() {
  local key
  for key in window_animation_scale transition_animation_scale animator_duration_scale; do
    tv_shell settings put global "$key" "$1"
  done
}

cmd="${1:-}"
[ -n "$cmd" ] || usage 1
shift

case "$cmd" in
  show)
    tv_connect
    for key in window_animation_scale transition_animation_scale animator_duration_scale \
               stay_on_while_plugged_in; do
      printf 'global %-28s %s\n' "$key" "$(tv_out settings get global "$key")"
    done
    printf 'system %-28s %s\n' screen_off_timeout "$(tv_out settings get system screen_off_timeout)"
    printf 'secure %-28s %s\n' sleep_timeout "$(tv_out settings get secure sleep_timeout)"
    tv_out wm size
    tv_out wm density
    ;;
  animations)
    case "${1:-}" in
      0|0.5|1|1.5|2|5|10) tv_connect; set_animations "$1" ;;
      *) tv_die "Erlaubte Werte: 0, 0.5, 1, 1.5, 2, 5, 10" ;;
    esac
    ;;
  screensaver)
    case "${1:-}" in ""|*[!0-9]*) usage 1 ;; esac
    tv_connect
    tv_shell settings put system screen_off_timeout $(($1 * 1000))
    ;;
  sleep)
    tv_connect
    case "${1:-}" in
      never) tv_shell settings put secure sleep_timeout -1 ;;
      ""|*[!0-9]*) usage 1 ;;
      *) tv_shell settings put secure sleep_timeout $(($1 * 1000)) ;;
    esac
    ;;
  stay-awake)
    tv_connect
    case "${1:-}" in
      # 3 = Netzteil (1) + USB (2), vgl. BatteryManager.BATTERY_PLUGGED_*
      on)  tv_shell settings put global stay_on_while_plugged_in 3 ;;
      off) tv_shell settings put global stay_on_while_plugged_in 0 ;;
      *) usage 1 ;;
    esac
    ;;
  size)
    case "${1:-}" in
      [1-9]*x[1-9]*) ;;
      *) tv_die "Auflösung als BREITExHÖHE angeben, z. B. 1920x1080" ;;
    esac
    case "$1" in *[!0-9x]*) tv_die "Auflösung als BREITExHÖHE angeben, z. B. 1920x1080" ;; esac
    tv_connect
    tv_shell wm size "$1"
    ;;
  density)
    case "${1:-}" in ""|*[!0-9]*) usage 1 ;; esac
    [ "$1" -ge 72 ] || tv_die "Dichte zu klein (mindestens 72)."
    tv_connect
    tv_shell wm density "$1"
    ;;
  reset)
    tv_connect
    tv_shell wm size reset
    tv_shell wm density reset
    set_animations 1
    ;;
  -h|--help|help) usage 0 ;;
  *) usage 1 ;;
esac
