import 'dart:developer';

import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:hub/features/notification/index.dart';
import 'package:hub/features/storage/index.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/flutter_flow/nav/nav.dart';

Future<void> initializeApp() async {
  WidgetsFlutterBinding.ensureInitialized();
  await _initializeTracking();
  await StorageHelper().init();

  await _initializeFirebase();
  await _initializeNotificationService();
  _initializeUrlStrategy();
  _initializeSystemSettings();
  await _initializeFlutterFlow();
  await _initializeNav();
}

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();
  print('Notification Service initialized');
}

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

Future<void> _initializeSystemSettings() async {
  print('Initializing System Settings...');
  final crashlyticsInstance = FirebaseCrashlytics.instance;

  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  if (kDebugMode) {
    //kDebugMode
    print('Debug mode');
    bool unsentReports =
        await FirebaseCrashlytics.instance.checkForUnsentReports();
    if (unsentReports) {
      // Existem relatórios não enviados
      await crashlyticsInstance.sendUnsentReports();
      print('Existem relatórios de falhas não enviados.');
    } else {
      // Não existem relatórios não enviados
      print('Todos os relatórios de falhas foram enviados.');
    }
  } else {
    print('Release mode');

    await crashlyticsInstance.setCrashlyticsCollectionEnabled(true);
    // if (crashlyticsInstance.isCrashlyticsCollectionEnabled) {
    FlutterError.onError = await 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');
}
