part of 'app_test.dart';

Future<void> _loggedWithMultiLocalsAccount(
    [bool forceLinkedLocal = true]) async {
  await _init();
  await StorageHelper() //
      .set(SecureStorageKey.isLogged.value, 'true');
  await StorageHelper() //
      .set(SecureStorageKey.haveLocal.value, 'true');
  await StorageHelper() //
      .set(ProfileStorageKey.devUUID.key, 'b5c3818753e76d85');
  await StorageHelper() //
      .set(ProfileStorageKey.userUUID.key, '649c45d7514a28.85876308');

  await StorageHelper() //
      .set(SecureStorageKey.email.value, 'email_app@exemplo.com');
  await StorageHelper() //
      .set(SecureStorageKey.password.value, '12345678');
  await StorageHelper() //
      .set(LocalsStorageKey.isNewVersion.key, true);
  if (forceLinkedLocal == true) {
    await StorageHelper() //
        .set(ProfileStorageKey.clientUUID.key, '7');
    await PhpGroup //
        .resopndeVinculo
        .call(tarefa: 'A');
    await LicenseRepositoryImpl() //
        .resetLicense();
  }
}

Future<void> _loggedWithSomeoneLocalAccount(
    [bool forceLinkedLocal = true]) async {
  await _init();
  await StorageHelper() //
      .set(SecureStorageKey.isLogged.value, 'true');
  await StorageHelper() //
      .set(SecureStorageKey.haveLocal.value, 'true');
  await StorageHelper() //
      .set(ProfileStorageKey.devUUID.key, 'ca838e17f4139268');
  await StorageHelper() //
      .set(ProfileStorageKey.userUUID.key, '678aa05b0c2154.50583237');

  await StorageHelper() //
      .set(SecureStorageKey.email.value, 'local_teste8@exemplo.com');
  await StorageHelper() //
      .set(SecureStorageKey.password.value, '12345678');
  await StorageHelper() //
      .set(LocalsStorageKey.isNewVersion.key, true);
  if (forceLinkedLocal == true) {
    await StorageHelper() //
        .set(ProfileStorageKey.clientUUID.key, '7');
    await PhpGroup //
        .resopndeVinculo
        .call(tarefa: 'A');
    await LicenseRepositoryImpl() //
        .resetLicense();
  }
}

Future<void> _unlogged() async {
  await _init();
  await StorageHelper() //
      .set(SecureStorageKey.isLogged.value, 'false');
  await StorageHelper() //
      .set(SecureStorageKey.haveLocal.value, 'false');
  await StorageHelper() //
      .set(ProfileStorageKey.devUUID.key, '');
  await StorageHelper() //
      .set(ProfileStorageKey.userUUID.key, '');
  await StorageHelper() //
      .set(ProfileStorageKey.clientUUID.key, '');
  await StorageHelper() //
      .set(SecureStorageKey.email.value, '');
  await StorageHelper() //
      .set(SecureStorageKey.password.value, '');
  await StorageHelper() //
      .set(LocalsStorageKey.isNewVersion.key, true);
}

Future<void> _init() async {
  WidgetsFlutterBinding.ensureInitialized();
  await _initializeTracking();
  await _initializeStorage();
  await _initializeFirebase();
  await _initializeNotificationService();
  _initializeUrlStrategy();
  _initializeSystemSettings();
  await _initializeFlutterFlow();
  await _initializeNav();
}

Future<void> _navigateToSignIn(PatrolTester $) async {
  final signInButton = $(#toggleSignInPage).waitUntilVisible();
  await signInButton.tap();
}

Future<void> _navigateToSignUp(PatrolTester $) async {
  final signUpButton = $(#toggleSignUpPage).waitUntilVisible();
  await signUpButton.tap();
}

Future<void> _navigateBackUsingSystemGesture() async =>
    IntegrationTestWidgetsFlutterBinding.instance.keyboard
        .isLogicalKeyPressed(LogicalKeyboardKey.escape);

Future<void> _initializeTracking() async {
  print('Requesting tracking authorization...');
  await AppTrackingTransparency.requestTrackingAuthorization();
  print('Tracking authorization requested');
}

Future<void> _initializeFirebase() async {
  print('Initializing Firebase...');
  await Firebase.initializeApp();
  print('Firebase initialized');
}

Future<void> _initializeNotificationService() async {
  print('Initializing Notification Service...');
  await NotificationService.initialize(false);

  print('Notification Service initialized');
}

void _initializeUrlStrategy() {
  print('Initializing URL Strategy...');
  setUrlStrategy(PathUrlStrategy());
  print('URL Strategy initialized');
}

void _initializeSystemSettings() {
  print('Initializing System Settings...');
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  if (kDebugMode) {
    print('Debug mode');
  } else {
    final crashlyticsInstance = FirebaseCrashlytics.instance;
    print('Release mode');
    if (crashlyticsInstance.isCrashlyticsCollectionEnabled) {
      FlutterError.onError = crashlyticsInstance.recordFlutterError;
      print('Crashlytics enabled');
    }
  }
}

Future<void> _initializeFlutterFlow() async {
  print('Initializing FlutterFlow...');
  await FlutterFlowTheme.initialize();
  await FFLocalizations.initialize();
  print('FlutterFlow initialized');
}

Future<void> _initializeNav() async {
  print('Initializing Nav...');
  GoRouter.optionURLReflectsImperativeAPIs = true;
  usePathUrlStrategy();
  print('Nav initialized');
}

Future<void> _initializeStorage() async {
  await StorageHelper().init();
}
