use glib::Sender;
use gtk::prelude::*;
use std::{
    env,
    sync::{Arc, RwLock},
};

mod app_window;
use app_window::ApplicationWindow;

mod config;

mod playlist_card;
use playlist_card::PlaylistCard;

mod types;
use types::PlaybackState;

#[derive(Clone, Debug)]
pub enum Message {}

#[derive(Clone)]
pub struct Core {
    tx: Arc<RwLock<Option<Sender<Message>>>>,
}

pub fn main() {
    gio::resources_register_include!("com.luminescent-dreams.gm-control-panel.gresource")
        .expect("Failed to register resource");

    let app = adw::Application::builder()
        .application_id("com.luminescent-dreams.gm-control-panel")
        .build();

    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();

    let core = Core {
        tx: Arc::new(RwLock::new(None)),
    };

    app.connect_activate(move |app| {
        let (gtk_tx, gtk_rx) =
            gtk::glib::MainContext::channel::<Message>(gtk::glib::Priority::DEFAULT);

        *core.tx.write().unwrap() = Some(gtk_tx);

        let window = ApplicationWindow::new(app);
        window.window.present();

        gtk_rx.attach(None, move |_msg| glib::ControlFlow::Continue);
    });

    let args: Vec<String> = env::args().collect();
    ApplicationExtManual::run_with_args(&app, &args);
    runtime.shutdown_background();
}
