import 'dart:developer';

import 'package:get_it/get_it.dart';
import 'package:mnemo_cards/admin/admin_api.dart';
import 'package:mnemo_cards/di/locator.dart';
import 'package:mnemo_cards/features/packs/pack_cache_manager.dart';
import 'package:mnemo_cards/features/packs/packs_api.dart';
import 'package:mnemo_cards/flags.dart';
import 'package:mnemo_cards/managers/favorite_cards.dart';
import 'package:mnemo_cards/managers/repository/dio_provider.dart';
import 'package:mnemo_cards/managers/repository/http_repository.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';

import '../features/packs/images_holder.dart';
import '../features/packs/pack_updater.dart';
import '../features/packs/preview_packs_poller.dart';
import '../features/purchase/in_app_purchase.dart';
import '../features/tests/test_manager.dart';
import '../features/packs/pack_holder.dart';
import '../features/packs/pack_manager.dart';
import '../features/packs/preview_pack_holder.dart';
import '../managers/user_manager.dart';

GetIt getIt = GetIt.I;

Future<void> setInjections() async {
  getIt.registerSingletonAsync<DioProvider>(
    () => DioProvider().init(),
  );
  getIt.registerLazySingleton<HttpRepository>(
    () => HttpRepository(getIt.get<DioProvider>().dio),
  );
  getIt.registerLazySingleton<PacksApi>(
    () => PacksApi(getIt.get<DioProvider>().dio),
  );
  if (ADMIN_BUILD)
    getIt.registerLazySingleton<AdminApi>(
      () => AdminApi(getIt.get<DioProvider>().dio),
    );
  getIt.registerLazySingleton<PackHolder>(
    () => PackHolder(),
  );
  getIt.registerLazySingleton<PreviewPackHolder>(
    () => PreviewPackHolder(),
  );
  getIt.registerLazySingleton<PackCacheManager>(
    () => PackCacheManager(),
  );
  getIt.registerLazySingleton<ImagesHolder>(
    () => ImagesHolder(),
  );

  getIt.registerLazySingleton<PreviewPacksPoller>(
    () => PreviewPacksPoller(
      getIt.get<PacksApi>(),
      getIt.get<PreviewPackHolder>(),
    ),
  );

  getIt.registerLazySingleton<PackManager>(
    () => PackManager(
      getIt.get<HttpRepository>(),
      getIt.get<PacksApi>(),
      getIt.get<PackHolder>(),
      getIt.get<PreviewPackHolder>(),
      getIt.get<PackCacheManager>(),
    ),
  );

  getIt.registerLazySingleton<PackUpdater>(
    () => PackUpdater(
      getIt.get<PacksApi>(),
      getIt.get<PackCacheManager>(),
      getIt.get<PackHolder>(),
      getIt.get<PreviewPackHolder>(),
    ),
  );

  getIt.registerLazySingleton<UserManager>(
    () => UserManager(
      getIt.get<HttpRepository>(),
    ),
  );

  getIt.registerLazySingleton<FavoriteCardsController>(
    () => FavoriteCardsController(),
  );

  getIt.registerLazySingleton<TestManager>(
    () =>
        TestManager(getIt.get<PackCacheManager>(), getIt.get<HttpRepository>()),
  );

  getIt.registerLazySingleton<InAppPurchaseService>(
    () => InAppPurchaseService(),
  );

  getIt.registerLazySingleton(
    () => PurchaseDetailsStreamSubscription(),
  );
  await getIt.allReady();
}
