/* application.vala
 *
 * Copyright 2025 Vasiliy Doylov <nekocwd@mainlining.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

public class PipeTap.Application : Adw.Application {
    public Settings settings;
    public Ui.SliderOverlay exposure;
    public Ui.SliderOverlay focus;
    public Ui.SliderOverlay contrast;
    public Ui.MainBar main_bar;
    public string device { get; set; }

    public Application () {
        Object (
                application_id: "io.gitlab.nekocwd.pipetap",
                flags: ApplicationFlags.DEFAULT_FLAGS
        );
    }

    public override bool dbus_register (GLib.DBusConnection connection, string object_path) {
        base.dbus_register (connection, object_path);
        DBus.init (connection, object_path);
        return true;
    }

    construct {
        ActionEntry[] action_entries = {
            { "about", this.on_about_action },
            { "preferences", this.on_preferences_action },
            { "quit", this.quit },
        };
        this.add_action_entries (action_entries, this);
        this.set_accels_for_action ("app.quit", { "<primary>q" });
        settings = new Settings ("io.gitlab.nekocwd.pipetap");
        if (settings.get_boolean ("is-uninitialised"))
            reset_settings ();
    }

    public override void activate () {
        base.activate ();

        var styling = new Gtk.CssProvider ();
        styling.load_from_resource ("/io/gitlab/nekocwd/pipetap/style.css");
        Gtk.StyleContext.add_provider_for_display (Gdk.Display.get_default (),
                                                   styling,
                                                   Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

        if (exposure == null)
            exposure = new Ui.SliderOverlay (this, Logic.Ctrl.CtrlType.Exposure, Logic.Ctrl.CtrlType.ExposureEnable, "exp", "camera-iso-symbolic");
        if (focus == null)
            focus = new Ui.SliderOverlay (this, Logic.Ctrl.CtrlType.Focus, Logic.Ctrl.CtrlType.AutoFocusEnable, "focus", "camera-focus-symbolic");
        if (contrast == null)
            contrast = new Ui.SliderOverlay (this, Logic.Ctrl.CtrlType.Contrast, Logic.Ctrl.CtrlType.ContrastEnable, "contrast", "color-symbolic");
        if (main_bar == null)
            main_bar = new Ui.MainBar (this);

        bind_property ("device", main_bar, "visible", GLib.BindingFlags.SYNC_CREATE, (b, v, ref tgt) => {
            tgt.set_boolean (device != null);
            message ("%s", device);
            return true;
        });
    }

    private void on_about_action () {
        string[] developers = { "Vasiliy Doylov <nekocwd@mainlining.org>" };
        string[] artists = { "magdesign" };
        var about = new Adw.AboutDialog () {
            application_name = "pipetap",
            application_icon = "io.gitlab.nekocwd.pipetap",
            developer_name = "NekoCWD",
            translator_credits = _("translator-credits"),
            version = "0.1.0",
            developers = developers,
            artists = artists,
            copyright = "© 2025 Vasiliy Doylov",
        };

        about.present (null);
    }

    private void on_preferences_action () {
        new Ui.Settings ().present (null);
    }

    public void reset_settings () {
        settings.set_boolean ("is-uninitialised", false);
        var settings = this.settings.get_child ("main-controls-pos");
        settings.set_int ("orientation", 1);
        settings.set_int ("margin-right", 80);
        settings.set_int ("margin-left", 80);
        settings.set_int ("margin-top", 65);
        settings.set_int ("margin-bottom", -1);
        settings.apply ();
        settings = this.settings.get_child ("exp-pos");
        settings.set_int ("orientation", 0);
        settings.set_int ("margin-right", 10);
        settings.set_int ("margin-left", -1);
        settings.set_int ("margin-top", 65);
        settings.set_int ("margin-bottom", 180);
        settings.apply ();
        settings = this.settings.get_child ("contrast-pos");
        settings.set_int ("orientation", 1);
        settings.set_int ("margin-right", 10);
        settings.set_int ("margin-left", 10);
        settings.set_int ("margin-top", -1);
        settings.set_int ("margin-bottom", 110);
        settings.apply ();
        settings = this.settings.get_child ("focus-pos");
        settings.set_int ("orientation", 0);
        settings.set_int ("margin-right", -1);
        settings.set_int ("margin-left", 10);
        settings.set_int ("margin-top", 65);
        settings.set_int ("margin-bottom", 180);
        settings.apply ();
        this.settings.apply ();
    }
}
