/*
 * Copyright (c) 2026 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

using GLib;


namespace Xfce
{
    public class ScreenSaverProvider : Ft.Provider, Ft.ScreenSaverProvider
    {
        public bool active {
            get {
                return this._active;
            }
        }

        private bool                _active = false;
        private Xfce.ScreenSaver?   screensaver_proxy = null;
        private uint                watcher_id = 0;
        private ulong               active_changed_id = 0;

        private void on_name_appeared (GLib.DBusConnection connection,
                                       string              name,
                                       string              name_owner)
        {
            this.available = true;
        }

        private void on_name_vanished (GLib.DBusConnection? connection,
                                       string               name)
        {
            this.available = false;
        }

        private void on_active_changed (bool new_value)
        {
            if (this._active != new_value) {
                this._active = new_value;
                this.notify_property ("active");
            }
        }

        public override async void initialize (GLib.Cancellable? cancellable) throws GLib.Error
        {
            this.watcher_id = GLib.Bus.watch_name (GLib.BusType.SESSION,
                                                   "org.xfce.ScreenSaver",
                                                   GLib.BusNameWatcherFlags.NONE,
                                                   this.on_name_appeared,
                                                   this.on_name_vanished);
        }

        public override async void uninitialize () throws GLib.Error
        {
            if (this.watcher_id != 0) {
                GLib.Bus.unwatch_name (this.watcher_id);
                this.watcher_id = 0;
            }
        }

        public override async void enable (GLib.Cancellable? cancellable) throws GLib.Error
        {
            try {
                this.screensaver_proxy = yield GLib.Bus.get_proxy<Xfce.ScreenSaver>
                                    (GLib.BusType.SESSION,
                                     "org.xfce.ScreenSaver",
                                     "/org/xfce/ScreenSaver",
                                     GLib.DBusProxyFlags.DO_NOT_AUTO_START,
                                     cancellable);
                this._active = yield this.screensaver_proxy.get_active ();

                this.active_changed_id = this.screensaver_proxy.active_changed.connect (this.on_active_changed);
            }
            catch (GLib.Error error) {
                GLib.warning ("Error while initializing session proxy: %s", error.message);
            }
        }

        public override async void disable () throws GLib.Error
        {
            if (this.active_changed_id != 0) {
                this.screensaver_proxy.disconnect (this.active_changed_id);
                this.active_changed_id = 0;
            }

            this.screensaver_proxy = null;

            if (this._active) {
                this._active = false;
                this.notify_property ("active");
            }
        }
    }
}
