import 'dart:io' show Platform;

import '../../../providers/player_engine_override_provider.dart';


enum PlayerEngineKind { fvp, mpv, media3 }

PlayerEngineKind? playerEngineKindFromDisplayName(String displayName) {
  return switch (displayName) {
    'fvp' => PlayerEngineKind.fvp,
    'mpv' => PlayerEngineKind.mpv,
    'media3' => PlayerEngineKind.media3,
    _ => null,
  };
}


PlayerEngineKind? nextAutoFallbackEngine({
  required PlayerEngineOverride override,
  required PlayerEngineKind? failedEngineKind,
  required bool isDolbyVision,
}) {
  if (override != PlayerEngineOverride.auto) return null;
  return switch (failedEngineKind) {
    PlayerEngineKind.media3 =>
      isDolbyVision ? PlayerEngineKind.fvp : PlayerEngineKind.mpv,
    PlayerEngineKind.mpv => PlayerEngineKind.fvp,
    PlayerEngineKind.fvp || null => null,
  };
}


PlayerEngineKind resolvePlayerEngineKind(
  PlayerEngineOverride override, {
  required bool isDolbyVision,
  String? videoCodec,
  bool? isAndroid,
}) {
  final onAndroid = isAndroid ?? Platform.isAndroid;
  final codec = videoCodec?.toLowerCase();
  final isProRes = codec == 'prores';
  switch (override) {
    case PlayerEngineOverride.fvp:
      return PlayerEngineKind.fvp;
    case PlayerEngineOverride.mpv:
      return PlayerEngineKind.mpv;
    case PlayerEngineOverride.media3:
      if (!onAndroid) return PlayerEngineKind.mpv;
      if (isProRes) return PlayerEngineKind.fvp;
      return PlayerEngineKind.media3;
    case PlayerEngineOverride.auto:
      if (onAndroid) {
        if (isProRes) return PlayerEngineKind.fvp;
        return PlayerEngineKind.media3;
      }
      if (isDolbyVision || isProRes) return PlayerEngineKind.fvp;
      return PlayerEngineKind.mpv;
  }
}
