// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'dart:async';

import 'package:example_router/core/enums/dev_mode.dart';
import 'package:example_router/core/flavors/flavor.dart';
import 'package:example_router/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:get_it/get_it.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';

final getIt = GetIt.I;

/// Service and Data Source locator
abstract class GetItInitializer {
  static FutureOr<void> _initCommon() async {
    // Initialize common sources/services
    getIt.registerLazySingleton<AuthenticationSessionDataSource<int>>(
      () => AuthenticationSessionDataSourceImpl<int>(),
    );
  }

  static FutureOr<void> _initEmulator() async {
    // Initialize emulator sources/services
    final firebaseAuth = FirebaseAuth.instance;
    await firebaseAuth.useAuthEmulator('localhost', 9099);
    getIt
      ..registerLazySingleton<AuthenticationRemoteDataSource<int>>(
        () => AuthenticationFirebaseDataSourceImpl<int>(
            firebaseAuth: firebaseAuth,
            googleSignIn:
                GoogleSignIn(clientId: DefaultFirebaseOptions.ios.iosClientId)),
      )
      ..registerLazySingleton<AuthenticationCacheDataSource<int>>(
        () => AuthenticationFirebaseCacheDataSourceImpl<int>(
          firebaseAuth: firebaseAuth,
        ),
      );
  }

  static FutureOr<void> _initFirebase() async {
    // Initialize firebase sources/services.
    getIt
      ..registerLazySingleton<AuthenticationRemoteDataSource<int>>(
        () => AuthenticationFirebaseDataSourceImpl<int>(
            firebaseAuth: FirebaseAuth.instance,
            googleSignIn:
                GoogleSignIn(clientId: DefaultFirebaseOptions.ios.iosClientId)),
      )
      ..registerLazySingleton<AuthenticationCacheDataSource<int>>(
        () => AuthenticationFirebaseCacheDataSourceImpl<int>(
          firebaseAuth: FirebaseAuth.instance,
        ),
      );
  }

  static FutureOr<void> _initRest() async {
    // Initialize rest api sources/services
  }

  static FutureOr<void> init() async {
    await _initCommon();
    final flavor = Flavor.get();

    if (flavor.devMode == DevMode.rest) {
      await _initRest();
    } else if (flavor.devMode == DevMode.emulator) {
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      await _initEmulator();
    } else {
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      await _initFirebase();
    }

    await getIt.allReady();
  }
}
