import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'main.dart';
import 'src/integrations/mattermost/mattermost_push_host_integration.dart';
import 'src/integrations/mattermost/mattermost_push_plugin_client.dart';

class IopClientBootstrapOptions {
  final bool initializeFirebase;
  final bool initializeMattermost;
  final MattermostPushHostIntegration? mattermostHost;

  const IopClientBootstrapOptions({
    this.initializeFirebase = true,
    this.initializeMattermost = true,
    this.mattermostHost,
  });
}

final MattermostPushHostIntegration defaultMattermostHost =
    MattermostPushHostIntegration(pushClient: MattermostPushPluginClient());

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

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