use crate::app::PatchpalApplication;
use gtk::glib;
use gtk::prelude::*;
use std::str;

pub(crate) async fn agreement(application: PatchpalApplication) -> Option<uuid::Uuid> {
    if let Some(id) = application.config().get_telemetry_id() {
        return if id.is_nil() {
            #[cfg(feature = "debug_telemetry")]
            println!("telemetry: opted out");
            None
        } else {
            Some(id)
        };
    }
    if application.config().get_local::<bool>("ignore-telemetry").is_some() {
        #[cfg(feature = "debug_telemetry")]
        println!("telemetry: ignored until next time");
        return None;
    }
    let telemetry_text = gtk::gio::resources_lookup_data(
        "/com/redhat/patchpal-gui/texts/telemetry-info.txt",
        gtk::gio::ResourceLookupFlags::NONE,
    ).unwrap();
    let label = gtk::Label::builder()
        .label(str::from_utf8(&telemetry_text).unwrap())
        .wrap(true)
        .use_markup(true)
        .build();
    let button_agree = gtk::Button::builder()
        .label("Agree")
        .build();
    let button_disagree = gtk::Button::builder()
        .label("Disagree")
        .build();
    let button_later = gtk::Button::builder()
        .label("Ask me next time")
        .build();
    let hbox = gtk::Box::builder()
        .orientation(gtk::Orientation::Horizontal)
        .spacing(10)
        .build();
    hbox.append(&button_agree);
    hbox.append(&button_disagree);
    hbox.append(&button_later);
    let vbox = gtk::Box::builder()
        .margin_bottom(10)
        .margin_end(10)
        .margin_start(10)
        .margin_top(10)
        .orientation(gtk::Orientation::Vertical)
        .spacing(10)
        .build();
    vbox.append(&label);
    vbox.append(&hbox);
    let window = gtk::Window::builder()
        .child(&vbox)
        .default_widget(&button_agree)
        .modal(true)
        .title("Telemetry agreement")
        .transient_for(application.app_window())
        .application(&application)
        .build();

    let (sender, receiver) = smol::channel::bounded(1);
    button_agree.connect_clicked(glib::clone!(
        #[weak] application,
        #[weak] window,
        #[strong] sender,
        move |_| {
            let id = uuid::Uuid::new_v4();
            let config = application.config();
            config.set_telemetry_id(id);
            let _ = config.save();
            window.close();
            let _ = sender.force_send(Some(id));
        }
    ));
    button_disagree.connect_clicked(glib::clone!(
        #[weak] application,
        #[weak] window,
        #[strong] sender,
        move |_| {
            let id = uuid::Uuid::nil();
            let config = application.config();
            config.set_telemetry_id(id);
            let _ = config.save();
            window.close();
            let _ = sender.force_send(None);
        }
    ));
    button_later.connect_clicked(glib::clone!(
        #[weak] application,
        #[weak] window,
        #[strong] sender,
        move |_| {
            application.config().set_local("ignore-telemetry", true.to_string());
            window.close();
            let _ = sender.force_send(None);
        }
    ));

    window.present();
    receiver.recv().await.unwrap_or(None)
}
