import 'package:chat/utils/network/http_request.dart';
import 'package:dio/dio.dart';

class Http {
  /// 取消请求
  static void cancelRequests({
    required CancelToken token,
  }) {
    HttpRequest().cancelRequests(
      token: token,
    );
  }

  /// GET 请求
  static Future<T> get<T>(
    String path, {
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onReceiveProgress,
  }) async {
    return await HttpRequest().request(
      path,
      method: HttpMethod.get,
      params: params,
      options: options,
      cancelToken: cancelToken,
    );
  }

  /// POST 请求
  static Future<T> post<T>(
    String path, {
    Map<String, dynamic>? params,
    dynamic data,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    return await HttpRequest().request(
      path,
      method: HttpMethod.post,
      params: params,
      data: data,
      options: options,
      cancelToken: cancelToken,
      onReceiveProgress: onReceiveProgress,
    );
  }

  /// PUT
  static Future<T> put<T>(
    String path, {
    Map<String, dynamic>? params,
    dynamic data,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    return await HttpRequest().request(
      path,
      method: HttpMethod.put,
      params: params,
      data: data,
      options: options,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
  }

  /// DELETE
  static Future<T> delete<T>(
    String path, {
    Map<String, dynamic>? params,
    dynamic data,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    return await HttpRequest().request(
      path,
      method: HttpMethod.delete,
      params: params,
      data: data,
      options: options,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
  }

  /// 上传文件
  static Future<T> upload<T>(
    String path, {
    required String filePath,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    var formData = FormData.fromMap({
      'upload': await MultipartFile.fromFile(filePath),
    });

    return await HttpRequest().request(
      path,
      method: HttpMethod.post,
      params: params,
      data: formData,
      options: options,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
  }
}
