import 'package:dio/dio.dart';
import 'package:uco_mobile_poc/app/core/config/server_response.dart';

import '../utils/logs_utils.dart';
import 'api_utils.dart';


class DioExceptions {
  static String errorMsg = "";
  static int statusCode = -1;

  static ServerResponse responseModel =
      ServerResponse({}, true, "", errorMsg, statusCode);

  static ServerResponse parseError(DioError dioError) {
    //

    dp("============================================", dioError);

    pe("Error response", dioError.response);
    statusCode = dioError.response?.statusCode ?? -1;
    responseModel.setStatusCode(statusCode);
    switch (dioError.type) {
      case DioErrorType.cancel:
        errorMsg = "Request to API server was cancelled";
        break;
      case DioErrorType.connectTimeout:
        errorMsg = "Connection timeout with API server";
        break;
      case DioErrorType.receiveTimeout:
        errorMsg = "Receive timeout in connection with API server";
        break;
      case DioErrorType.response:
        _handleError(dioError.response);
        break;
      case DioErrorType.sendTimeout:
        errorMsg = "Send timeout in connection with API server";
        break;
      case DioErrorType.other:
        if (dioError.message.contains("SocketException")) {
          errorMsg = 'No Internet';
          break;
        }
        errorMsg = "Unexpected error occurred";
        break;
      default:
        errorMsg = "Something went wrong";
        break;
    }

    return responseModel;
  }

  static void _handleError(dynamic error) {
    switch (statusCode) {
      case 400:
        {
          ApiUtils.parseError(responseModel, error);
        }
        break;
      case 401:
        {
          responseModel.setErrorMsg("Unauthorized");
        }
        break;
      case 403:
        {
          responseModel.setErrorMsg("Forbidden");
        }
        break;
      case 404:
        {
          responseModel.setResponse(error['message']);
        }
        break;
      case 500:
        {
          responseModel.setErrorMsg("Something went wrong, please try again");
        }
        break;
      case 502:
        {
          responseModel.setErrorMsg("Bad gateway");
        }
        break;
      default:
        {
          responseModel
              .setErrorMsg("Oops something went wrong, please try again");
        }
    }
  }
}
