use std::sync::Arc;

use bgm::BgmManager;
use rodio::{
  dynamic_mixer::{mixer, DynamicMixerController}, OutputStream, Sink
};

use crate::utils::soon::Soon;

pub mod bgm;

pub struct Manager {
  bgm_manager: Soon<BgmManager>,
  _output_stream: OutputStream,
  _sound_sink: Sink,
  _sound_controller: Arc<DynamicMixerController<i16>>,
}

impl Manager {
  pub fn new() -> Self {
    let (output_stream, stream_handle) = OutputStream::try_default().unwrap();
    let sound_sink = Sink::try_new(&stream_handle).unwrap();
    let music_sink = Sink::try_new(&stream_handle).unwrap();
    let (sound_controller, mixer) = mixer::<i16>(2, 44100);
    sound_sink.append(mixer);

    Self {
      bgm_manager: Soon::new(BgmManager::new(music_sink)),
      _output_stream: output_stream,
      _sound_sink: sound_sink,
      _sound_controller: sound_controller,
    }
  }

  pub fn is_bgm_manager_loaded(&mut self) -> bool {
    self.bgm_manager.is_done()
  }

  pub fn get_bgm_manager(&mut self) -> &mut BgmManager {
    self.bgm_manager.try_borrow_mut().unwrap()
  }
}
