import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/features/player/session/player_engine_resolution.dart';
import 'package:smplayer/providers/player_engine_override_provider.dart';

void main() {
  group('resolvePlayerEngineKind - media3 override', () {
    test('Android + 非 DV/ProRes → media3', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.media3,
          isDolbyVision: false,
          videoCodec: 'h264',
          isAndroid: true,
        ),
        PlayerEngineKind.media3,
      );
    });

    test('桌面 → mpv（不论 DV / codec）', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.media3,
          isDolbyVision: false,
          videoCodec: 'h264',
          isAndroid: false,
        ),
        PlayerEngineKind.mpv,
      );
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.media3,
          isDolbyVision: true,
          videoCodec: 'prores',
          isAndroid: false,
        ),
        PlayerEngineKind.mpv,
      );
    });

    test('Android + DV → media3（用户显式选 media3 时直通 DV）', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.media3,
          isDolbyVision: true,
          videoCodec: 'hevc',
          isAndroid: true,
        ),
        PlayerEngineKind.media3,
      );
    });

    test('Android + ProRes → fvp（MediaCodec 不解 ProRes）', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.media3,
          isDolbyVision: false,
          videoCodec: 'prores',
          isAndroid: true,
        ),
        PlayerEngineKind.fvp,
      );
    });
  });

  group('resolvePlayerEngineKind - auto behavior', () {
    test('auto + Android + 非 DV/ProRes → media3', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.auto,
          isDolbyVision: false,
          videoCodec: 'h264',
          isAndroid: true,
        ),
        PlayerEngineKind.media3,
      );
    });

    test('auto + Android + DV → media3', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.auto,
          isDolbyVision: true,
          videoCodec: 'hevc',
          isAndroid: true,
        ),
        PlayerEngineKind.media3,
      );
    });

    test('auto + Android + ProRes → fvp', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.auto,
          isDolbyVision: false,
          videoCodec: 'prores',
          isAndroid: true,
        ),
        PlayerEngineKind.fvp,
      );
    });

    test('auto + 桌面 → mpv', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.auto,
          isDolbyVision: false,
          videoCodec: 'h264',
          isAndroid: false,
        ),
        PlayerEngineKind.mpv,
      );
    });

    test('auto + 桌面 DV → fvp', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.auto,
          isDolbyVision: true,
          videoCodec: 'hevc',
          isAndroid: false,
        ),
        PlayerEngineKind.fvp,
      );
    });

    test('fvp override → 总是 fvp', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.fvp,
          isDolbyVision: false,
          videoCodec: 'h264',
          isAndroid: false,
        ),
        PlayerEngineKind.fvp,
      );
    });

    test('mpv override → 总是 mpv（即便 DV）', () {
      expect(
        resolvePlayerEngineKind(
          PlayerEngineOverride.mpv,
          isDolbyVision: true,
          videoCodec: 'hevc',
          isAndroid: true,
        ),
        PlayerEngineKind.mpv,
      );
    });
  });

  group('playerEngineKindFromDisplayName', () {
    test('media3 → PlayerEngineKind.media3', () {
      expect(
        playerEngineKindFromDisplayName('media3'),
        PlayerEngineKind.media3,
      );
    });

    test('fvp / mpv 仍可反查', () {
      expect(playerEngineKindFromDisplayName('fvp'), PlayerEngineKind.fvp);
      expect(playerEngineKindFromDisplayName('mpv'), PlayerEngineKind.mpv);
    });

    test('未知 displayName → null', () {
      expect(playerEngineKindFromDisplayName('unknown'), isNull);
    });
  });

  group('nextAutoFallbackEngine', () {
    test('auto + media3 失败（非 DV）→ mpv', () {
      expect(
        nextAutoFallbackEngine(
          override: PlayerEngineOverride.auto,
          failedEngineKind: PlayerEngineKind.media3,
          isDolbyVision: false,
        ),
        PlayerEngineKind.mpv,
      );
    });

    test('auto + media3 失败（DV）→ 跳过 mpv 直达 fvp', () {
      expect(
        nextAutoFallbackEngine(
          override: PlayerEngineOverride.auto,
          failedEngineKind: PlayerEngineKind.media3,
          isDolbyVision: true,
        ),
        PlayerEngineKind.fvp,
      );
    });

    test('auto + mpv 失败 → fvp', () {
      expect(
        nextAutoFallbackEngine(
          override: PlayerEngineOverride.auto,
          failedEngineKind: PlayerEngineKind.mpv,
          isDolbyVision: false,
        ),
        PlayerEngineKind.fvp,
      );
    });

    test('auto + fvp 失败 → 无回退', () {
      expect(
        nextAutoFallbackEngine(
          override: PlayerEngineOverride.auto,
          failedEngineKind: PlayerEngineKind.fvp,
          isDolbyVision: true,
        ),
        isNull,
      );
    });

    test('非 auto 不自动回退', () {
      expect(
        nextAutoFallbackEngine(
          override: PlayerEngineOverride.media3,
          failedEngineKind: PlayerEngineKind.media3,
          isDolbyVision: true,
        ),
        isNull,
      );
    });

    test('未知失败引擎不自动回退', () {
      expect(
        nextAutoFallbackEngine(
          override: PlayerEngineOverride.auto,
          failedEngineKind: null,
          isDolbyVision: false,
        ),
        isNull,
      );
    });
  });
}
