import Decimal from "decimal.js";

import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import starkString from "starkstring";
import {
  useBuySellMarketConfigs,
  useGetOtcV1PublicOtcMinmaxamount,
} from "../..";
import { useDisplayDecimalDigits } from "../useDisplayDecimalDigits";
import { BuySellContext } from "./context";

export const useBuySellAmountValidation = () => {
  //  TODO "when use direct useWatch lastChangeField return undefined shervin"
  // const { control } = BuySellContext.useFormContext();

  // const [fromAsset, toAsset, lastChangedField] = useWatch({
  //   name: ["fromAsset", "toAsset", "lastChangedField"],
  //   control,
  //   defaultValue: {
  //     lastChangedField: "from",
  //   },
  // });
  // console.log({55:lastChangedField})

  const { lastChangedField, fromAsset, toAsset } = BuySellContext.useWatch();
  const { setValue } = BuySellContext.useFormContext();
  const { truncFix } = useDisplayDecimalDigits();
  const { t } = useTranslation();
  const {
    data: {
      minSourceQuantity,
      minDestinationQuantity,
      maxDestinationQuantity,
      maxSourceQuantity,
    } = {},
  } = useGetOtcV1PublicOtcMinmaxamount(
    {
      sourceCurrencySymbol: fromAsset,
      destinationCurrencySymbol: toAsset,
    },
    {
      enabled: !!fromAsset && !!toAsset,
    },
  );

  const minimumFrom = truncFix(minSourceQuantity, fromAsset) || 0;
  const maximumFrom = truncFix(maxSourceQuantity, fromAsset) || 0;
  const minimumTo = truncFix(minDestinationQuantity, toAsset) || 0;
  const maximumTo = truncFix(maxDestinationQuantity, toAsset) || 0;
  const { selectedCurrency } = useBuySellMarketConfigs({
    mode: lastChangedField,
  });

  const checkAmount = useCallback(
    ({
      value,
      mode,
    }: {
      value: string | null | undefined;
      mode: "from" | "to";
    }) => {
      const maximum = mode === "from" ? maximumFrom : maximumTo;
      const minimum =
        mode === "from" ? minSourceQuantity : minDestinationQuantity;

      const maximumFormatted = starkString(maximum)
        .scientificNotationToDecimal()
        .toCurrency()
        .toString();
      const minimumFormatted = starkString(minimum || 0)
        .scientificNotationToDecimal()
        .toCurrency()
        .toString();

      if (!value) {
        setValue("shouldCharge", false);
        return undefined;
      }
      if (lastChangedField === mode) {
        if (new Decimal(value).lessThan(minimum || 0)) {
          return t("valueShouldBeMoreThanMinPrice", {
            minPrice: minimumFormatted,
          });
        }
        if (new Decimal(value).greaterThan(maximum || 0)) {
          return t("valueShouldBeLessThanMaxPrice", {
            maxPrice: maximumFormatted,
          });
        }
        // else if (
        //   selectedCurrency &&
        //   selectedCurrency?.availableRemain < Number(value)
        // ) {
        //   return t("insufficientBalance");
        // }
      }
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      maximumFrom,
      maximumTo,
      minDestinationQuantity,
      minSourceQuantity,
      selectedCurrency?.symbol,
    ],
  );
  return {
    minimumFrom,
    maximumFrom,
    minimumTo,
    maximumTo,
    checkAmount,
  };
};
