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_hud.dart';

void main() {
  Future<void> pumpHud(
    WidgetTester tester, {
    double zoomRatio = 1.0,
    double minZoomRatio = 1.0,
    double maxZoomRatio = 3.0,
    bool isRecording = false,
    bool isSwitchingLens = false,
    Future<void> Function()? onStop,
    ValueChanged<double>? onZoomSelected,
  }) async {
    await tester.pumpWidget(
      ScreenUtilInit(
        designSize: const Size(375, 812),
        builder: (context, _) {
          return MaterialApp(
            home: Scaffold(
              backgroundColor: Colors.black,
              body: RecordingHudWidget(
                hasDndAccess: true,
                isBatteryOptimizedIgnored: true,
                notificationsGranted: true,
                isRecording: isRecording,
                isStartingRecording: false,
                isSwitchingLens: isSwitchingLens,
                isTouchLocked: false,
                zoomRatio: zoomRatio,
                minZoomRatio: minZoomRatio,
                maxZoomRatio: maxZoomRatio,
                onStart: () async {},
                onStop: onStop ?? () async {},
                onOpenDnd: () {},
                onOpenBattery: () {},
                onToggleTouchLock: () {},
                onZoomSelected: onZoomSelected ?? (_) {},
              ),
            ),
          );
        },
      ),
    );
    await tester.pump();
  }

  testWidgets('shows preset zoom buttons', (tester) async {
    await pumpHud(tester);

    expect(find.text('广角'), findsNothing);
    expect(find.text('1x'), findsOneWidget);
    expect(find.text('2x'), findsNothing);
    expect(find.text('3x'), findsNothing);
  });

  testWidgets('shows wide angle when ultra-wide camera capability is 0.5', (
    tester,
  ) async {
    await pumpHud(tester, minZoomRatio: 0.5);

    expect(find.text('广角'), findsOneWidget);
    expect(find.text('1x'), findsOneWidget);
    expect(find.text('2x'), findsNothing);
    expect(find.text('3x'), findsNothing);
  });

  testWidgets('shows wide angle when 0.6x camera capability supports it', (
    tester,
  ) async {
    await pumpHud(tester, minZoomRatio: 0.6);

    expect(find.text('广角'), findsOneWidget);
    expect(find.text('1x'), findsOneWidget);
  });

  testWidgets('marks current 0.5x wide angle ratio as selected', (
    tester,
  ) async {
    await pumpHud(tester, zoomRatio: 0.5, minZoomRatio: 0.5);

    final selectedButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('广角'), matching: find.byType(TextButton)),
    );
    expect(selectedButton.enabled, isFalse);
  });

  testWidgets('marks current 0.6x wide angle ratio as selected', (
    tester,
  ) async {
    await pumpHud(tester, zoomRatio: 0.6, minZoomRatio: 0.6);

    final selectedButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('广角'), matching: find.byType(TextButton)),
    );
    expect(selectedButton.enabled, isFalse);
  });

  testWidgets('does not expose presets beyond max zoom ratio', (tester) async {
    await pumpHud(tester, minZoomRatio: 0.5, maxZoomRatio: 0.55);

    expect(find.text('广角'), findsOneWidget);
    expect(find.text('1x'), findsNothing);
  });

  testWidgets('tapping 0.5x reports 0.5 when camera capability is 0.5', (
    tester,
  ) async {
    double? selected;
    await pumpHud(
      tester,
      minZoomRatio: 0.5,
      onZoomSelected: (ratio) => selected = ratio,
    );

    await tester.tap(find.text('广角'));
    await tester.pump(const Duration(milliseconds: 350));

    expect(selected, 0.5);
  });

  testWidgets('tapping 0.6x reports 0.6 when camera only supports 0.6x', (
    tester,
  ) async {
    double? selected;
    await pumpHud(
      tester,
      minZoomRatio: 0.6,
      onZoomSelected: (ratio) => selected = ratio,
    );

    await tester.tap(find.text('广角'));
    await tester.pump(const Duration(milliseconds: 350));

    expect(selected, 0.6);
  });

  testWidgets('allows 0.6x while recording on main camera after unlock', (
    tester,
  ) async {
    double? selected;
    await pumpHud(
      tester,
      minZoomRatio: 0.6,
      isRecording: true,
      onZoomSelected: (ratio) => selected = ratio,
    );

    final ultraWideButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('广角'), matching: find.byType(TextButton)),
    );
    final mainButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('1x'), matching: find.byType(TextButton)),
    );

    expect(ultraWideButton.enabled, isTrue);
    expect(mainButton.enabled, isFalse);

    await tester.tap(find.text('广角'));
    await tester.pump(const Duration(milliseconds: 350));

    expect(selected, 0.6);
  });

  testWidgets('allows 1x while recording on ultra-wide after unlock', (
    tester,
  ) async {
    double? selected;
    await pumpHud(
      tester,
      zoomRatio: 0.5,
      minZoomRatio: 0.5,
      isRecording: true,
      onZoomSelected: (ratio) => selected = ratio,
    );

    final ultraWideButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('广角'), matching: find.byType(TextButton)),
    );
    final mainButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('1x'), matching: find.byType(TextButton)),
    );

    expect(ultraWideButton.enabled, isFalse);
    expect(mainButton.enabled, isTrue);

    await tester.tap(find.text('1x'));
    await tester.pump(const Duration(milliseconds: 350));

    expect(selected, 1.0);
  });

  testWidgets('disables stop button while switching lens', (tester) async {
    var stopped = false;
    await pumpHud(
      tester,
      minZoomRatio: 0.6,
      isRecording: true,
      isSwitchingLens: true,
      onStop: () async => stopped = true,
    );

    await tester.tap(find.byType(GestureDetector).last);
    await tester.pump();

    expect(stopped, isFalse);
  });

  testWidgets('disables zoom buttons while switching lens', (tester) async {
    double? selected;
    await pumpHud(
      tester,
      minZoomRatio: 0.6,
      isRecording: true,
      isSwitchingLens: true,
      onZoomSelected: (ratio) => selected = ratio,
    );

    final ultraWideButton = tester.widget<TextButton>(
      find.ancestor(of: find.text('广角'), matching: find.byType(TextButton)),
    );

    expect(ultraWideButton.enabled, isFalse);
    await tester.tap(find.text('广角'));
    await tester.pump(const Duration(milliseconds: 350));

    expect(selected, isNull);
  });
}
