part of 'app_test.dart';

class LocalsTest {
  static Future setLocal() async {
    // patrolWidgetTest(
    //   'Selecionar um local disponível com multiplos locais disponiveis', //
    //   (PatrolTester tester) async {
    //     $ = tester;
    //     $.tester.printToConsole('Locals Test - Selecionar um local disponível');
    //
    //     await _loggedWithMultiLocalsAccount(false);
    //     await $.pumpWidgetAndSettle(const App());
    //     await $.waitUntilVisible($(MenuStaggeredView));
    //     await $.waitUntilVisible($(LocalProfileComponentWidget));
    //
    //     final PatrolFinder profileFinder =
    //         $(#AsyncLocalProfileComponentWidget_InkWell);
    //     expect(profileFinder, findsOneWidget);
    //
    //     await $(profileFinder) //
    //         .waitUntilVisible()
    //         .tap();
    //
    //     final PatrolFinder bottomSheetFinder =
    //         await $(BottomArrowLinkedLocalsComponentWidget) //
    //             .waitUntilVisible();
    //     expect(bottomSheetFinder, findsOneWidget);
    //
    //     final PatrolFinder listViewFinder = await $(bottomSheetFinder) //
    //         .$(ListView)
    //         .waitUntilVisible();
    //     expect(listViewFinder, findsOneWidget);
    //
    //     await $.pump(const Duration(milliseconds: 500));
    //
    //     final PatrolFinder entriesFinder = await $(listViewFinder) //
    //         .$(CardItemTemplateComponentWidget)
    //         .waitUntilVisible();
    //     expect(entriesFinder, findsWidgets);
    //
    //     if (entriesFinder.evaluate().isNotEmpty) {
    //       await $(entriesFinder.first) //
    //           .waitUntilVisible()
    //           .tap();
    //       await $.pumpAndSettle();
    //     }
    //   },
    // );
    patrolWidgetTest(
      'Selecionar um local disponível com somente um local disponivel', //
      (PatrolTester tester) async {
        $ = tester;
        $.tester.printToConsole('Locals Test - Selecionar um local disponível');

        await _loggedWithSomeoneLocalAccount(false);
        await $.pumpWidgetAndSettle(const App());
        await $.waitUntilVisible($(MenuStaggeredView));
        await $.waitUntilVisible($(LocalProfileComponentWidget));

        final PatrolFinder profileFinder =
            $(#AsyncLocalProfileComponentWidget_InkWell);
        expect(profileFinder, findsOneWidget);

        await $(profileFinder) //
            .waitUntilVisible()
            .tap();
      },
    );
  }

  static Future unlinkLocal() async {
    // patrolWidgetTest(
    //     'Desvincular do local selecionado com multiplos locais disponiveis', //
    //     (PatrolTester tester) async {
    //   $ = tester;
    //   $.tester.printToConsole(
    //       'Locals Test - Desvincular do local selecionado com multiplos locais disponiveis');
    //
    //   await _loggedWithMultiLocalsAccount();
    //   await $.pumpWidgetAndSettle(const App());
    //   await $.waitUntilVisible($(MenuStaggeredView));
    //
    //   final PatrolFinder gridView = await $(GridView) //
    //       .waitUntilVisible();
    //   final PatrolFinder entries = await $(gridView)
    //       .$(ButtonMenuItem) //
    //       .waitUntilVisible();
    //
    //   await $.pumpAndSettle();
    //   expect(entries, findsWidgets);
    //   final PatrolFinder settings = await $(gridView) //
    //       .$(Icons.settings)
    //       .waitUntilVisible();
    //   expect(settings, findsOneWidget);
    //
    //   await $(settings).tap();
    //
    //   final PatrolFinder unlinkButton = await $(Symbols.digital_out_of_home) //
    //       .waitUntilVisible();
    //   expect(unlinkButton, findsOneWidget);
    //   await $(unlinkButton) //
    //       .waitUntilVisible()
    //       .tap();
    //
    //   final PatrolFinder alertDialog = await $(#AlertDialogKey) //
    //       .waitUntilVisible();
    //   await alertDialog
    //       .$(#AcceptOptionKey) //
    //       .waitUntilVisible()
    //       .tap(settlePolicy: SettlePolicy.noSettle);
    //
    //   await $.pump();
    //   await $.pump(const Duration(seconds: 1));
    //   await $.pump();
    //
    //   final PatrolFinder bottomSheetFinder =
    //       await $(BottomArrowLinkedLocalsComponentWidget) //
    //           .waitUntilVisible();
    //
    //   expect(bottomSheetFinder, findsOneWidget);
    //
    //   final PatrolFinder listViewFinder = await $(bottomSheetFinder) //
    //       .$(ListView)
    //       .waitUntilVisible();
    //
    //   expect(listViewFinder, findsOneWidget);
    //
    //   final PatrolFinder entriesFinder = await $(listViewFinder)
    //       .$(CardItemTemplateComponentWidget)
    //       .waitUntilVisible();
    //
    //   expect(entriesFinder, findsWidgets);
    // });
    //
    // patrolWidgetTest(
    //   'Vincular um local desvinculado com multiplos locais disponiveis', //
    //   (PatrolTester tester) async {
    //     $ = tester;
    //     $.tester.printToConsole('Locals Test - Vincular um local desvinculado');
    //
    //     await _loggedWithMultiLocalsAccount(false);
    //     await $.pumpWidget(const App());
    //
    //     final PatrolFinder bottomSheetFinder =
    //         await $(BottomArrowLinkedLocalsComponentWidget) //
    //             .waitUntilVisible();
    //     expect(bottomSheetFinder, findsOneWidget);
    //
    //     await $.pump(const Duration(milliseconds: 500));
    //
    //     final PatrolFinder listViewFinder = $(bottomSheetFinder) //
    //         .$(ListView);
    //     expect(listViewFinder, findsOneWidget);
    //
    //     await $.pump(const Duration(milliseconds: 500));
    //
    //     final PatrolFinder entriesFinder = $(listViewFinder) //
    //         .$(CardItemTemplateComponentWidget);
    //     expect(entriesFinder, findsWidgets);
    //
    //     if (entriesFinder.evaluate().isNotEmpty) {
    //       await $(entriesFinder.first).waitUntilVisible().tap();
    //
    //       final PatrolFinder alertDialog = await $(#AlertDialogKey) //
    //           .waitUntilVisible();
    //       await alertDialog
    //           .$(#AcceptOptionKey) //
    //           .waitUntilVisible()
    //           .tap(settlePolicy: SettlePolicy.noSettle);
    //     }
    //
    //     await Future.delayed(const Duration(milliseconds: 500));
    //
    //     // try {
    //     //   await $.pumpAndSettle(
    //     //     duration: const Duration(seconds: 2),
    //     //     phase: EnginePhase.sendSemanticsUpdate,
    //     //     timeout: const Duration(seconds: 2),
    //     //   );
    //     //   throw Exception('Local está vinculado');
    //     // } catch (e) {
    //     //   await Future.delayed(const Duration(milliseconds: 500));
    //     //   return;
    //     // }
    //   },
    // );
    patrolWidgetTest(
        'Desvincular do local selecionado com somente um local disponivel', //
        (PatrolTester tester) async {
      $ = tester;
      $.tester.printToConsole(
          'Locals Test - Desvincular do local selecionado com multiplos locais disponiveis');

      await _loggedWithSomeoneLocalAccount();
      await $.pumpWidgetAndSettle(const App());
      await $.waitUntilVisible($(MenuStaggeredView));

      final PatrolFinder gridView = await $(GridView) //
          .waitUntilVisible();
      final PatrolFinder entries = await $(gridView)
          .$(ButtonMenuItem) //
          .waitUntilVisible();

      await $.pumpAndSettle();
      expect(entries, findsWidgets);
      final PatrolFinder settings = await $(gridView) //
          .$(Icons.settings)
          .waitUntilVisible();
      expect(settings, findsOneWidget);

      await $(settings).tap();

      final PatrolFinder unlinkButton = await $(Symbols.digital_out_of_home) //
          .waitUntilVisible();
      expect(unlinkButton, findsOneWidget);
      await $(unlinkButton) //
          .waitUntilVisible()
          .tap();

      final PatrolFinder alertDialog = await $(#AlertDialogKey) //
          .waitUntilVisible();
      await alertDialog
          .$(#AcceptOptionKey) //
          .waitUntilVisible()
          .tap(settlePolicy: SettlePolicy.noSettle);

      await $.pump();
      await $.pump(const Duration(seconds: 1));
      await $.pump();

      final PatrolFinder bottomSheetFinder =
          await $(BottomArrowLinkedLocalsComponentWidget) //
              .waitUntilVisible();

      expect(bottomSheetFinder, findsOneWidget);

      final PatrolFinder listViewFinder = await $(bottomSheetFinder) //
          .$(ListView)
          .waitUntilVisible();

      expect(listViewFinder, findsOneWidget);

      final PatrolFinder entriesFinder = await $(listViewFinder)
          .$(CardItemTemplateComponentWidget)
          .waitUntilVisible();

      expect(entriesFinder, findsWidgets);
    });

    patrolWidgetTest(
      'Vincular um local desvinculado com somente um local disponivel', //
      (PatrolTester tester) async {
        $ = tester;
        $.tester.printToConsole('Locals Test - Vincular um local desvinculado');

        await _loggedWithSomeoneLocalAccount(false);
        await $.pumpWidget(const App());

        final PatrolFinder bottomSheetFinder =
            await $(BottomArrowLinkedLocalsComponentWidget) //
                .waitUntilVisible();
        expect(bottomSheetFinder, findsOneWidget);

        await $.pump(const Duration(milliseconds: 500));

        final PatrolFinder listViewFinder = $(bottomSheetFinder) //
            .$(ListView);
        expect(listViewFinder, findsOneWidget);

        await $.pump(const Duration(milliseconds: 500));

        final PatrolFinder entriesFinder = $(listViewFinder) //
            .$(CardItemTemplateComponentWidget);
        expect(entriesFinder, findsWidgets);

        if (entriesFinder.evaluate().isNotEmpty) {
          await $(entriesFinder.first).waitUntilVisible().tap();

          final PatrolFinder alertDialog = await $(#AlertDialogKey) //
              .waitUntilVisible();
          await alertDialog
              .$(#AcceptOptionKey) //
              .waitUntilVisible()
              .tap(settlePolicy: SettlePolicy.noSettle);
        }

        await Future.delayed(const Duration(milliseconds: 500));

        // try {
        //   await $.pumpAndSettle(
        //     duration: const Duration(seconds: 2),
        //     phase: EnginePhase.sendSemanticsUpdate,
        //     timeout: const Duration(seconds: 2),
        //   );
        //   throw Exception('Local está vinculado');
        // } catch (e) {
        //   await Future.delayed(const Duration(milliseconds: 500));
        //   return;
        // }
      },
    );
  }

  static Future attachLocal() async {
    patrolWidgetTest(
      'Auto-Validação de Locais com Multiplos Locais', //
      (PatrolTester tester) async {
        $ = tester;
        $.tester.printToConsole('Locals Test - Vincular um local desvinculado');

        await _loggedWithMultiLocalsAccount(false);
        await $.pumpWidget(const App());

        final PatrolFinder bottomSheetFinder =
            await $(BottomArrowLinkedLocalsComponentWidget) //
                .waitUntilVisible();
        expect(bottomSheetFinder, findsOneWidget);

        await $.pump(const Duration(milliseconds: 500));

        final PatrolFinder listViewFinder = $(bottomSheetFinder) //
            .$(ListView);
        expect(listViewFinder, findsOneWidget);

        await $.pump(const Duration(milliseconds: 500));

        final PatrolFinder entriesFinder = $(listViewFinder) //
            .$(CardItemTemplateComponentWidget);
        expect(entriesFinder, findsWidgets);

        await Future.delayed(const Duration(milliseconds: 500));
      },
    );
    patrolWidgetTest(
      'Auto-Validação de Locais com local unico', //
      (PatrolTester tester) async {
        $ = tester;
        $.tester.printToConsole('Locals Test - Vincular um local desvinculado');

        await _loggedWithSomeoneLocalAccount(false);
        await $.pumpWidget(const App());

        final PatrolFinder bottomSheetFinder =
            await $(BottomArrowLinkedLocalsComponentWidget) //
                .waitUntilVisible();
        expect(bottomSheetFinder, findsOneWidget);

        await $.pump(const Duration(milliseconds: 500));

        final PatrolFinder listViewFinder = $(bottomSheetFinder) //
            .$(ListView);
        expect(listViewFinder, findsOneWidget);

        await $.pump(const Duration(milliseconds: 500));

        final PatrolFinder entriesFinder = $(listViewFinder) //
            .$(CardItemTemplateComponentWidget);
        expect(entriesFinder, findsWidgets);

        await Future.delayed(const Duration(milliseconds: 500));
      },
    );
  }
}
