use super::apply_preview_profiles;
use crate::launcher::preview::{LauncherPreview, PreviewSurface};
use crate::launcher::state::{CaptureSizePreset, FeedSourcePreset, LauncherState};

#[test]
fn fresh_preview_bootstrap_is_overridden_by_launcher_state_profiles() {
    let preview = LauncherPreview::new("http://127.0.0.1:1".to_string()).unwrap();
    let state = LauncherState::default();

    let bootstrap = preview.profile_for_test(1, PreviewSurface::Inline).unwrap();
    assert_eq!(bootstrap.0, 0);
    assert_eq!(bootstrap.3, 1920);
    assert_eq!(bootstrap.4, 1080);
    assert_eq!(bootstrap.5, 60);
    assert_eq!(bootstrap.6, 18_000);

    apply_preview_profiles(&preview, &state);

    let inline = preview.profile_for_test(1, PreviewSurface::Inline).unwrap();
    assert_eq!(inline.0, 1);
    assert_eq!(inline.3, 1920);
    assert_eq!(inline.4, 1080);
    assert_eq!(inline.5, 60);
    assert_eq!(inline.6, 18_000);

    let window = preview.profile_for_test(1, PreviewSurface::Window).unwrap();
    assert_eq!(window.0, 1);
    assert_eq!(window.3, 1920);
    assert_eq!(window.4, 1080);
    assert_eq!(window.5, 60);
    assert_eq!(window.6, 18_000);

    preview.shutdown_all();
}

#[test]
fn source_preview_profile_stays_honest_after_apply() {
    let preview = LauncherPreview::new("http://127.0.0.1:1".to_string()).unwrap();
    let mut state = LauncherState::default();
    state.set_capture_size_preset(1, CaptureSizePreset::P1080);

    apply_preview_profiles(&preview, &state);

    let inline = preview.profile_for_test(1, PreviewSurface::Inline).unwrap();
    assert_eq!(inline.0, 1);
    assert_eq!(inline.3, 1920);
    assert_eq!(inline.4, 1080);
    assert_eq!(inline.5, 60);
    assert_eq!(inline.6, 18_000);

    preview.shutdown_all();
}

#[test]
fn mirrored_preview_profile_keeps_its_own_feed_id_but_uses_the_other_source() {
    let preview = LauncherPreview::new("http://127.0.0.1:1".to_string()).unwrap();
    let mut state = LauncherState::default();
    state.set_feed_source_preset(0, FeedSourcePreset::OtherEye);

    apply_preview_profiles(&preview, &state);

    let inline = preview.profile_for_test(0, PreviewSurface::Inline).unwrap();
    let window = preview.profile_for_test(0, PreviewSurface::Window).unwrap();
    assert_eq!(inline.0, 1);
    assert_eq!(window.0, 1);

    preview.shutdown_all();
}

#[test]
fn mirrored_preview_profile_inherits_the_source_eye_mode() {
    let preview = LauncherPreview::new("http://127.0.0.1:1".to_string()).unwrap();
    let mut state = LauncherState::default();
    state.set_feed_source_preset(0, FeedSourcePreset::OtherEye);
    state.set_capture_size_preset(1, CaptureSizePreset::P720);

    apply_preview_profiles(&preview, &state);

    let inline = preview.profile_for_test(0, PreviewSurface::Inline).unwrap();
    let window = preview.profile_for_test(0, PreviewSurface::Window).unwrap();
    assert_eq!(inline.0, 1);
    assert_eq!(window.0, 1);
    assert_eq!(inline.3, 1280);
    assert_eq!(inline.4, 720);
    assert_eq!(inline.5, 60);
    assert_eq!(inline.6, 12_000);
    assert_eq!(window.3, 1280);
    assert_eq!(window.4, 720);
    assert_eq!(window.5, 60);
    assert_eq!(window.6, 12_000);

    preview.shutdown_all();
}

#[test]
fn off_preview_profile_disables_both_surfaces_instead_of_leaving_idle_feeds_running() {
    let preview = LauncherPreview::new("http://127.0.0.1:1".to_string()).unwrap();
    let mut state = LauncherState::default();
    state.set_feed_source_preset(0, FeedSourcePreset::Off);

    apply_preview_profiles(&preview, &state);

    assert_eq!(
        preview.feed_disabled_for_test(0, PreviewSurface::Inline),
        Some(true)
    );
    assert_eq!(
        preview.feed_disabled_for_test(0, PreviewSurface::Window),
        Some(true)
    );
    assert_eq!(
        preview.feed_disabled_for_test(1, PreviewSurface::Inline),
        Some(false)
    );

    preview.shutdown_all();
}
