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

void main() {
  group('resolveRecordingTouchLockUnlockIntent', () {
    test('returns stopRecording for portrait bottom 30 percent', () {
      final intent = resolveRecordingTouchLockUnlockIntent(
        position: const Offset(120, 466.9),
        size: const Size(375, 667),
      );

      expect(intent, RecordingTouchLockUnlockIntent.stopRecording);
    });

    test('returns unlockOnly for portrait area outside bottom 30 percent', () {
      final intent = resolveRecordingTouchLockUnlockIntent(
        position: const Offset(120, 320),
        size: const Size(375, 667),
      );

      expect(intent, RecordingTouchLockUnlockIntent.unlockOnly);
    });

    test('returns stopRecording for landscape right 30 percent', () {
      final intent = resolveRecordingTouchLockUnlockIntent(
        position: const Offset(466.9, 120),
        size: const Size(667, 375),
      );

      expect(intent, RecordingTouchLockUnlockIntent.stopRecording);
    });

    test('returns unlockOnly for landscape area outside right 30 percent', () {
      final intent = resolveRecordingTouchLockUnlockIntent(
        position: const Offset(320, 120),
        size: const Size(667, 375),
      );

      expect(intent, RecordingTouchLockUnlockIntent.unlockOnly);
    });
  });

  group('RecordingTouchLockOverlayWidget', () {
    Future<void> pumpOverlay(
      WidgetTester tester, {
      required Size surfaceSize,
      required ValueChanged<RecordingTouchLockUnlockIntent> onUnlocked,
    }) async {
      await tester.binding.setSurfaceSize(surfaceSize);
      addTearDown(() => tester.binding.setSurfaceSize(null));

      await tester.pumpWidget(
        ScreenUtilInit(
          designSize: const Size(375, 812),
          builder: (context, _) {
            return MaterialApp(
              home: Scaffold(
                body: Stack(
                  children: [
                    RecordingTouchLockOverlayWidget(
                      enabled: true,
                      unlockHoldDuration: const Duration(seconds: 2),
                      onUnlocked: onUnlocked,
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      );
    }

    testWidgets('long press in portrait bottom 30 percent stops recording', (
      tester,
    ) async {
      RecordingTouchLockUnlockIntent? receivedIntent;
      await pumpOverlay(
        tester,
        surfaceSize: const Size(375, 667),
        onUnlocked: (intent) => receivedIntent = intent,
      );

      final gesture = await tester.startGesture(const Offset(120, 600));
      await tester.pump(const Duration(seconds: 2));
      await gesture.up();

      expect(receivedIntent, RecordingTouchLockUnlockIntent.stopRecording);
    });

    testWidgets('long press outside stop area only unlocks', (tester) async {
      RecordingTouchLockUnlockIntent? receivedIntent;
      await pumpOverlay(
        tester,
        surfaceSize: const Size(375, 667),
        onUnlocked: (intent) => receivedIntent = intent,
      );

      final gesture = await tester.startGesture(const Offset(120, 320));
      await tester.pump(const Duration(seconds: 2));
      await gesture.up();

      expect(receivedIntent, RecordingTouchLockUnlockIntent.unlockOnly);
    });

    testWidgets('releasing before hold duration does not unlock', (
      tester,
    ) async {
      RecordingTouchLockUnlockIntent? receivedIntent;
      await pumpOverlay(
        tester,
        surfaceSize: const Size(375, 667),
        onUnlocked: (intent) => receivedIntent = intent,
      );

      final gesture = await tester.startGesture(const Offset(120, 600));
      await tester.pump(const Duration(milliseconds: 1500));
      await gesture.up();
      await tester.pump(const Duration(seconds: 1));

      expect(receivedIntent, isNull);
    });
  });
}
