import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_inspection/data/data_source/remote/auth/auth_remote.dart';
import 'package:rasadyar_inspection/data/data_source/remote/auth/auth_remote_imp.dart';
import 'package:rasadyar_inspection/data/data_source/remote/inspection/inspection_remote_imp.dart';
import 'package:rasadyar_inspection/data/data_source/remote/user/user_data_source_imp.dart';
import 'package:rasadyar_inspection/data/repositories/auth/auth_repository_imp.dart';
import 'package:rasadyar_inspection/data/repositories/inspection/inspection_repository_imp.dart';
import 'package:rasadyar_inspection/data/repositories/user/user_repository_imp.dart';
import 'package:rasadyar_inspection/data/utils/dio_exception_handeler.dart';
import 'package:rasadyar_inspection/presentation/routes/app_routes.dart';

GetIt diInspection = GetIt.asNewInstance();

Future<void> setupInspectionDI() async {
  diInspection.registerSingleton(DioErrorHandler());
  var tokenService = Get.find<TokenStorageService>();
  if (tokenService.baseurl.value == null) {
    await tokenService.saveBaseUrl(Module.inspection,'https://bazrasbackend.rasadyaar.ir/');
  }
  diInspection.registerLazySingleton<AppInterceptor>(
    () => AppInterceptor(
      //TODO : Update the base URL to the correct one for inspection module
      refreshTokenCallback: () async => null,
      saveTokenCallback: (String newToken) async {
      //  await tokenService.saveAccessToken(newToken);
      },
      clearTokenCallback: () async {
        await tokenService.deleteModuleTokens(Module.inspection);
        Get.offAllNamed(InspectionRoutes.auth, arguments: Module.inspection);
      },
      authArguments: Module.inspection,
    ),
  );

  diInspection.registerLazySingleton<DioRemote>(() {
    return DioRemote(
      baseUrl: tokenService.baseurl.value,
      interceptors: diInspection.get<AppInterceptor>(),
    );
  });

  var dioRemote = diInspection.get<DioRemote>();
  await dioRemote.init();

  diInspection.registerLazySingleton<AuthRemote>(
    () => AuthRemoteImp(dioRemote),
  );

  diInspection.registerSingleton<AuthRepositoryImpl>(
    AuthRepositoryImpl(diInspection.get<AuthRemote>()),
  );

  diInspection.registerSingleton<UserRemoteDataSourceImp>(
    UserRemoteDataSourceImp(dioRemote),
  );

  diInspection.registerSingleton<UserRepositoryImp>(
    UserRepositoryImp(diInspection.get<UserRemoteDataSourceImp>()),
  );

  diInspection.registerSingleton<InspectionRemoteDataSourceImp>(
    InspectionRemoteDataSourceImp(dioRemote),
  );

  diInspection.registerSingleton<InspectionRepositoryImp>(
    InspectionRepositoryImp(remoteDataSource: diInspection.get<InspectionRemoteDataSourceImp>()),
  );

  diInspection.registerSingleton(ImagePicker());
}


Future<void> removeInspectionDI() async {
  eLog("removeInspectionDI");
  await diInspection.resetScope();
 /* if (diInspection.isRegistered<DioErrorHandler>()) {
    diInspection.unregister<DioErrorHandler>();
  }
  if (diInspection.isRegistered<AppInterceptor>()) {
    diInspection.unregister<AppInterceptor>();
  }
  if (diInspection.isRegistered<DioRemote>()) {
    diInspection.unregister<DioRemote>();
  }
  if (diInspection.isRegistered<AuthRemote>()) {
    diInspection.unregister<AuthRemote>();
  }
  if (diInspection.isRegistered<AuthRepositoryImpl>()) {
    diInspection.unregister<AuthRepositoryImpl>();
  }
  if (diInspection.isRegistered<UserRemoteDataSourceImp>()) {
    diInspection.unregister<UserRemoteDataSourceImp>();
  }
  if (diInspection.isRegistered<UserRepositoryImp>()) {
    diInspection.unregister<UserRepositoryImp>();
  }
  if (diInspection.isRegistered<InspectionRemoteDataSourceImp>()) {
    diInspection.unregister<InspectionRemoteDataSourceImp>();
  }
  if (diInspection.isRegistered<InspectionRepositoryImp>()) {
    diInspection.unregister<InspectionRepositoryImp>();
  }
  if (diInspection.isRegistered<ImagePicker>()) {
    diInspection.unregister<ImagePicker>();
  }*/
}
