namespace StillTerminal {
    public class StSettings : GLib.Object {
        // General Settings
        public int window_width { get; set; }
        public int window_height { get; set; }
        public bool keep_window_size { get; set; }
        public bool start_maximized { get; set; }
        public double cell_height { get; set; }
        public double cell_width { get; set; }

        // Appearance
        public string system_color { get; set; }
        public int padding { get; set; }
        public int opacity { get; set; }
        public bool use_custom_font { get; set; }
        public string custom_font { get; set; }
        public bool bold_is_bright { get; set; }

        // Scroll
        public bool show_scrollbar { get; set; }
        public int scrollback_limit { get; set; }

        // Misc
        public bool easy_copy_paste { get; set; }
        public bool notification_on_task { get; set; }
        public string last_profile_id { get; set; }
        public bool warn_on_paste { get; set; }
        public bool new_tab_button_opens_default { get; set; }
        public GLib.Settings settings;

        public StSettings () {
            settings = new GLib.Settings ("io.stillhq.terminal");
            settings.bind ("window-width", this, "window_width", SettingsBindFlags.DEFAULT);
            settings.bind ("window-height", this, "window_height", SettingsBindFlags.DEFAULT);
            settings.bind ("keep-window-size", this, "keep_window_size", SettingsBindFlags.DEFAULT);
            settings.bind ("start-maximized", this, "start_maximized", SettingsBindFlags.DEFAULT);
            settings.bind ("use-custom-font", this, "use_custom_font", SettingsBindFlags.DEFAULT);
            settings.bind ("custom-font", this, "custom_font", SettingsBindFlags.DEFAULT);
            settings.bind ("cell-height", this, "cell_height", SettingsBindFlags.DEFAULT);
            settings.bind ("cell-width", this, "cell_width", SettingsBindFlags.DEFAULT);
            settings.bind ("bold-is-bright", this, "bold_is_bright", SettingsBindFlags.DEFAULT);
            settings.bind ("easy-copy-paste", this, "easy_copy_paste", SettingsBindFlags.DEFAULT);
            settings.bind ("system-color", this, "system_color", SettingsBindFlags.DEFAULT);
            settings.bind ("padding", this, "padding", SettingsBindFlags.DEFAULT);
            settings.bind ("opacity", this, "opacity", SettingsBindFlags.DEFAULT);
            settings.bind ("show-scrollbar", this, "show_scrollbar", SettingsBindFlags.DEFAULT);
            settings.bind ("scrollback-limit", this, "scrollback_limit", SettingsBindFlags.DEFAULT);
            settings.bind ("notification-on-task", this, "notification_on_task", SettingsBindFlags.DEFAULT);
            settings.bind ("last-profile-id", this, "last_profile_id", SettingsBindFlags.DEFAULT);
            settings.bind ("warn-on-paste", this, "warn_on_paste", SettingsBindFlags.DEFAULT);
            settings.bind (
                "new-tab-button-opens-default",
                this,
                "new_tab_button_opens_default",
                SettingsBindFlags.DEFAULT
            );
        }

        public void bind_to_vte (StTerminal vte) {
            settings.bind ("cell-height", vte, "cell_height_scale", SettingsBindFlags.DEFAULT);
            settings.bind ("cell-width", vte, "cell_width_scale", SettingsBindFlags.DEFAULT);
            settings.bind ("bold-is-bright", vte, "bold_is_bright", SettingsBindFlags.DEFAULT);

            settings.bind ("padding", vte, "margin-start", SettingsBindFlags.DEFAULT);
            settings.bind ("padding", vte, "margin-end", SettingsBindFlags.DEFAULT);
            settings.bind ("padding", vte, "margin-top", SettingsBindFlags.DEFAULT);
            settings.bind ("padding", vte, "margin-bottom", SettingsBindFlags.DEFAULT);

            vte.style_manager.notify["dark"].connect (
                (_style_manager, _pspec) => {
                vte.set_appearance ();
            }
                );

            if (vte.profile.color_scheme == "system") {
                this.notify["system-color"].connect (
                    (_settings, _pspec) => {
                    vte.set_appearance ();
                }
                    );
            }


            this.notify["opacity"].connect (
                (_settings, _pspec) => {
                vte.set_appearance ();
            }
                );
        }

