import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'main.dart';
import 'src/integrations/nexo/nexo_notification_host_integration.dart';
import 'src/integrations/nexo/nexo_notification_plugin_client.dart';

class IopClientBootstrapOptions {
  final bool initializeFirebase;
  final bool initializeNotifications;
  final NexoNotificationHostIntegration? notificationHost;

  const IopClientBootstrapOptions({
    this.initializeFirebase = true,
    this.initializeNotifications = true,
    this.notificationHost,
  });
}

final NexoNotificationHostIntegration defaultNotificationHost =
    NexoNotificationHostIntegration(pushClient: NexoNotificationPluginClient());

Future<void> bootstrapIopClient({
  IopClientBootstrapOptions options = const IopClientBootstrapOptions(),
}) async {
  applyFullscreenMode();
  final initializeExternalPush = !kIsWeb;
  if (initializeExternalPush && options.initializeFirebase) {
    await Firebase.initializeApp();
  }
  if (initializeExternalPush && options.initializeNotifications) {
    final host = options.notificationHost ?? defaultNotificationHost;
    await host.initialize();
  }
}

Future<void> runIopClient({
  IopClientBootstrapOptions options = const IopClientBootstrapOptions(),
}) async {
  WidgetsFlutterBinding.ensureInitialized();
  await bootstrapIopClient(options: options);
  final initializeExternalPush = !kIsWeb;
  runApp(
    IopClientApp(
      notificationHost:
          initializeExternalPush && options.initializeNotifications
          ? (options.notificationHost ?? defaultNotificationHost)
          : null,
    ),
  );
}
