import BigNumber from "bignumber.js";

import _ from "lodash";

import {
  BaseCartItem,
  ShopUserInfo,
  ShopInfo,
  TimeLimitDiscountConfig,
  CanReturnMemberPriceArgs,
  returnPriceArgs,
} from "./types";

/**
 * 判断商品是否可以使用限时折扣
 * @param goods 商品对象
 * @param limitTimeDiscountRes 限时折扣配置
 * @param shopInfo  店铺信息
 * @param shopUserInfo 店铺用户信息
 * @param idKey 商品ID键名，默认"id"
 * @returns
 */
export function canUseLimitTimeDiscount(
  goods: BaseCartItem,
  limitTimeDiscountRes: TimeLimitDiscountConfig | null | undefined,
  shopInfo: ShopInfo,
  shopUserInfo: ShopUserInfo,
  idKey = "id" as keyof BaseCartItem
) {
  shopInfo = shopInfo || {};
  shopUserInfo = shopUserInfo || {};
  if(shopInfo.isMemberPrice){
    shopUserInfo.isMemberPrice=1
  }
  if (!limitTimeDiscountRes || !limitTimeDiscountRes.id) {
    return false;
  }

  const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");

  const goodsCanUse =
    limitTimeDiscountRes.foodType == 1 ||
    canUseFoods.includes(`${goods[idKey]}`);
  if (!goodsCanUse) {
    return false;
  }
  if (limitTimeDiscountRes.discountPriority == "limit-time") {
    return true;
  }
  if (limitTimeDiscountRes.discountPriority == "vip-price") {
    if (
      shopUserInfo.isVip == 1 &&
      shopUserInfo.isMemberPrice == 1 &&
      goods.memberPrice * 1 > 0
    ) {
      return false;
    }
    return true;
   
  }

  return false;
}

/**
 * 返回商品显示价格
 * @params {*} args 参数对象
 * @params {*} args.goods 商品对象
 * @params {*} args.shopInfo 店铺信息
 * @params {*} args.limitTimeDiscountRes 限时折扣信息
 * @params {*} args.shopUserInfo 店铺用户信息
 * @returns
 */
export function returnPrice(args: returnPriceArgs) {
  let {
    goods,
    shopInfo,
    limitTimeDiscountRes,
    shopUserInfo,
    idKey = "product_id",
  } = args;
  limitTimeDiscountRes = limitTimeDiscountRes || {
    foods: "",
    foodType: 2,
    discountPriority: "",
    discountRate: 0,
    id: 0,
    shopId: 0,
    useType: "",
  };
  const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");
  const includesGoods =
    limitTimeDiscountRes.foodType == 1 ||
    canUseFoods.includes("" + goods[idKey]);
  shopInfo = shopInfo || {};
  shopUserInfo = shopUserInfo || {};
  if (
    shopUserInfo.isMemberPrice == 1 &&
    shopUserInfo.isVip == 1 &&
    shopInfo.isMemberPrice == 1
  ) {
    const memberPrice = goods.memberPrice || goods.salePrice;

    //是会员而且启用会员价
    if (limitTimeDiscountRes) {
      //使用限时折扣
      //限时折扣优先
      if (limitTimeDiscountRes.discountPriority == "limit-time") {
        if (includesGoods) {
          return returnLimitPrice({
            price: goods.salePrice,
            limitTimeDiscountRes,
          });
        } else {
          return memberPrice;
        }
      }
      if (
        limitTimeDiscountRes.discountPriority == "vip-price" &&
        includesGoods
      ) {
        if (goods.memberPrice * 1 > 0) {
          //会员优先
          return memberPrice;
        } else {
          const price = returnLimitPrice({
            price: goods.salePrice,
            limitTimeDiscountRes,
            goods: goods,
          });

          return price;
        }
      } else {
        return memberPrice;
      }
    } else {
      //是会员没有限时折扣
      return memberPrice;
    }
  } else {
    //不是会员或者没有启用会员价
    if (limitTimeDiscountRes && limitTimeDiscountRes.id && includesGoods) {
      const price = returnLimitPrice({
        price: goods.salePrice,
        limitTimeDiscountRes,
        goods: goods,
      });

      return price;
    } else {
      return goods.salePrice;
    }
  }
}

interface returnLimitPriceArgs {
  limitTimeDiscountRes: TimeLimitDiscountConfig | null | undefined;
  price: number;
  goods?: BaseCartItem;
}
/**
 * 返回限时折扣价格
 * @params {*} args 参数对象
 * @params {*} args.limitTimeDiscountRes 限时折扣信息
 * @params {*} args.price 商品价格
 * @param  {*} args.goods 商品对象
 * @returns
 */
export function returnLimitPrice(args: returnLimitPriceArgs) {
  const { limitTimeDiscountRes, price, goods } = args;
  const discountRate = new BigNumber(
    limitTimeDiscountRes ? limitTimeDiscountRes.discountRate : 100
  ).dividedBy(100);

  const result = BigNumber(price)
    .times(discountRate)
    .decimalPlaces(2, BigNumber.ROUND_UP)
    .toNumber();
  return result;
}

/**
 * 判断是否返回会员价
 * @param {*} args 参数对象
 * @param {*} args.shopInfo 店铺信息
 * @param {*} args.shopUserInfo 店铺用户信息
 * @returns
 */
export function canReturnMemberPrice(args: CanReturnMemberPriceArgs) {
  const { shopInfo, shopUserInfo } = args;
  if (shopUserInfo.isMemberPrice == 1 && shopUserInfo.isVip == 1) {
    return true;
  } else {
    return false;
  }
}

/**
 * 返回会员价格
 * @param {*} goods
 * @returns
 */
export function returnMemberPrice(goods: BaseCartItem) {
  return goods.memberPrice || goods.salePrice;
}

export const utils = {
  returnPrice,
  canUseLimitTimeDiscount,
  returnLimitPrice,
  canReturnMemberPrice,
  returnMemberPrice,
};

export default utils;
