use std::sync::Mutex;

use demand::{Confirm, Dialog, DialogButton};

use crate::env;
use crate::ui::ctrlc;
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::theme::get_theme;

static MUTEX: Mutex<()> = Mutex::new(());

static SKIP_PROMPT: Mutex<bool> = Mutex::new(false);

pub fn confirm<S: Into<String>>(message: S) -> eyre::Result<bool> {
    confirm_with_default(message, true)
}

pub fn confirm_with_default<S: Into<String>>(message: S, default_yes: bool) -> eyre::Result<bool> {
    let _lock = MUTEX.lock().unwrap(); // Prevent multiple prompts at once
    ctrlc::show_cursor_after_ctrl_c();

    if !console::user_attended_stderr() || env::__USAGE.is_some() {
        return Ok(false);
    }
    let _progress_pause = MultiProgressReport::try_get().map(|report| report.pause_progress());
    let theme = get_theme();
    let result = Confirm::new(message)
        .selected(default_yes)
        .theme(&theme)
        .run()?;
    Ok(result)
}

pub fn confirm_with_all<S: Into<String>>(message: S) -> eyre::Result<bool> {
    let _lock = MUTEX.lock().unwrap(); // Prevent multiple prompts at once
    ctrlc::show_cursor_after_ctrl_c();

    if !console::user_attended_stderr() || env::__USAGE.is_some() {
        return Ok(false);
    }

    let mut skip_prompt = SKIP_PROMPT.lock().unwrap();
    if *skip_prompt {
        return Ok(true);
    }

    let _progress_pause = MultiProgressReport::try_get().map(|report| report.pause_progress());
    let theme = get_theme();
    let answer = Dialog::new(message)
        .buttons(vec![
            DialogButton::new("Yes"),
            DialogButton::new("No"),
            DialogButton::new("All"),
        ])
        .selected_button(1)
        .theme(&theme)
        .run()?;

    let result = match answer.as_str() {
        "Yes" => true,
        "No" => false,
        "All" => {
            *skip_prompt = true;
            true
        }
        _ => false,
    };
    Ok(result)
}
