import 'package:dio/dio.dart';
import 'package:uuvpn/constant/app_strings.dart';
import 'package:uuvpn/router/application.dart';
import 'package:uuvpn/router/routers.dart';
import 'package:uuvpn/utils/common_util.dart';
import 'package:uuvpn/utils/shared_preferences_util.dart';

class HttpUtil {
  static HttpUtil get instance => _httpUtil;
  static final HttpUtil _httpUtil = HttpUtil();
  late Dio dio;

  HttpUtil() {
    BaseOptions options = BaseOptions(
      connectTimeout: Duration(seconds: 10000),
      receiveTimeout: Duration(seconds: 10000),
    );
    dio = Dio(options);
    dio.interceptors
        .add(InterceptorsWrapper(onRequest: (options, handler) async {
      //print("========================请求数据===================");
      //print("url=${options.uri.toString()}");
      //print("headers=${options.headers}");
      //print("params=${options.data}");

      //如果token存在在请求参数加上token
      await SharedPreferencesUtil.getInstance()
          ?.getString(AppStrings.token)
          .then((token) {
        if (token != null) {
          options.queryParameters[AppStrings.token] = token;
          //print("token=$token");
        }
      });

      //如果auth_data存在在请求参数加上auth_data
      await SharedPreferencesUtil.getInstance()
          ?.getString(AppStrings.authData)
          .then((authData) {
        if (authData != null) {
          options.queryParameters[AppStrings.authData] = authData;
          //print("authData=$authData");
        }
      });

      return handler.next(options);
    }, onResponse: (response, handler) {
      //print("========================请求数据===================");
      //print("code=${response.statusCode}");

      if (response.statusCode! < 200 || response.statusCode! >= 300) {
        if (response.statusCode == 403) {
          Application.navigatorKey.currentState?.pushNamed(Routers.login);
        }

        return handler.reject(DioException(
            requestOptions: response.requestOptions,
            response: response,
            type: DioExceptionType.badResponse));
      }

      return handler.next(response);
    }, onError: (error, handler) {
      //print("========================请求错误===================");
      //print("message =${error.message}");
      //print("code=${error.response?.statusCode}");

      return handler.next(error);
    }));
  }

  //get请求
  Future get(String url,
      {Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response =
          await dio.get(url, queryParameters: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.get(url, queryParameters: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.get(url, options: options);
    } else {
      response = await dio.get(url);
    }
    return response.data;
  }

  //post请求
  Future post(String url,
      {Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.post(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.post(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.post(url, options: options);
    } else {
      response = await dio.post(url);
    }
    return response.data;
  }

  //put请求
  Future put(String url,
      {Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.put(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.put(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.put(url, options: options);
    } else {
      response = await dio.put(url);
    }
    return response.data;
  }

  //delete请求
  Future delete(String url,
      {Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.delete(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.delete(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.delete(url, options: options);
    } else {
      response = await dio.delete(url);
    }
    return response.data;
  }
}
