use godot::{classes::Engine, prelude::*};
use godot_tokio::AsyncRuntime;

mod player;
mod portals;
//mod screencapture;

struct Extension;

#[gdextension]
unsafe impl ExtensionLibrary for Extension {
    fn on_level_init(level: InitLevel) {
        if level == InitLevel::Scene {
            let mut engine = Engine::singleton();

            // This is where we register our async runtime singleton.
            engine.register_singleton(AsyncRuntime::SINGLETON, &AsyncRuntime::new_alloc());
        }
    }
    fn on_level_deinit(level: InitLevel) {
        if level == InitLevel::Scene {
            let mut engine = Engine::singleton();

            // Here is where we free our async runtime singleton from memory.
            if let Some(async_singleton) = engine.get_singleton(AsyncRuntime::SINGLETON) {
                engine.unregister_singleton(AsyncRuntime::SINGLETON);
                async_singleton.free();
            } else {
                godot_warn!(
                    "Failed to find & free singleton -> {}",
                    AsyncRuntime::SINGLETON
                );
            }
        }
    }
}
