//! Startup helpers to apply configuration defaults to the running app.
//!
//! Functions here translate `config` values into runtime settings and
//! initialize audio thread state accordingly.

use crate::app::App;
use crate::audio::{AudioCmd, AudioPlayer};

/// Apply playback-related defaults from `settings` to the `app` and
/// initialize the `audio_player` accordingly.
pub fn apply_playback_defaults(app: &mut App, audio_player: &AudioPlayer) -> Option<Vec<usize>> {
    // Initialize playback defaults in the audio thread.
    let mut pending_shuffle_reselect_from: Option<Vec<usize>> = None;
    if app.shuffle {
        pending_shuffle_reselect_from = app
            .order_handle
            .as_ref()
            .and_then(|h| h.lock().ok().map(|v| v.clone()))
            .or_else(|| Some((0..app.tracks.len()).collect()));
        let _ = audio_player.send(AudioCmd::ToggleShuffle);
    }

    let _ = audio_player.send(AudioCmd::SetLoopMode(app.loop_mode));
    let _ = audio_player.send(AudioCmd::SetQueue(app.display_indices()));
    app.clear_queue_dirty();

    pending_shuffle_reselect_from
}