        public void bind_to_general (StPrefsGeneralPage general) {
            settings.bind ("window-width", general.window_group.window_width, "value", SettingsBindFlags.DEFAULT);
            settings.bind ("window-height", general.window_group.window_height, "value", SettingsBindFlags.DEFAULT);
            settings.bind ("keep-window-size", general.window_group.save_window_size, "active", SettingsBindFlags.DEFAULT);
            settings.bind ("keep-window-size", general.window_group.window_width, "sensitive", SettingsBindFlags.INVERT_BOOLEAN);
            settings.bind ("keep-window-size", general.window_group.window_height, "sensitive", SettingsBindFlags.INVERT_BOOLEAN);
            settings.bind ("start-maximized", general.window_group.start_maximized, "active", SettingsBindFlags.DEFAULT);
            settings.bind (
                "new-tab-button-opens-default",
                general.tab_group.new_tab_button_opens_default,
                "active",
                SettingsBindFlags.DEFAULT
            );
            settings.bind ("cell-height", general.cell_spacing_group.cell_height, "value", SettingsBindFlags.DEFAULT);
            settings.bind ("cell-width", general.cell_spacing_group.cell_width, "value", SettingsBindFlags.DEFAULT);
            settings.bind ("padding", general.appearance_group.padding, "value", SettingsBindFlags.DEFAULT);
            settings.bind ("opacity", general.appearance_group.opacity_setting, "value", SettingsBindFlags.DEFAULT);
            settings.bind ("use-custom-font", general.appearance_group.use_custom_font, "active", SettingsBindFlags.DEFAULT);
            settings.bind ("use-custom-font", general.appearance_group.custom_font, "sensitive", SettingsBindFlags.DEFAULT);
            settings.bind ("bold-is-bright", general.appearance_group.bold_is_bright, "active", SettingsBindFlags.DEFAULT);
            settings.bind ("show-scrollbar", general.appearance_group.show_scrollbars, "active", SettingsBindFlags.INVERT_BOOLEAN);

            // Initialize and keep the row subtitle in sync with setting
            general.appearance_group.scheme_setting_changed (this.settings, "system-color");
            settings.changed.connect ((key) => {
                if (key == "system-color") {
                    general.appearance_group.scheme_setting_changed (this.settings, key);
                }
            });

            // Connecting the font button
            general.appearance_group.font_button.set_font_desc (
                Pango.FontDescription.from_string (this.custom_font)
                );
            general.appearance_group.font_button.notify["font-desc"].connect (
                (_button) => {
                general.appearance_group.font_button_changed (this.settings);
            }
                );
            settings.changed.connect ((key) => {
                if (key == "custom-font") {
                    general.appearance_group.font_setting_changed (this.settings, key);
                }
            });
        }

        public void refresh_accelerators (Gtk.Application app) {
            app.set_accels_for_action ("win.new-tab", settings.get_strv ("shortcut-new-tab"));
            app.set_accels_for_action ("win.reopen-last-tab", settings.get_strv ("shortcut-reopen-last-tab"));
            app.set_accels_for_action ("win.close-tab", settings.get_strv ("shortcut-close-tab"));
            app.set_accels_for_action ("win.next-tab", settings.get_strv ("shortcut-next-tab"));
            app.set_accels_for_action ("win.previous-tab", settings.get_strv ("shortcut-previous-tab"));
            app.set_accels_for_action ("win.copy", settings.get_strv ("shortcut-copy"));
            app.set_accels_for_action ("win.paste", settings.get_strv ("shortcut-paste"));
            app.set_accels_for_action ("win.fullscreen", settings.get_strv ("shortcut-fullscreen"));
            app.set_accels_for_action ("app.new-window", settings.get_strv ("shortcut-new-window"));
            app.set_accels_for_action ("win.preferences", settings.get_strv ("shortcut-preferences"));
            app.set_accels_for_action ("app.zoom-in", settings.get_strv ("shortcut-zoom-in"));
            app.set_accels_for_action ("app.zoom-out", settings.get_strv ("shortcut-zoom-out"));
            app.set_accels_for_action ("app.select-all", settings.get_strv ("shortcut-select-all"));
            // Tab overview
            app.set_accels_for_action ("win.tab-overview-toggle", settings.get_strv ("shortcut-tab-overview-toggle"));
            app.set_accels_for_action ("win.tab-overview-open", settings.get_strv ("shortcut-tab-overview-open"));
        }

        public void bind_to_shortcut_controller (ShortcutController controller) {
            settings.bind ("shortcut-new-tab", controller, "new-tab", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-reopen-last-tab", controller, "reopen-last-tab", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-close-tab", controller, "close-tab", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-next-tab", controller, "next-tab", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-previous-tab", controller, "previous-tab", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-copy", controller, "copy", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-paste", controller, "paste", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-fullscreen", controller, "fullscreen", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-new-window", controller, "new-window", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-preferences", controller, "preferences", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-zoom-in", controller, "zoom-in", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-zoom-out", controller, "zoom-out", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-select-all", controller, "select-all", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-tab-overview-toggle", controller, "tab-overview-toggle", SettingsBindFlags.DEFAULT);
            settings.bind ("shortcut-tab-overview-open", controller, "tab-overview-open", SettingsBindFlags.DEFAULT);
        }
    }
}