// test/pen_brush_color_memory_test.dart
//
// Pins rnote-style per-brush color memory in the note editor: each brush
// remembers its OWN color, selecting a brush restores that brush's color (the
// PenCanvas receives it), and picking a color updates ONLY the active brush's
// entry — switching back to a different brush restores the other color.
//
// Drives the real PenNoteScreen toolbar (BrushPickerButton popup + color dots)
// and reads PenCanvas.color to assert the active drawing color.

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  Color canvasColor(WidgetTester tester) =>
      tester.widget<PenCanvas>(find.byType(PenCanvas)).color;

  /// Pick the brush named [label] from the BrushPickerButton popup menu.
  Future<void> selectBrush(WidgetTester tester, String label) async {
    // The brush picker carries the 'Brush' tooltip.
    await tester.tap(find.byTooltip('Brush'));
    await tester.pumpAndSettle();
    await tester.tap(find.text(label).last);
    await tester.pumpAndSettle();
  }

  /// Tap the toolbar color dot whose swatch is exactly [c]. The dot is an
  /// AnimatedContainer (the swatch) inside a GestureDetector; tap the gesture
  /// detector ancestor so the onTap fires.
  Future<void> tapColorDot(WidgetTester tester, Color c) async {
    final swatch = find.byWidgetPredicate((w) =>
        w is AnimatedContainer &&
        w.decoration is BoxDecoration &&
        (w.decoration as BoxDecoration).color == c &&
        (w.decoration as BoxDecoration).shape == BoxShape.circle);
    final gd = find.ancestor(of: swatch, matching: find.byType(GestureDetector));
    await tester.tap(gd.first);
    await tester.pump();
  }

  testWidgets('each brush remembers its own color; switching restores it',
      (tester) async {
    SharedPreferences.setMockInitialValues({});
    await tester.pumpWidget(const ProviderScope(
      child: MaterialApp(home: PenNoteScreen()),
    ));
    await tester.pump(); // let PenConfig load

    // Defaults from _brushColors: fountain pen = black, ballpoint = blue.
    expect(canvasColor(tester), Colors.black,
        reason: 'fountain pen starts black');

    // Switch to the ballpoint brush → its remembered color (blue) becomes active.
    await selectBrush(tester, 'Ballpoint');
    expect(canvasColor(tester), Colors.blue,
        reason: 'selecting ballpoint restores ITS remembered color');

    // Change the ACTIVE (ballpoint) brush's color to red via a color dot.
    await tapColorDot(tester, Colors.red);
    expect(canvasColor(tester), Colors.red,
        reason: 'color change applies to the active brush');

    // Switch back to the fountain pen → its color is still black (unchanged).
    await selectBrush(tester, 'Fountain pen');
    expect(canvasColor(tester), Colors.black,
        reason: 'fountain pen color was NOT affected by changing ballpoint');

    // Back to ballpoint → it remembers the red we set.
    await selectBrush(tester, 'Ballpoint');
    expect(canvasColor(tester), Colors.red,
        reason: 'ballpoint remembers its own updated color');
  });
}
