import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:farm_tpf/custom_model/ErrorCommon.dart';
import 'package:farm_tpf/utils/const_string.dart';

class AppException {
  static String handleError(dynamic error, {String? customMessageError}) {
    var errorDescription = "";

    try {
      var dioError = error as DioError;
      switch (dioError.type) {
        case DioErrorType.response:
          int statusCode = dioError.response?.statusCode ?? 0;
          if (statusCode == 400) {
            errorDescription = customMessageError ?? exception_dio_400;
            try {
              Map<String, dynamic> errorMap = jsonDecode(dioError.response?.data);
              var errorCode = ErrorCommon.fromJson(errorMap).errorKey;

              switch (errorCode) {
                case '1000':
                  errorDescription = exception_dio_1000;
                  break;
                case '1001':
                  errorDescription = exception_dio_1001;
                  break;
                case '1002':
                  errorDescription = exception_dio_1002;
                  break;
                case '1003':
                  errorDescription = exception_dio_1003;
                  break;
                case '1004':
                  errorDescription = exception_dio_1004;
                  break;
                case '1005':
                  errorDescription = exception_dio_1005;
                  break;
                case '1006':
                  errorDescription = exception_dio_1006;
                  break;
                case '1007':
                  errorDescription = exception_dio_1007;
                  break;
                case '1008':
                  errorDescription = exception_dio_1008;
                  break;
                case '1009':
                  errorDescription = exception_dio_1009;
                  break;
                case '1010':
                  errorDescription = exception_dio_1010;
                  break;
                case '1011':
                  errorDescription = exception_dio_1011;
                  break;
                case '1012':
                  errorDescription = exception_dio_1012;
                  break;
                case '1013':
                  errorDescription = exception_dio_1013;
                  break;
                case '1014':
                  errorDescription = exception_dio_1014;
                  break;
                case '1015':
                  errorDescription = exception_dio_1015;
                  break;
                case '1016':
                  errorDescription = exception_dio_1016;
                  break;
                case '1017':
                  errorDescription = exception_dio_1017;
                  break;
                case '1018':
                  errorDescription = exception_dio_1018;
                  break;
                case '1019':
                  errorDescription = exception_dio_1019;
                  break;
                case '1020':
                  errorDescription = exception_dio_1020;
                  break;
                case '1021':
                  errorDescription = exception_dio_1021;
                  break;
                default:
                  errorDescription = customMessageError ?? exception_dio_400;
              }
            } catch (e) {
              //
            }
          } else if (statusCode == 401) {
            errorDescription = exception_dio_401;
          } else if (statusCode == 403) {
            errorDescription = exception_dio_403;
          } else if (statusCode == 404) {
            errorDescription = exception_dio_404;
          } else if (statusCode == 500) {
            errorDescription = exception_dio_500;
          } else {
            errorDescription = exception_common;
          }
          break;
        case DioErrorType.connectTimeout:
          // TODO: Handle this case.
          break;
        case DioErrorType.sendTimeout:
          // TODO: Handle this case.
          break;
        case DioErrorType.receiveTimeout:
          // TODO: Handle this case.
          break;
        case DioErrorType.cancel:
          // TODO: Handle this case.
          break;
        case DioErrorType.other:
          // TODO: Handle this case.
          break;
      }
    } catch (e) {
      errorDescription = exception_common;
    }
    return errorDescription;
  }
}
