#!/usr/bin/env python3
"""
Hotkey trigger for immediate Steam DASH clip extraction (`--force`).
"""
import os
import shutil
import subprocess
import time
from pathlib import Path


def notify(title: str, body: str = "", icon: str = "video-x-generic", expire_ms: int = 3000):
    try:
        subprocess.Popen(
            ["notify-send", "-a", "Steam Clip Extractor", "-i", icon, "-t", str(expire_ms), title, body],
            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
        )
    except Exception:
        pass


def play_sound(sound_name: str):
    path = f"/usr/share/sounds/freedesktop/stereo/{sound_name}.oga"
    if os.path.exists(path):
        subprocess.Popen(["pw-play", path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)


def main():
    notify("Processing Steam Clip...", "Extracting MP4 in background...", expire_ms=2500)
    play_sound("message-new-instant")
    time.sleep(1.5)

    extractor = shutil.which("steam-extract-clips") or str(Path(__file__).resolve().with_name("steam-extract-clips"))
    res = subprocess.run([extractor, "--force"], capture_output=True, text=True)
    lines = [l for l in res.stdout.splitlines() if "[+] Extracted:" in l]

    if lines:
        notify("Clip Extraction Complete", f"Exported {len(lines)} clip(s):\n{lines[-1].replace('[+] Extracted: ', '')}", icon="dialog-ok", expire_ms=5000)
        play_sound("complete")
    else:
        notify("Steam Clips", "All matching clips are already extracted.", icon="dialog-information", expire_ms=3000)


if __name__ == "__main__":
    main()
