/**
 * 订单价值
 */
import React, { useMemo, forwardRef, ForwardRefRenderFunction, useImperativeHandle } from "react";
import { getOrderValue } from "@utils/contractRules";
import { useOrderValuePrice } from "./useOrderValuePrice";
import { OrderTypeEnum, OrderSideEnum } from "@shared/types";

interface Props {
  unit?: string;
  scale: number;
  buyQuantity: any;
  sellQuantity: any;
  inputPrice: any;
  triggerPrice: any;
  buyOne?: string | undefined;
  sellOne?: string | undefined;
  orderType: OrderTypeEnum;
}

export interface OrderValueProps {
  getOrderValue: (side: OrderSideEnum) => string;
}
const OrderValueComp: ForwardRefRenderFunction<OrderValueProps, Props> = (props, ref) => {
  const { scale, buyQuantity, sellQuantity } = props;
  const { buy, sell } = useOrderValuePrice(
    props.inputPrice,
    props.triggerPrice,
    props.buyOne,
    props.sellOne,
    props.orderType
  );

  const orderValue = useMemo(() => {
    const buyValue = getOrderValue({
      price: buy,
      quantity: buyQuantity,
      scale: scale
    });
    const sellValue = getOrderValue({
      price: sell,
      quantity: sellQuantity,
      scale: scale
    });
    return {
      buyValue,
      sellValue
    };
  }, [scale, sell, buy, buyQuantity, sellQuantity]);
  useImperativeHandle(ref, () => ({
    getOrderValue: (side: OrderSideEnum) => {
      // 下单点击确认时候，获取单方向的orderValue
      return side === OrderSideEnum.Sell ? orderValue.sellValue : orderValue.buyValue;
    }
  }));

  //   console.log(buyQuantity, sellQuantity);
  return (
    <div className={"flex justify-between nts-orderValue-root mb-[20px]"}>
      <div className="nts-orderValue-buy text-left">
        <span className="mb-[2px]">订单价值</span>
        <div className="text-gray-16">
          {orderValue.buyValue} {props.unit}
        </div>
      </div>
      <div className="nts-orderValue-sell text-right">
        <span className="mb-[2px]">订单价值</span>
        <div className="text-gray-16">
          {orderValue.sellValue} {props.unit}
        </div>
      </div>
    </div>
  );
};

export const OrderValue = forwardRef(OrderValueComp);
