import { useQueryClient } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import {
  getWalletV1PrivateUserassetSpotDefaultAll,
  usePostExchangeV1PrivateOrder,
} from "../../../../services";
import { selectedSymbolStore } from "../../../../store";
import { getErrorMessage } from "../../../../utils";
import { useNotification } from "../../../notification";
import { useClearPriceOnOrder } from "../../../useClearPriceOnOrder";
import { MarketOrderValues, SellForm } from "../types";

const useSubmit = () => {
  const { t } = useTranslation();
  const { selectedSymbol } = selectedSymbolStore.useState();
  const { handleSubmit, setError } = SellForm.useFormContext();
  const { clearAllPrices } = useClearPriceOnOrder();
  const { successNotification, errorNotification } = useNotification();
  const queryClient = useQueryClient();
  const { mutate, isLoading, error, isSuccess } = usePostExchangeV1PrivateOrder(
    {
      onSuccess: () => {
        clearAllPrices();
        successNotification({
          message: t("orderSuccessfullyFilled"),
        });
        queryClient.invalidateQueries({
          queryKey: [getWalletV1PrivateUserassetSpotDefaultAll.key],
          refetchType: "all",
          exact: true,
        });
      },
      onError: (error) => {
        errorNotification({
          message: getErrorMessage(error),
        });
      },
    },
  );

  const onSellStopLimitSubmit = ({
    amount,
    selectedOption,
    stopPrice,
    total,
  }: MarketOrderValues) => {
    let quantity: number | undefined;
    let quoteOrderQty: number | undefined;

    if (!Number(stopPrice)) {
      setError("stopPrice", {
        message: t("enterPrice"),
      });
      return;
    }

    if (selectedOption.value === "amount") {
      quantity = Number(amount);

      if (!quantity) {
        setError("amount", {
          message: t("enterAmount"),
        });
        return;
      }
    } else {
      quoteOrderQty = Number(total);
      if (!quoteOrderQty) {
        setError("total", {
          message: t("enterAmount"),
        });
        return;
      }
    }

    mutate({
      requestBody: {
        type: "STOP_LOSS",
        symbol: selectedSymbol?.symbol,
        quantity: quantity,
        quoteOrderQty: quoteOrderQty,
        side: "SELL",
        stopPrice: Number(stopPrice),
        orderSourceType: "Trade",
        orderResponseType: "FULL",
        timeInForce: "GTC",
      },
    });
  };

  return {
    onSubmit: handleSubmit(onSellStopLimitSubmit),
    isLoading,
    error,
    isSuccess,
  };
};

export { useSubmit };
