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.errorLogs("API Exception: ${failure.message}");
    } else if (failure is ServerFailure) {
      loggerService.errorLogs("URL: ${failure.url} \n Server Failure: ${failure.message}");
      await _showDialog(failure, title: "Server Failure");
    } else if (failure is DataParsingFailure) {
      loggerService.errorLogs("Data Parsing Failure: ${failure.message}");
      await _showDialog(failure, title: "Data Error");
    } else if (failure is StatusCodeFailure) {
      loggerService.errorLogs("StatusCode Failure: ${failure.message}");
      await _showDialog(failure, title: "StatusCodeFailure");
    } else if (failure is ConnectivityFailure) {
      loggerService.errorLogs("ConnectivityFailure : ${failure.message}");
      await _showDialog(failure, title: "ConnectivityFailure ", onOkPressed: () {});
    } else if (failure is UnAuthenticatedUserFailure) {
      loggerService.errorLogs("URL: ${failure.url} \n UnAuthenticatedUser Failure: ${failure.message}");
      await _showDialog(failure, title: "UnAuthenticatedUser Failure", onOkPressed: () => navigationService.replaceAllRoutesAndNavigateToLanding());
    } else if (failure is AppUpdateFailure) {
      loggerService.errorLogs("AppUpdateFailure : ${failure.message}");
      await _showDialog(failure, title: "AppUpdateFailure Error", onOkPressed: () => navigationService.replaceAllRoutesAndNavigateToLanding());
    } else if (failure is HttpException) {
      loggerService.errorLogs("Http Exception: ${failure.message}");
      await _showDialog(failure, title: "Network Error");
    } else if (failure is UnknownFailure) {
      loggerService.errorLogs("URL: ${failure.url} \n Unknown Failure: ${failure.message}");
      await _showDialog(failure, title: "Unknown Failure");
    } else if (failure is InvalidCredentials) {
      loggerService.errorLogs("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.errorLogs("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);
  }
}
