import 'dart:ffi';

import 'package:fpdart/fpdart.dart';
import 'package:kaer_with_panels/app/common/app_config.dart';
import 'package:kaer_with_panels/app/services/api_service/api.dart';
import 'package:kaer_with_panels/app/model/response/kr_login_data.dart';
import 'package:kaer_with_panels/app/model/response/kr_node_list.dart';
import 'package:kaer_with_panels/app/model/response/kr_package_list.dart';
import 'package:kaer_with_panels/app/network/base_response.dart';
import 'package:kaer_with_panels/app/network/http_error.dart';
import 'package:kaer_with_panels/app/network/http_util.dart';

import '../../model/response/kr_already_subscribe.dart';
import '../../model/response/kr_node_group_list.dart';
import '../../model/response/kr_order_status.dart';
import '../../model/response/kr_payment_methods.dart';
import '../../model/response/kr_purchase_order_no.dart';
import '../../model/response/kr_status.dart';
import '../../model/response/kr_user_available_subscribe.dart';

/// 订阅相关
class KRSubscribeApi {
  /// 获取可购买套餐
  Future<Either<HttpError, KRPackageList>> kr_getPackageListList() async {
    final Map<String, dynamic> data = <String, dynamic>{};

    BaseResponse<KRPackageList> baseResponse =
        await HttpUtil.getInstance().request<KRPackageList>(
      Api.kr_getPackageList,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model);
  }

  /// 获取节点列表
  Future<Either<HttpError, KRNodeList>> kr_nodeList(int id) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    BaseResponse<KRNodeList> baseResponse =
        await HttpUtil.getInstance().request<KRNodeList>(
      Api.kr_nodeList,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model);
  }

  /// 获取用户可用订阅
  Future<Either<HttpError, List<KRUserAvailableSubscribeItem>>>
      kr_userAvailableSubscribe({bool containsNodes = false}) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['contains_nodes'] = containsNodes;

    BaseResponse<KRUserAvailableSubscribeList> baseResponse =
        await HttpUtil.getInstance().request<KRUserAvailableSubscribeList>(
      Api.kr_userAvailableSubscribe,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.list);
  }

  /// 获取分组节点
  Future<Either<HttpError, List<KRNodeGroupListItem>>>
      kr_nodeGroupList() async {
    final Map<String, dynamic> data = <String, dynamic>{};

    BaseResponse<KRNodeGroupList> baseResponse =
        await HttpUtil.getInstance().request<KRNodeGroupList>(
      Api.kr_nodeGroupList,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.list);
  }

  /// 通过该接口判断订单状态
  Future<Either<HttpError, KROrderStatus>> kr_orderDetail(
      String orderId) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['order_no'] = orderId;

    BaseResponse<KROrderStatus> baseResponse =
        await HttpUtil.getInstance().request<KROrderStatus>(
      Api.kr_orderDetail,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model);
  }

  /// 获取支付方式
  Future<Either<HttpError, List<KRPaymentMethod>>>
      kr_getPaymentMethods() async {
    final Map<String, dynamic> data = <String, dynamic>{};

    BaseResponse<KRPaymentMethods> baseResponse =
        await HttpUtil.getInstance().request<KRPaymentMethods>(
      Api.kr_getPaymentMethods,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.list);
  }

  /// 进行下单
  Future<Either<HttpError, String>> kr_purchase(
      int planId, int quantity, int payment, String coupon) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['subscribe_id'] = planId;
    data['quantity'] = quantity;
    data['payment'] = payment;
    data['coupon'] = "";

    BaseResponse<KRPurchaseOrderNo> baseResponse =
        await HttpUtil.getInstance().request<KRPurchaseOrderNo>(
      Api.kr_purchase,
      data,
      method: HttpMethod.POST,
      isShowLoading: true,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.orderNo);
  }

  /// 续费
  Future<Either<HttpError, String>> kr_renewal(
      int planId, int quantity, int payment, String coupon) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['user_subscribe_id'] = planId;
    data['quantity'] = quantity;
    data['payment'] = payment;
    data['coupon'] = "";

    BaseResponse<KRPurchaseOrderNo> baseResponse =
        await HttpUtil.getInstance().request<KRPurchaseOrderNo>(
      Api.kr_renewal,
      data,
      method: HttpMethod.POST,
      isShowLoading: true,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.orderNo);
  }

  /// 获取用户已订阅套餐
  Future<Either<HttpError, List<KRAlreadySubscribe>>>
      kr_getAlreadySubscribe() async {
    final Map<String, dynamic> data = <String, dynamic>{};

    BaseResponse<KRAlreadySubscribeList> baseResponse =
        await HttpUtil.getInstance().request<KRAlreadySubscribeList>(
      Api.kr_getAlreadySubscribe,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.list);
  }

  Future<Either<HttpError, String>> kr_prePurchase(
      int planId, int quantity, String payment, String coupon) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['subscribe_id'] = planId;
    data['quantity'] = quantity;
    data['payment'] = payment;
    data['coupon'] = "";

    BaseResponse<KRPurchaseOrderNo> baseResponse =
        await HttpUtil.getInstance().request<KRPurchaseOrderNo>(
      Api.kr_preOrder,
      data,
      method: HttpMethod.POST,
      isShowLoading: true,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.orderNo);
  }

  /// 获取支付地址,跳转到付款地址
  Future<Either<HttpError, String>> kr_checkout(String orderId) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['orderNo'] = orderId;
    data['returnUrl'] = AppConfig.getInstance().baseUrl;

    BaseResponse<KRPurchaseOrderUrl> baseResponse =
        await HttpUtil.getInstance().request<KRPurchaseOrderUrl>(
      Api.kr_checkout,
      data,
      method: HttpMethod.POST,
      isShowLoading: true,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.url);
  }

  /// 重置订阅周期
  Future<Either<HttpError, bool>> kr_resetSubscribePeriod(
      int userSubscribeId) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['user_subscribe_id'] = userSubscribeId;
    BaseResponse<KRStatus> baseResponse =
        await HttpUtil.getInstance().request<KRStatus>(
      Api.kr_resetSubscribePeriod,
      data,
      method: HttpMethod.POST,
      isShowLoading: true,
    );
    if (!baseResponse.isSuccess) {
      return left(
          HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    return right(baseResponse.model.kr_bl);
  }
}
