use archetype_asset::cache::AssetCache;
use archetype_asset::gpu::mock::MockGpu;
use archetype_asset::runtime::TokioSpawner;
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    println!("Async Loading Showcase");

    let gpu = MockGpu::new();
    let spawner = TokioSpawner::new();
    let cache = AssetCache::new(gpu, spawner, 100 * 1024 * 1024);

    let assets = vec![
        "hero_character.glb",
        "enemy_grunt.glb",
        "weapon_sword.glb",
        "environment_tree.glb",
    ];

    println!("Starting batch load of {} assets...", assets.len());
    let start = std::time::Instant::now();

    // Spawn concurrent load tasks
    let mut handles = vec![];
    for path in assets {
        let c = cache.clone();
        handles.push(tokio::spawn(async move {
            println!("Requesting: {}", path);
            // This will "fail" to find file in Mock environment but simulates the async flow
            let _ = c.get_or_load_model(path);
            println!("Finished request: {}", path);
        }));
    }

    // Do "work" while loading
    for i in 0..5 {
        println!("Main thread working... {}", i);
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    // Await all
    for h in handles {
        let _ = h.await;
    }

    println!("All assets processed in {:?}", start.elapsed());
    Ok(())
}
