import 'package:electronic_assistant/utils/http_handler.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:get/get.dart';

import '../data/consts/error_code.dart';
import '../resource/string.gen.dart';

class ErrorHandler {
  ErrorHandler._();

  static void toastError(dynamic error, {String? message}) {
    String toastMessage = (error is ServerErrorException)
        ? _getToastMessageFromError(error)
        : _getDefaultToastMessage(message);
    ToastUtil.showToast(toastMessage);
  }

  static String _getToastMessageFromError(ServerErrorException error) {
    return getErrorCodeMsg(error.code) ?? StringName.networkError.tr;
  }

  static String? getErrorCodeMsg(int? code) {
    String? msg;
    switch (code) {
      case ErrorCode.errorCodeNoLogin:
        msg = StringName.errorCodeNoLogin.tr;
        break;
    }
    return msg;
  }

  static String _getDefaultToastMessage(String? message) {
    return message ?? StringName.networkError.tr;
  }
}
