import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:recording_tool/features/dialog/dialog-record.dart';
import 'package:recording_tool/features/recording/widgets/widget_recording_saved_dialog.dart';
import 'package:recording_tool/gen/assets.gen.dart';

void main() {
  Future<void> pumpDialogHost(WidgetTester tester, Widget child) async {
    await tester.pumpWidget(
      ScreenUtilInit(
        designSize: const Size(375, 812),
        builder: (context, _) {
          return MaterialApp(home: Scaffold(body: child));
        },
      ),
    );
  }

  testWidgets('single button dialog shows configured content and closes', (
    tester,
  ) async {
    var tapped = false;

    await pumpDialogHost(
      tester,
      Builder(
        builder: (context) {
          return TextButton(
            onPressed: () {
              RecordDialog.showSingle(
                context,
                title: '无选手信息！',
                buttonText: '粘贴',
                onPressed: () => tapped = true,
              );
            },
            child: const Text('show'),
          );
        },
      ),
    );

    await tester.tap(find.text('show'));
    await tester.pumpAndSettle();

    expect(find.byType(Image), findsOneWidget);
    expect(find.image(AssetImage(Assets.images.imageDialogBg.path)), findsOne);
    expect(find.text('无选手信息！'), findsOneWidget);
    expect(find.text('粘贴'), findsOneWidget);

    await tester.tap(find.text('粘贴'));
    await tester.pumpAndSettle();

    expect(tapped, isTrue);
    expect(find.text('无选手信息！'), findsNothing);
  });

  testWidgets('double button dialog dispatches each action', (tester) async {
    var leftTapped = false;
    var rightTapped = false;

    await pumpDialogHost(
      tester,
      Builder(
        builder: (context) {
          return TextButton(
            onPressed: () {
              RecordDialog.showDouble(
                context,
                title: '本轮比赛视频已保存到相册\n请选择后续录制信息',
                leftText: '继续本轮',
                rightText: '录制新轮',
                onLeftPressed: () => leftTapped = true,
                onRightPressed: () => rightTapped = true,
              );
            },
            child: const Text('show'),
          );
        },
      ),
    );

    await tester.tap(find.text('show'));
    await tester.pumpAndSettle();
    await tester.tap(find.text('继续本轮'));
    await tester.pumpAndSettle();

    expect(leftTapped, isTrue);
    expect(rightTapped, isFalse);

    await tester.tap(find.text('show'));
    await tester.pumpAndSettle();
    await tester.tap(find.text('录制新轮'));
    await tester.pumpAndSettle();

    expect(rightTapped, isTrue);
  });

  testWidgets('recording saved dialog follows design title only', (
    tester,
  ) async {
    await pumpDialogHost(
      tester,
      Builder(
        builder: (context) {
          return TextButton(
            onPressed: () {
              showRecordingSavedDialog(
                context,
                sessionTitle: '王东方 丨李想 空中格斗赛',
                onContinueRound: () {},
                onRecordNewRound: () {},
              );
            },
            child: const Text('show'),
          );
        },
      ),
    );

    await tester.tap(find.text('show'));
    await tester.pumpAndSettle();

    expect(find.text('王东方 丨李想 空中格斗赛'), findsNothing);
    expect(find.text('本轮比赛视频已保存到相册\n请选择后续录制信息'), findsOneWidget);
    expect(find.text('继续本轮'), findsOneWidget);
    expect(find.text('录制新轮'), findsOneWidget);
  });
}
