// Device-independent guards for two more live-path behaviors: the highlighter
// tool produces a highlighter-kind stroke, and a single finger PANS the shared
// transform when finger-drawing is off (touch → pan, not draw).

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:badnote/editor/canvas/pen_canvas.dart';
import 'package:badnote/editor/canvas/pen_stroke.dart';

void main() {
  const pageSize = Size(400, 600);

  Widget host({
    required void Function(PenStroke) onComplete,
    required TransformationController controller,
    CanvasTool tool = CanvasTool.pen,
    bool allowFinger = false,
  }) =>
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: SizedBox(
              width: pageSize.width,
              height: pageSize.height,
              child: PenCanvas(
                pageWidget: Container(color: const Color(0xFFEEEEEE)),
                pageSize: pageSize,
                strokes: const <PenStroke>[],
                transformationController: controller,
                tool: tool,
                color: const Color(0xFFFF0000),
                strokeWidth: 0.02,
                allowFingerDrawing: allowFinger,
                onStrokeComplete: onComplete,
                onEraseStroke: (_, __) {},
              ),
            ),
          ),
        ),
      );

  testWidgets('highlighter tool commits a highlighter-kind stroke',
      (tester) async {
    final committed = <PenStroke>[];
    final controller = TransformationController();
    addTearDown(controller.dispose);

    await tester.pumpWidget(host(
      onComplete: committed.add,
      controller: controller,
      tool: CanvasTool.highlighter,
    ));

    final center = tester.getCenter(find.byType(PenCanvas));
    final g = await tester.startGesture(center, kind: PointerDeviceKind.stylus);
    await g.moveBy(const Offset(30, 0));
    await g.up();
    await tester.pump();

    expect(committed, hasLength(1));
    expect(committed.single.kind, PenStrokeKind.highlighter);
  });

  testWidgets('a single finger PANS the transform when finger-drawing is off',
      (tester) async {
    final committed = <PenStroke>[];
    final controller = TransformationController();
    addTearDown(controller.dispose);

    await tester.pumpWidget(host(
      onComplete: committed.add,
      controller: controller,
      allowFinger: false,
    ));

    final before = controller.value.getTranslation();
    final center = tester.getCenter(find.byType(PenCanvas));
    final g = await tester.startGesture(center, kind: PointerDeviceKind.touch);
    await g.moveBy(const Offset(40, 25));
    await g.moveBy(const Offset(20, 15));
    await g.up();
    await tester.pump();

    final after = controller.value.getTranslation();
    // It panned (translation changed) and did NOT draw.
    expect((after.x - before.x).abs() + (after.y - before.y).abs(),
        greaterThan(1.0));
    expect(committed, isEmpty);
  });
}
