import 'package:rasadyar_chicken/chicken.dart';
import 'package:rasadyar_chicken/data/common/dio_error_handler.dart';
import 'package:rasadyar_chicken/data/data_source/local/chicken_local.dart';
import 'package:rasadyar_chicken/data/data_source/local/chicken_local_imp.dart';
import 'package:rasadyar_chicken/data/data_source/remote/auth/auth_remote.dart';
import 'package:rasadyar_chicken/data/data_source/remote/auth/auth_remote_imp.dart';
import 'package:rasadyar_chicken/data/data_source/remote/chicken/chicken_remote.dart';
import 'package:rasadyar_chicken/data/data_source/remote/chicken/chicken_remote_imp.dart';
import 'package:rasadyar_chicken/data/repositories/auth/auth_repository.dart';
import 'package:rasadyar_chicken/data/repositories/auth/auth_repository_imp.dart';
import 'package:rasadyar_chicken/data/repositories/chicken/chicken_repository.dart';
import 'package:rasadyar_chicken/data/repositories/chicken/chicken_repository_imp.dart';
import 'package:rasadyar_core/core.dart';

GetIt diChicken = GetIt.instance;

Future<void> setupChickenDI() async {
  diChicken.registerSingleton(DioErrorHandler());
  var tokenService = Get.find<TokenStorageService>();

  diChicken.registerLazySingleton<AppInterceptor>(
    () => AppInterceptor(
      // سامانه مرغ فعلاً رفرش توکن ندارد
      refreshTokenCallback: () async => null,
      saveTokenCallback: (String newToken) async {
        await tokenService.saveAccessToken(newToken);
      },
      clearTokenCallback: () async {
        await tokenService.deleteTokens();
        Get.offAllNamed(ChickenRoutes.auth, arguments: Module.chicken);
      },
    ),
    instanceName: 'chickenInterceptor',
  );

  var baseUrl = tokenService.baseurl.value;

  diChicken.registerLazySingleton<DioRemote>(
    () => DioRemote(
      baseUrl: baseUrl,
      interceptors: diChicken.get<AppInterceptor>(instanceName: 'chickenInterceptor'),
    ),
  );

  final dioRemote = diChicken.get<DioRemote>();
  await dioRemote.init();

  diChicken.registerLazySingleton<AuthRemoteDataSource>(
    () => AuthRemoteDataSourceImp(dioRemote),
  );

  diChicken.registerLazySingleton<AuthRepository>(
    () => AuthRepositoryImpl(diChicken.get<AuthRemoteDataSource>()),
  );

  diChicken.registerLazySingleton<ChickenRemoteDatasource>(
    () => ChickenRemoteDatasourceImp(diChicken.get<DioRemote>()),
  );

  diChicken.registerLazySingleton<ChickenLocalDataSource>(() => ChickenLocalDataSourceImp());

  diChicken.registerLazySingleton<ChickenRepository>(
    () => ChickenRepositoryImp(
      remote: diChicken.get<ChickenRemoteDatasource>(),
      local: diChicken.get<ChickenLocalDataSource>(),
    ),
  );
}

Future<void> newSetupAuthDI(String newUrl) async {
  tLog("setup 2");
  var tokenService = Get.find<TokenStorageService>();
  if (tokenService.baseurl.value == null) {
    await tokenService.saveBaseUrl(newUrl);
  }

  if (diChicken.isRegistered<DioRemote>()) {
    await diChicken.unregister<DioRemote>();
    diChicken.registerLazySingleton<DioRemote>(
      () => DioRemote(
        baseUrl: newUrl,
        interceptors: diChicken.get<AppInterceptor>(instanceName: 'chickenInterceptor'),
      ),
    );
    final dioRemote = diChicken.get<DioRemote>();
    await dioRemote.init();
  }

  if (diChicken.isRegistered<AuthRemoteDataSource>()) {
    await diChicken.unregister<AuthRemoteDataSource>();
    diChicken.registerLazySingleton<AuthRemoteDataSource>(
      () => AuthRemoteDataSourceImp(diChicken.get<DioRemote>()),
    );
  }

  if (diChicken.isRegistered<AuthRepository>()) {
    await diChicken.unregister<AuthRepository>();
    diChicken.registerLazySingleton<AuthRepository>(
      () => AuthRepositoryImpl(diChicken.get<AuthRemoteDataSource>()),
    );
  }

  if (diChicken.isRegistered<ChickenRemoteDatasource>()) {
    await diChicken.unregister<ChickenRemoteDatasource>();
    diChicken.registerLazySingleton<ChickenRemoteDatasource>(
      () => ChickenRemoteDatasourceImp(diChicken.get<DioRemote>()),
    );
  }

  if (diChicken.isRegistered<ChickenLocalDataSource>()) {
    await diChicken.unregister<ChickenLocalDataSource>();
    diChicken.registerLazySingleton<ChickenLocalDataSource>(() => ChickenLocalDataSourceImp());
  }

  if (diChicken.isRegistered<ChickenRepository>()) {
    await diChicken.unregister<ChickenRepository>();
    diChicken.registerLazySingleton<ChickenRepository>(
      () => ChickenRepositoryImp(
        remote: diChicken.get<ChickenRemoteDatasource>(),
        local: diChicken.get<ChickenLocalDataSource>(),
      ),
    );
  }
}



Future<void> removeChickenDI() async {
  if (diChicken.isRegistered<DioErrorHandler>()) {
    diChicken.unregister<DioErrorHandler>();
  }
  if (diChicken.isRegistered<AppInterceptor>(instanceName: 'chickenInterceptor')) {
    diChicken.unregister<AppInterceptor>(instanceName: 'chickenInterceptor');
  }
  if (diChicken.isRegistered<DioRemote>()) {
    diChicken.unregister<DioRemote>();
  }
  if (diChicken.isRegistered<AuthRemoteDataSource>()) {
    diChicken.unregister<AuthRemoteDataSource>();
  }
  if (diChicken.isRegistered<AuthRepository>()) {
    diChicken.unregister<AuthRepository>();
  }
  if (diChicken.isRegistered<ChickenRemoteDatasource>()) {
    diChicken.unregister<ChickenRemoteDatasource>();
  }
  if (diChicken.isRegistered<ChickenLocalDataSource>()) {
    diChicken.unregister<ChickenLocalDataSource>();
  }
  if (diChicken.isRegistered<ChickenRepository>()) {
    diChicken.unregister<ChickenRepository>();
  }
}