import 'package:deus_app/common/local/StoreHelper.dart';
import 'package:deus_app/common/utils/ConstantString.dart';
import 'package:deus_app/main.dart';
import 'package:deus_app/page/login/login_page.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';

/// 请求方法
enum DioMethod {
  get,
  post,
  put,
  delete,
  patch,
  head,
}

class DioUtil {
  /// 单例模式
  static DioUtil? _instance;
  factory DioUtil() => _instance ?? DioUtil._internal();
  static DioUtil? get instance => _instance ?? DioUtil._internal();

  /// 连接超时时间
  static const int connectTimeout = 30 * 1000;

  /// 响应超时时间
  static const int receiveTimeout = 30 * 1000;

  /// Dio实例
  static Dio _dio = Dio();

  /// 初始化
  DioUtil._internal() {
    // 初始化基本选项
    BaseOptions options = BaseOptions(
        baseUrl: 'https://fqgz.flowbb.cn:7070/system-api/',
        connectTimeout: connectTimeout,
        receiveTimeout: receiveTimeout);
    _instance = this;
    // 初始化dio
    _dio = Dio(options);
    // 添加拦截器
    _dio.interceptors.add(InterceptorsWrapper(
        onRequest: _onRequest, onResponse: _onResponse, onError: _onError));
    _dio.interceptors.add(PrettyDioLogger(requestHeader: true, requestBody:
    true, responseHeader: true));
  }

  /// 请求拦截器
  void _onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // 对非open的接口的请求参数全部增加userId
    if (!options.path.contains("open")) {
      options.queryParameters["userId"] = "xxx";
    }
    // 头部添加token
    options.headers["Authorization"] = StoreHelper.getStorage(ConstantString.token);
    // 更多业务需求
    handler.next(options);
    // super.onRequest(options, handler);
  }

  /// 相应拦截器
  void _onResponse(
      Response response, ResponseInterceptorHandler handler) async {
    // 请求成功是对数据做基本处理
    if (response.statusCode == 200) {
      // ....
      handler.next(response);
    } else {
      // ....
      // showToast(response.statusMessage);
    }
    if (response.requestOptions.baseUrl.contains("???????")) {
      // 对某些单独的url返回数据做特殊处理
    }
  }

  /// 错误处理
  void _onError(DioError error, ErrorInterceptorHandler handler) {
    handler.next(error);
  }

  /// 请求类
  Future<T> request<T>(
      String path, {
        DioMethod method = DioMethod.get,
        Map<String, dynamic>? params,
        data,
        CancelToken? cancelToken,
        Options? options,
        ProgressCallback? onSendProgress,
        ProgressCallback? onReceiveProgress,
      }) async {
    const _methodValues = {
      DioMethod.get: 'get',
      DioMethod.post: 'post',
      DioMethod.put: 'put',
      DioMethod.delete: 'delete',
      DioMethod.patch: 'patch',
      DioMethod.head: 'head'
    };
    EasyLoading.show(status: 'loading...');
    options ??= Options(method: _methodValues[method]);
    try {
      Response response;
      response = await _dio.request(path,
          data: data,
          queryParameters: params,
          cancelToken: cancelToken,
          options: options,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
      EasyLoading.dismiss();
      if(response.data['code']==401){
        // 跳转登录页面并清空栈
        globalNavigatorKey.currentState
            ?.pushNamedAndRemoveUntil(LoginPage.routeName, (route) => false);
        // showToast('登录超时，请重新登录');
      }
        return response.data;
    } on DioError catch (e) {
      EasyLoading.dismiss();
      // showToast(e.message);
      if(e.response?.data['code']==401){
        // 跳转登录页面并清空栈
        globalNavigatorKey.currentState
            ?.pushNamedAndRemoveUntil(LoginPage.routeName, (route) => false);
        // showToast('登录超时，请重新登录');
      }
      return e.response?.data;
    }
  }

  /// 开启日志打印
  /// 需要打印日志的接口在接口请求前 DioUtil.instance?.openLog();
  void openLog() {
    _dio.interceptors
        .add(LogInterceptor(responseHeader: false, responseBody: true));
  }
}
