import 'package:wolf_3d_dart/src/data/data_version.dart';
import 'package:wolf_3d_dart/src/data_types/game_version.dart';
import 'package:wolf_3d_dart/src/registry/asset_registry.dart';
import 'package:wolf_3d_dart/src/registry/built_in/retail_asset_registry.dart';
import 'package:wolf_3d_dart/src/registry/built_in/shareware_asset_registry.dart';

/// The input used by [AssetRegistryResolver] to select or build a registry.
class RegistrySelectionContext {
  const RegistrySelectionContext({
    required this.gameVersion,
    required this.dataVersion,
  });

  /// Broad release family (Retail, Shareware, Spear of Destiny, …).
  final GameVersion gameVersion;

  /// Exact data-file identity determined from the VSWAP checksum.
  final DataVersion dataVersion;
}

/// Selects or builds an [AssetRegistry] for a given [RegistrySelectionContext].
///
/// Implement this interface to provide a fully custom selection strategy,
/// for example to support a game variant not covered by the built-in resolver.
abstract class AssetRegistryResolver {
  const AssetRegistryResolver();

  AssetRegistry resolve(RegistrySelectionContext context);
}

/// Default resolver that maps known [DataVersion] and [GameVersion] values to
/// built-in registries.
///
/// Selection precedence:
/// 1. Exact [DataVersion] match (most specific).
/// 2. [GameVersion] family fallback.
/// 3. Retail layout as the last-resort fallback for unknown variants.
class BuiltInAssetRegistryResolver implements AssetRegistryResolver {
  const BuiltInAssetRegistryResolver();

  @override
  AssetRegistry resolve(RegistrySelectionContext context) {
    // 1. Exact data-version match.
    switch (context.dataVersion) {
      case DataVersion.version10Retail:
      case DataVersion.version11Retail:
      case DataVersion.version14Retail:
        return RetailAssetRegistry();
      case DataVersion.version14Shareware:
      case DataVersion.spearOfDestinyShareware:
        return SharewareAssetRegistry(strictOriginalShareware: true);
      case _:
        break; // fall through to GameVersion family check
    }

    // 2. GameVersion family fallback.
    switch (context.gameVersion) {
      case GameVersion.retail:
        return RetailAssetRegistry();
      case GameVersion.shareware:
        return SharewareAssetRegistry();
      case GameVersion.spearOfDestiny:
      case GameVersion.spearOfDestinyDemo:
        // Spear-specific modules are not yet implemented; retail layout is
        // the closest structural match and will be replaced in a later phase.
        return RetailAssetRegistry();
    }
  }
}
