"""Screen lock — jetzt sperren + Locker installieren."""
from __future__ import annotations

import gi

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Adw, Gtk  # noqa: E402

from taninux.core import lock
from taninux.gui.console import RunnerCard
from taninux.gui.process import ProcessRunner


def build() -> Gtk.Widget:
    page = Adw.PreferencesPage()
    group = Adw.PreferencesGroup(title="Screen lock")
    page.add(group)

    if not lock.locker():
        group.add(Adw.ActionRow(title="Install to enable screen locking"))
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
        box.set_margin_top(6)
        box.append(RunnerCard("Install", lock.install_argv(), privileged=True))
        group.add(box)
        return page

    runner = ProcessRunner()
    row = Adw.ActionRow(
        title="Lock now",
        subtitle="Auto-lock timing is set under Power.",
    )
    btn = Gtk.Button(label="Lock")
    btn.add_css_class("pill")
    btn.set_valign(Gtk.Align.CENTER)
    btn.connect("clicked", lambda _b: runner.run(lock.lock_now_argv() or []))
    row.add_suffix(btn)
    group.add(row)
    return page
