use game_audio::{
    SoundSystem,
    track::{SongPlayer, parse},
};
use std::{thread::sleep, time::Duration};

fn ramp_parameter(player: &mut SongPlayer, system: &SoundSystem, from: f32, to: f32, seconds: f32) {
    let steps = 48;
    for step in 0..=steps {
        let progress = step as f32 / steps as f32;
        player.set_parameter("combat", from + (to - from) * progress);
        let _ = player.update(system);
        sleep(Duration::from_secs_f32(seconds / steps as f32));
    }
}

fn main() {
    let song = match parse(include_str!("adaptive/adaptive.track")) {
        Ok(song) => song,
        Err(error) => {
            eprintln!("parse error: {error}");
            return;
        }
    };

    let system = SoundSystem::new();
    if !system.is_active() {
        eprintln!("no audio device; the fades still run, just silently");
    }

    let mut player = SongPlayer::new();
    if let Err(error) = player.play(&system, &song, None, 0.0) {
        eprintln!("play error: {error}");
        return;
    }

    let script = [
        ("exploring — drums and bass only", 0.0, 4.0),
        ("enemies near — lead fades in", 0.7, 2.0),
        ("combat — lead at full", 1.0, 4.0),
        ("combat over — lead fades out", 0.0, 3.0),
    ];

    let mut current = 0.0;
    for (label, intensity, seconds) in script {
        println!("{label}");
        ramp_parameter(&mut player, &system, current, intensity, seconds);
        current = intensity;
    }

    player.stop();
}
