import 'dart:io';

import 'package:hmg_patient_app_new/core/exceptions/api_exception.dart';
import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart';
import 'package:hmg_patient_app_new/core/utils/loading_utils.dart';
import 'package:hmg_patient_app_new/services/dialog_service.dart';
import 'package:hmg_patient_app_new/services/logger_service.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';

abstract class ErrorHandlerService {
  Future<void> handleError(
      {required Failure failure, Function() onOkPressed, Function(Failure)? onUnHandledFailure, Function(Failure)? onMessageStatusFailure});
}

class ErrorHandlerServiceImp implements ErrorHandlerService {
  final DialogService dialogService;
  final LoggerService loggerService;
  final NavigationService navigationService;

  ErrorHandlerServiceImp({
    required this.dialogService,
    required this.loggerService,
    required this.navigationService,
  });

  @override
  Future<void> handleError(
      {required Failure failure, Function()? onOkPressed, Function(Failure)? onUnHandledFailure, Function(Failure)? onMessageStatusFailure}) async {
    if (failure is APIException) {
      loggerService.logError("API Exception: ${failure.message}");
    } else if (failure is ServerFailure) {
      loggerService.logError("URL: ${failure.url} \n Server Failure: ${failure.message}");
      await _showDialog(failure, title: "Server Failure");
    } else if (failure is DataParsingFailure) {
      loggerService.logError("Data Parsing Failure: ${failure.message}");
      await _showDialog(failure, title: "Data Error");
    } else if (failure is StatusCodeFailure) {
      loggerService.logError("StatusCode Failure: ${failure.message}");
      await _showDialog(failure, title: "StatusCodeFailure");
    } else if (failure is ConnectivityFailure) {
      loggerService.logError("ConnectivityFailure : ${failure.message}");
      await _showDialog(failure, title: "ConnectivityFailure ");
    } else if (failure is UnAuthenticatedUserFailure) {
      loggerService.logError("URL: ${failure.url} \n UnAuthenticatedUser Failure: ${failure.message}");
      await _showDialog(failure, title: "UnAuthenticatedUser Failure", onOkPressed: () => navigationService.replaceAllRoutesAndNavigateToLanding());
    } else if (failure is AppUpdateFailure) {
      loggerService.logError("AppUpdateFailure : ${failure.message}");
      await _showDialog(failure, title: "AppUpdateFailure Error", onOkPressed: () => navigationService.replaceAllRoutesAndNavigateToLanding());
    } else if (failure is HttpException) {
      loggerService.logError("Http Exception: ${failure.message}");
      await _showDialog(failure, title: "Network Error");
    } else if (failure is UnknownFailure) {
      loggerService.logError("URL: ${failure.url} \n Unknown Failure: ${failure.message}");
      await _showDialog(failure, title: "Unknown Failure");
    } else if (failure is InvalidCredentials) {
      loggerService.logError("Invalid Credentials : ${failure.message}");
      await _showDialog(failure, title: "Invalid Credentials ");
    } else if (failure is UserIntimationFailure) {
      if (onUnHandledFailure != null) {
        onUnHandledFailure(failure);
      } else {
        await _showDialog(failure, title: "UserIntimationFailure", onOkPressed: onOkPressed);
      }
    } else if (failure is MessageStatusFailure) {
      if (onMessageStatusFailure != null) {
        onMessageStatusFailure(failure);
      } else {
        await _showDialog(failure, title: "MessageStatusFailure", onOkPressed: onOkPressed);
      }
    } else {
      loggerService.logError("Unhandled failure type: $failure");
      await _showDialog(failure, title: "Unhandled Error", onOkPressed: onOkPressed);
    }
  }

  Future<void> _showDialog(Failure failure, {required String title, Function()? onOkPressed}) async {
    if (LoadingUtils.isLoading) {
      LoadingUtils.hideFullScreenLoader();
    }
    await dialogService.showErrorBottomSheet(title: title, message: failure.message, onOkPressed: onOkPressed);
  }
}
