import 'dart:typed_data';

import 'package:test/test.dart';
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
import 'package:wolf_3d_dart/wolf_3d_input.dart';

void main() {
  group('Map overlay toggle', () {
    test('toggles in gameplay on map toggle pulse', () {
      final input = _PulseInput();
      final engine = _buildEngine(input: input, difficulty: Difficulty.medium);

      engine.init();
      expect(engine.isMapOverlayVisible, isFalse);

      input.queueMapToggle = true;
      engine.tick(const Duration(milliseconds: 16));
      expect(engine.isMapOverlayVisible, isTrue);

      input.queueMapToggle = true;
      engine.tick(const Duration(milliseconds: 16));
      expect(engine.isMapOverlayVisible, isFalse);
    });

    test('does not toggle while menu is open', () {
      final input = _PulseInput();
      final engine = _buildEngine(input: input, difficulty: null);

      engine.init();
      expect(engine.isMenuOpen, isTrue);
      expect(engine.isMapOverlayVisible, isFalse);

      input.queueMapToggle = true;
      engine.tick(const Duration(milliseconds: 16));
      expect(engine.isMapOverlayVisible, isFalse);
    });

    test('clears map overlay when pause menu opens', () {
      final input = _PulseInput();
      final engine = _buildEngine(input: input, difficulty: Difficulty.medium);

      engine.init();

      input.queueMapToggle = true;
      engine.tick(const Duration(milliseconds: 16));
      expect(engine.isMapOverlayVisible, isTrue);

      input.queueBack = true;
      engine.tick(const Duration(milliseconds: 16));
      expect(engine.isMenuOpen, isTrue);
      expect(engine.isMapOverlayVisible, isFalse);
    });
  });
}

class _PulseInput extends Wolf3dInput {
  bool queueMapToggle = false;
  bool queueBack = false;

  @override
  void update() {
    isMapToggle = queueMapToggle;
    isBack = queueBack;

    queueMapToggle = false;
    queueBack = false;

    isMovingForward = false;
    isMovingBackward = false;
    isTurningLeft = false;
    isTurningRight = false;
    isInteracting = false;
    isFiring = false;
    requestedWeapon = null;
    menuTapX = null;
    menuTapY = null;
  }
}

WolfEngine _buildEngine({
  required Wolf3dInput input,
  required Difficulty? difficulty,
}) {
  final wallGrid = _buildGrid();
  final objectGrid = _buildGrid();
  _fillBoundaries(wallGrid, 2);
  objectGrid[2][2] = MapObject.playerEast;

  return WolfEngine(
    data: WolfensteinData(
      version: GameVersion.shareware,
      dataVersion: DataVersion.unknown,
      registry: RetailAssetRegistry(),
      walls: [
        _solidSprite(1),
        _solidSprite(1),
        _solidSprite(2),
        _solidSprite(2),
      ],
      sprites: List.generate(436, (_) => _solidSprite(255)),
      sounds: const [],
      adLibSounds: const [],
      music: const [],
      vgaImages: const [],
      episodes: [
        Episode(
          name: 'Episode 1',
          levels: [
            WolfLevel(
              name: 'Level 1',
              wallGrid: wallGrid,
              areaGrid: List.generate(64, (_) => List.filled(64, -1)),
              objectGrid: objectGrid,
              music: Music.level01,
            ),
          ],
        ),
      ],
    ),
    difficulty: difficulty,
    startingEpisode: 0,
    frameBuffer: FrameBuffer(64, 64),
    input: input,
    onGameWon: () {},
  );
}

SpriteMap _buildGrid() => List.generate(64, (_) => List.filled(64, 0));

void _fillBoundaries(SpriteMap grid, int wallId) {
  for (int i = 0; i < 64; i++) {
    grid[0][i] = wallId;
    grid[63][i] = wallId;
    grid[i][0] = wallId;
    grid[i][63] = wallId;
  }
}

Sprite _solidSprite(int colorIndex) {
  return Sprite(Uint8List.fromList(List.filled(64 * 64, colorIndex)));
}
