import Taro from '@tarojs/taro';

import { ProductType, QrCodeType } from '@/constants/product';
import http from '@/http';
import { API, DOMAIN } from '@/http/api';
import {
  ProductInfo,
  GetProductDetailRequest,
  GetProductIsUnlockRequest,
  PostUseProductRequest,
  GetProductIsUnlockResponse,
  CustomerServiceInfo,
  CreatePayInfoRequest,
  CreatePayInfoResponse,
  CreatePayOrderParams,
  GetOrderInfoRequest,
  OrderInfo,
} from '@/types/product';
import { getUserId } from '@/utils/user';

export const isCancelPay = err => err?.errMsg === 'requestPayment:fail cancel';

export const getOrderPrice = (price: number) => {
  // return 1;
  return price * 100;
};

export async function requestProductList() {
  const data = { userId: getUserId() };
  const list = await http.post<ProductInfo[]>(API.GET_PRODUCT_LIST, { data });
  return list;
}

// 判断某个产品是否已经解锁
export async function requestProductUseRecord(
  productCode: ProductType,
  params: Omit<PostUseProductRequest, 'productCode' | 'userId'> = {}
) {
  const data: GetProductIsUnlockRequest = { ...params, productCode, userId: getUserId() };
  // 返回结果不是空对象则是已经解锁
  return await http.post<GetProductIsUnlockResponse>(API.PRODUCT_USE_RECORD, {
    data,
    contentType: 'application/x-www-form-urlencoded',
  });
}

// 使用某一个产品
export async function requestUseProduct(
  productCode: ProductType,
  params: Omit<PostUseProductRequest, 'productCode' | 'userId'> = {}
) {
  const data: PostUseProductRequest = { ...params, productCode, userId: getUserId() };
  return await http.post<ProductInfo>(API.USE_PRODUCT, { data });
}

// 获取某个产品的剩余解锁次数
export async function requestProductBalance(productCode: ProductType) {
  const data: GetProductDetailRequest = { productCode, userId: getUserId() };
  const { balance } = await http.post<ProductInfo>(API.GET_PRODUCT_DETAIL, {
    data,
    contentType: 'application/x-www-form-urlencoded',
  });
  return balance;
}

// 是否可以购买某一个产品
export async function requestAllBuyProduct(productCode: ProductType) {
  const data: GetProductDetailRequest = { productCode, userId: getUserId() };
  const enable = await http.post<ProductInfo>(API.ALLOW_BUY_PRODUCT, {
    data,
    contentType: 'application/x-www-form-urlencoded',
  });
  return enable;
}

export async function requestCsQrCode(_type: QrCodeType) {
  const result = await http.post<CustomerServiceInfo>(API.CS_QR_CODE);
  return `${DOMAIN}/${result.vxQrCode}`;
}

export async function requestCreatePayInfo(params: Omit<CreatePayInfoRequest, 'payChannel' | 'payType' | 'userId'>) {
  const data: CreatePayInfoRequest = { ...params, payChannel: 1, payType: 1, userId: getUserId() };
  const result = await http.post<CreatePayInfoResponse>(API.CREATE_PAY_ORDER, { data });
  const createPayInfo = JSON.parse(result.createPayInfo) as CreatePayOrderParams;
  return { ...result, createPayInfo } as CreatePayInfoResponse & { createPayInfo: CreatePayOrderParams };
}

export async function requestOrderInfo(params: Omit<GetOrderInfoRequest, 'userId'>) {
  const data: GetOrderInfoRequest = { ...params, userId: getUserId() };
  const result = await http.post<OrderInfo>(API.GET_PAY_ORDER, {
    data,
    contentType: 'application/x-www-form-urlencoded',
  });
  return result;
}

export async function requestPayment(params: Taro.requestPayment.Option) {
  return new Promise((resolve, reject) =>
    Taro.requestPayment({
      ...params,
      fail: reject,
      success: res => {
        params.success?.(res);
        resolve(res);
      },
    })
  );
}
