/* select_remote_dialog.rs
 *
 * Copyright 2026 Christian Kruse
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

use adw::prelude::*;
use gettextrs::gettext;
use relm4::prelude::*;

pub struct SelectRemoteDialogInit {
    pub tag_name: String,
    pub remotes: Vec<String>,
    pub parent_window: Option<gtk::Window>,
}

#[derive(Debug)]
pub enum SelectRemoteDialogOutput {
    Confirmed(String),
    Cancelled,
}

pub struct SelectRemoteDialog {
    remote_model: gtk::StringList,
}

#[relm4::component(pub)]
impl Component for SelectRemoteDialog {
    type Init = SelectRemoteDialogInit;
    type Input = ();
    type Output = SelectRemoteDialogOutput;
    type CommandOutput = ();

    view! {
        #[root]
        adw::PreferencesGroup {
            #[name = "remote_row"]
            adw::ComboRow {
                set_title: &gettext("Remote"),
                set_model: Some(&model.remote_model),
                set_selected: 0,
            },
        }
    }

    fn init(init: Self::Init, root: Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
        let remote_model = gtk::StringList::new(&[]);
        for remote in &init.remotes {
            remote_model.append(remote);
        }

        let model = SelectRemoteDialog { remote_model };
        let widgets = view_output!();

        let dialog = adw::AlertDialog::builder()
            .heading(gettext("Push Tag “{}”").replace("{}", &init.tag_name))
            .build();

        dialog.set_extra_child(Some(&root));

        dialog.add_response("cancel", &gettext("Cancel"));
        dialog.add_response("confirm", &gettext("Push"));
        dialog.set_response_appearance("confirm", adw::ResponseAppearance::Suggested);
        dialog.set_default_response(Some("confirm"));
        dialog.set_close_response("cancel");

        let remote_model_ref = model.remote_model.clone();
        let remote_row = widgets.remote_row.clone();

        dialog.connect_response(None, move |_dialog, response| {
            if response == "confirm" {
                let remote = remote_model_ref
                    .string(remote_row.selected())
                    .map(|s| s.to_string())
                    .unwrap_or_default();

                if !remote.is_empty() {
                    sender.output(SelectRemoteDialogOutput::Confirmed(remote)).ok();
                }
            } else {
                sender.output(SelectRemoteDialogOutput::Cancelled).ok();
            }
        });

        dialog.present(init.parent_window.as_ref());

        ComponentParts { model, widgets }
    }
}
