import 'package:test/test.dart';
import 'package:wolf_3d_dart/src/registry/built_in/shareware_menu_module.dart';
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';

void main() {
  group('SharewareMenuPicModule', () {
    test('uses fixed canonical WL1 mapping when strict mode is enabled', () {
      final module = SharewareMenuPicModule(useOriginalWl1Map: true);

      expect(module.resolve(MenuPicKey.title)?.pictureIndex, 96);
      expect(module.resolve(MenuPicKey.pg13)?.pictureIndex, 97);
      expect(module.resolve(MenuPicKey.credits)?.pictureIndex, 98);
      expect(module.resolve(MenuPicKey.cursorActive)?.pictureIndex, 20);
      expect(module.resolve(MenuPicKey.footer)?.pictureIndex, 27);
      expect(module.resolve(MenuPicKey.difficultyEasy)?.pictureIndex, 29);
      expect(module.resolve(MenuPicKey.episode1)?.pictureIndex, 39);
    });

    test('returns null for unavailable WL1 episode art in strict mode', () {
      final module = SharewareMenuPicModule(useOriginalWl1Map: true);

      expect(module.resolve(MenuPicKey.episode2), isNull);
      expect(module.resolve(MenuPicKey.episode3), isNull);
      expect(module.resolve(MenuPicKey.episode4), isNull);
      expect(module.resolve(MenuPicKey.episode5), isNull);
      expect(module.resolve(MenuPicKey.episode6), isNull);
    });

    test('heuristic mode only shifts statusbar-era indices', () {
      final module = SharewareMenuPicModule();
      final imageSizes = List.generate(
        90,
        (_) => (width: 64, height: 64),
      );

      imageSizes[35] = (width: 320, height: 40); // STATUSBARPIC-like
      imageSizes[36] = (width: 320, height: 200); // TITLEPIC-like
      imageSizes[37] = (width: 320, height: 200); // PG13PIC-like
      imageSizes[38] = (width: 320, height: 200); // CREDITSPIC-like

      module.initWithImageSizes(imageSizes);

      // 84 shifted by -48.
      expect(module.resolve(MenuPicKey.title)?.pictureIndex, 36);
      // Control-panel assets should remain unshifted.
      expect(module.resolve(MenuPicKey.difficultyEasy)?.pictureIndex, 17);
      expect(module.resolve(MenuPicKey.episode1)?.pictureIndex, 27);
    });
  });

  group('BuiltInAssetRegistryResolver shareware selection', () {
    test(
      'uses strict canonical WL1 map for exact known shareware identity',
      () {
        const resolver = BuiltInAssetRegistryResolver();

        final registry = resolver.resolve(
          const RegistrySelectionContext(
            gameVersion: GameVersion.shareware,
            dataVersion: DataVersion.version14Shareware,
          ),
        );

        expect(registry.menu.resolve(MenuPicKey.title)?.pictureIndex, 96);
        expect(
          registry.menu.resolve(MenuPicKey.cursorActive)?.pictureIndex,
          20,
        );
        expect(registry.menu.resolve(MenuPicKey.episode2), isNull);
      },
    );

    test('uses flexible shareware fallback for unknown identities', () {
      const resolver = BuiltInAssetRegistryResolver();

      final registry = resolver.resolve(
        const RegistrySelectionContext(
          gameVersion: GameVersion.shareware,
          dataVersion: DataVersion.unknown,
        ),
      );

      // Unknown shareware falls back to baseline until image-size heuristic init.
      expect(registry.menu.resolve(MenuPicKey.title)?.pictureIndex, 84);
      expect(registry.menu.resolve(MenuPicKey.episode2)?.pictureIndex, 28);
    });
  });
}
