/* stash_switch_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 StashSwitchDialogInit {
    pub heading: String,
    pub body: String,
    pub proceed_label: String,
    pub allow_skip_stash: bool,
    pub has_tracked_changes: bool,
    pub has_untracked: bool,
    pub parent_window: Option<gtk::Window>,
}

#[derive(Debug)]
pub enum StashSwitchDialogOutput {
    ProceedWithoutStash,
    StashTrackedOnly,
    StashWithUntracked,
    Cancelled,
}

pub struct StashSwitchDialog;

#[relm4::component(pub)]
impl Component for StashSwitchDialog {
    type Init = StashSwitchDialogInit;
    type Input = ();
    type Output = StashSwitchDialogOutput;
    type CommandOutput = ();

    view! {
        #[root]
        gtk::Box {}
    }

    fn init(init: Self::Init, root: Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
        let dialog = adw::AlertDialog::builder()
            .heading(&init.heading)
            .body(&init.body)
            .build();

        dialog.add_response("cancel", &gettext("Cancel"));

        if init.allow_skip_stash {
            dialog.add_response("proceed", &init.proceed_label);
        }

        if init.has_tracked_changes {
            dialog.add_response("stash", &gettext("Stash and Continue"));
        }

        if init.has_untracked {
            dialog.add_response("stash-all", &gettext("Stash Including Untracked"));
        }

        let default_response = if init.has_tracked_changes {
            "stash"
        } else if init.has_untracked {
            "stash-all"
        } else if init.allow_skip_stash {
            "proceed"
        } else {
            "cancel"
        };

        if default_response != "cancel" {
            dialog.set_response_appearance(default_response, adw::ResponseAppearance::Suggested);
        }

        dialog.set_default_response(Some("cancel"));
        dialog.set_close_response("cancel");

        dialog.connect_response(None, move |_dialog, response| {
            let output = match response {
                "proceed" => StashSwitchDialogOutput::ProceedWithoutStash,
                "stash" => StashSwitchDialogOutput::StashTrackedOnly,
                "stash-all" => StashSwitchDialogOutput::StashWithUntracked,
                _ => StashSwitchDialogOutput::Cancelled,
            };

            sender.output(output).ok();
        });

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

        let widgets = view_output!();

        ComponentParts {
            model: StashSwitchDialog,
            widgets,
        }
    }
}
