/* window.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
 */

/*
 * That's shitcode!!!!
 * TODO: Refactor it before first release (or at least somewhen)
 */

[GtkTemplate (ui = "/io/gitlab/nekocwd/pipetap/gui/window.ui")]
public class PipeTap.Ui.FloatingWindow : Gtk.Window, Gtk.Orientable {
    [GtkChild]
    private unowned Adw.Bin content_bin;

    public Gtk.Orientation orientation { get; set; default = Gtk.Orientation.HORIZONTAL; }

    public string window_name { get; set; default = ""; }


    public Gtk.Widget content { get { return content_bin.get_child (); } set { content_bin.set_child (value); } }

    public FloatingWindow (Gtk.Application app, string name) {
        Object (application: app);
        _window_name = name;
        GtkLayerShell.init_for_window (this);


        var positions = ((PipeTap.Application) app).settings.get_child (window_name + "-pos");
        positions.bind_with_mapping ("orientation", this, "orientation", GLib.SettingsBindFlags.DEFAULT,
                                     (value, variant) =>
                                     { message ("%s", variant.get_type_string ()); value = variant.get_int32 () == 0 ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; return true; },
                                     (value) =>
                                     { return ((Gtk.Orientation) value) == Gtk.Orientation.VERTICAL ? 1 : 0; },
                                     null, null
        );
        positions.changed.connect (set_position);
        set_position ();
    }

    void set_position () {
        var settings = Logic.app.settings.get_child (window_name + "-pos");

        GtkLayerShell.set_anchor (this, GtkLayerShell.Edge.TOP, settings.get_int ("margin-top") != -1);
        GtkLayerShell.set_anchor (this, GtkLayerShell.Edge.BOTTOM, settings.get_int ("margin-bottom") != -1);
        GtkLayerShell.set_anchor (this, GtkLayerShell.Edge.LEFT, settings.get_int ("margin-left") != -1);
        GtkLayerShell.set_anchor (this, GtkLayerShell.Edge.RIGHT, settings.get_int ("margin-right") != -1);

        GtkLayerShell.set_margin (this, GtkLayerShell.Edge.TOP, settings.get_int ("margin-top"));
        GtkLayerShell.set_margin (this, GtkLayerShell.Edge.BOTTOM, settings.get_int ("margin-bottom"));
        GtkLayerShell.set_margin (this, GtkLayerShell.Edge.LEFT, settings.get_int ("margin-left"));
        GtkLayerShell.set_margin (this, GtkLayerShell.Edge.RIGHT, settings.get_int ("margin-right"));
    }
}