import 'package:block_seasons/game/models/piece_library.dart';
import 'package:block_seasons/ui/widgets/board_geometry.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  const geo = BoardGeometry(boardSize: 320); // cell = 40

  group('cellAt', () {
    test('maps points inside cells', () {
      expect(geo.cellAt(const Offset(5, 5)), (0, 0));
      expect(geo.cellAt(const Offset(60, 100)), (1, 2));
      expect(geo.cellAt(const Offset(319, 319)), (7, 7));
    });

    test('returns null outside the board', () {
      expect(geo.cellAt(const Offset(-1, 10)), isNull);
      expect(geo.cellAt(const Offset(10, 321)), isNull);
    });
  });

  group('cellRect', () {
    test('returns the drawn rect for a cell', () {
      final rect = geo.cellRect(2, 3);
      expect(rect.left, 80);
      expect(rect.top, 120);
      expect(rect.width, 40);
      expect(rect.height, 40);
    });
  });

  group('snapAnchor', () {
    final mono = PieceLibrary.byId('mono');
    final line5h = PieceLibrary.byId('line5_h');

    test('rounds to the nearest cell', () {
      expect(geo.snapAnchor(mono, const Offset(78, 122)), (2, 3));
      expect(geo.snapAnchor(mono, const Offset(99, 99)), (2, 2));
    });

    test('clamps so the piece bounding box stays on the board', () {
      // line5_h is 5 wide: anchor x can be at most 3.
      expect(geo.snapAnchor(line5h, const Offset(310, 0)), (3, 0));
      expect(geo.snapAnchor(line5h, const Offset(-50, -50)), (0, 0));
    });
  });
}
