using Adw;
using Gtk;

public class PreferencesWindow : Adw.PreferencesDialog {
    private Adw.EntryRow server_url_row;
    private Adw.EntryRow username_row;
    private Adw.PasswordEntryRow password_row;
    private Settings settings;

    public PreferencesWindow (Gtk.Window parent) {
        Object (
            title: "Settings"
        );

        add_css_class ("settings-dialog");

        var page = new PreferencesPage ();
        page.margin_top = 14;
        page.margin_bottom = 14;
        page.margin_start = 50;
        page.margin_end = 50;
        add (page);

        var connection_group = new PreferencesGroup ();
        connection_group.title = "Connection Settings";
        page.add (connection_group);
        
        server_url_row = new Adw.EntryRow ();
        server_url_row.title = "Server URL";
        connection_group.add (server_url_row);

        username_row = new Adw.EntryRow ();
        username_row.title = "Username";
        connection_group.add (username_row);

        password_row = new Adw.PasswordEntryRow ();
        password_row.title = "Password";
        connection_group.add (password_row);

        settings = new Settings();
        settings.settings_ready.connect(load_settings);
        load_settings();

        unowned var self = this;
        closed.connect(() => {
            self.save_settings();
        });
    }
    
    private void load_settings() {
        try {
            username_row.text = settings.get_username();
            server_url_row.text = settings.get_server_url();
            password_row.text = settings.get_password();
        } catch (Error e) {
            warning("Failed to load settings: %s", e.message);
        }
    }

    private void save_settings() {
        try {
            settings.set_server_url(server_url_row.text);
            settings.set_username(username_row.text);
            settings.set_password(password_row.text);
        } catch (Error e) {
            warning("Failed to save settings: %s", e.message);
        }
    }
} 