// import 'package:firebase_crashlytics/firebase_crashlytics.dart';
// import 'package:flutter/material.dart';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:get_it/get_it.dart';
import 'package:hmg_qline/api/api_client.dart';
import 'package:hmg_qline/repositories/screen_details_repo.dart';
import 'package:hmg_qline/repositories/signalR_repo.dart';
import 'package:hmg_qline/services/audio_service.dart';
import 'package:hmg_qline/services/cache_service.dart';
import 'package:hmg_qline/services/connectivity_service.dart';
import 'package:hmg_qline/services/logger_service.dart';
import 'package:hmg_qline/services/text_to_speech_service.dart';
import 'package:hmg_qline/services/crash_handler_service.dart';
import 'package:hmg_qline/utilities/native_method_handler.dart';
import 'package:hmg_qline/view_models/queuing_view_model.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:just_audio/just_audio.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';

final getIt = GetIt.instance;

class AppDependencies {
  static Future<void> addDependencies() async {
    final logger = Logger(
      printer: PrettyPrinter(
        printEmojis: false,
        colors: true,
        dateTimeFormat: DateTimeFormat.none,
      ),
    );

    //api client
    getIt.registerSingleton<LoggerService>(LoggerServiceImp(logger: logger));

    // register crash handler after logger so it can resolve logger
    getIt.registerSingleton<CrashHandlerService>(CrashHandlerServiceImp(loggerService: getIt.get<LoggerService>()));

    getIt.registerSingleton<ApiClient>(ApiClientImp(loggerService: getIt.get<LoggerService>()));

    //repos
    getIt.registerSingleton<SignalrRepo>(SignalrRepoImp(loggerService: getIt.get<LoggerService>()));
    getIt.registerSingleton<ScreenDetailsRepo>(ScreenDetailsRepoImp(apiClientInstance: getIt.get<ApiClient>(), loggerService: getIt.get<LoggerService>()));

    //repos
    getIt.registerSingleton<NativeMethodChannelService>(NativeMethodChannelServiceImp(loggerService: getIt.get<LoggerService>()));

    //ThirdPartyServices
    getIt.registerSingleton<ConnectivityService>(ConnectivityServiceImp(connectivityInstance: Connectivity()));
    getIt.registerSingleton<CacheService>(CacheServiceImp(preferencesInstance: await SharedPreferences.getInstance()));
    getIt.registerSingleton<AudioService>(AudioServiceImp(audioPlayerInstance: AudioPlayer()));
    getIt.registerSingleton<TextToSpeechService>(TextToSpeechServiceImp(textToSpeechInstance: FlutterTts(), loggerService: getIt.get<LoggerService>()));

    //ViewModels

    getIt.registerSingleton<ScreenConfigViewModel>(
      ScreenConfigViewModel(
        screenDetailsRepo: getIt.get<ScreenDetailsRepo>(),
        cacheService: getIt.get<CacheService>(),
        connectivityService: getIt.get<ConnectivityService>(),
        loggerService: getIt.get<LoggerService>(),
        nativeMethodChannelService: getIt.get<NativeMethodChannelService>(),
      ),
    );

    getIt.registerSingleton<QueuingViewModel>(
      QueuingViewModel(
        loggerService: getIt.get<LoggerService>(),
        screenDetailsRepo: getIt.get<ScreenDetailsRepo>(),
        cacheService: getIt.get<CacheService>(),
        textToSpeechService: getIt.get<TextToSpeechService>(),
        audioService: getIt.get<AudioService>(),
        signalrRepo: getIt.get<SignalrRepo>(),
      ),
    );
  }
}
