import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:recording_tool/features/recording/widgets/widget_recording_button.dart';

void main() {
  const designSize = Size(375, 812);
  const morphDuration = Duration(milliseconds: 380);

  Future<void> pumpButton(
    WidgetTester tester, {
    required bool isRecording,
    bool isStartingRecording = false,
  }) async {
    await tester.pumpWidget(
      ScreenUtilInit(
        designSize: designSize,
        builder: (context, _) {
          return MaterialApp(
            home: Scaffold(
              body: Center(
                child: RecordingControlButton(
                  isRecording: isRecording,
                  isStartingRecording: isStartingRecording,
                  onTap: () {},
                ),
              ),
            ),
          );
        },
      ),
    );
    await tester.pump();
  }

  Size innerCoreSize(WidgetTester tester) {
    final finder = find.byWidgetPredicate(
      (widget) =>
          widget is Container &&
          widget.decoration is BoxDecoration &&
          (widget.decoration! as BoxDecoration).color == Colors.red,
    );
    return tester.getSize(finder);
  }

  testWidgets('idle state uses large circular inner core', (tester) async {
    await pumpButton(tester, isRecording: false);

    final size = innerCoreSize(tester);
    expect(size.width, closeTo(62.r, 0.5));
    expect(size.height, closeTo(62.r, 0.5));
  });

  testWidgets('isStartingRecording morphs to stop square before isRecording', (
    tester,
  ) async {
    await pumpButton(
      tester,
      isRecording: false,
      isStartingRecording: true,
    );

    await tester.pump(morphDuration);
    await tester.pump();

    final size = innerCoreSize(tester);
    expect(size.width, closeTo(22.r, 0.5));
    expect(size.height, closeTo(22.r, 0.5));
  });

  testWidgets('isRecording forward and reverse morph without errors', (
    tester,
  ) async {
    await pumpButton(tester, isRecording: false);

    await tester.pumpWidget(
      ScreenUtilInit(
        designSize: designSize,
        builder: (context, _) {
          return MaterialApp(
            home: Scaffold(
              body: Center(
                child: RecordingControlButton(
                  isRecording: true,
                  onTap: () {},
                ),
              ),
            ),
          );
        },
      ),
    );
    await tester.pump(morphDuration);
    await tester.pump();

    expect(innerCoreSize(tester).width, closeTo(22.r, 0.5));

    await tester.pumpWidget(
      ScreenUtilInit(
        designSize: designSize,
        builder: (context, _) {
          return MaterialApp(
            home: Scaffold(
              body: Center(
                child: RecordingControlButton(
                  isRecording: false,
                  onTap: () {},
                ),
              ),
            ),
          );
        },
      ),
    );
    await tester.pump(morphDuration);
    await tester.pump();

    expect(innerCoreSize(tester).width, closeTo(62.r, 0.5));
  });

  testWidgets('failed start rolls morph back to idle circle', (tester) async {
    await pumpButton(
      tester,
      isRecording: false,
      isStartingRecording: true,
    );
    await tester.pump(morphDuration);
    await tester.pump();

    expect(innerCoreSize(tester).width, closeTo(22.r, 0.5));

    await pumpButton(tester, isRecording: false, isStartingRecording: false);
    await tester.pump(morphDuration);
    await tester.pump();

    expect(innerCoreSize(tester).width, closeTo(62.r, 0.5));
  });
}
