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

const useSubmit = ({
  onOpenConfirmationModal,
}: {
  onOpenConfirmationModal?: (onConfirm: () => void) => void;
}) => {
  const queryClient = useQueryClient();
  const { t } = useTranslation();
  const { handleSubmit, setError } = BuyForm.useFormContext();
  const { selectedOption } = BuyForm.useWatch();
  const { selectedSymbol } = selectedSymbolStore.useState();
  const { clearAllPrices } = useClearPriceOnOrder();
  const { successNotification, errorNotification } = useNotification();

  const { mutate, isLoading, error } = usePostExchangeV1PrivateOrder({
    onSuccess: () => {
      clearAllPrices();
      successNotification({
        message: t("orderSuccessfullyFilled"),
      });
      queryClient.invalidateQueries({
        queryKey: [getWalletV1PrivateUserassetSpotDefaultAll.key],
        refetchType: "all",
        exact: true,
      });
    },
    onError: (error) => {
      errorNotification({
        message: getErrorMessage(error),
      });
    },
  });

  const { userData } = useAuth();
  const { data: userPreferences } = useGetPartyV1PrivateUsersettingPreference({
    enabled: !!userData,
  });
  useResetOnSymbol(BuyForm.useFormContext);

  const placeMarketBuyOrder = ({ amount, total }: MarketOrderValues) => {
    let quantity: number | undefined;
    let quoteQuantity: number | undefined;

    const placeOrder = () => {
      mutate({
        requestBody: {
          type: "MARKET",
          symbol: selectedSymbol?.symbol,
          quantity: quantity,
          quoteOrderQty: quoteQuantity,
          side: "BUY",
          orderSourceType: "Trade",
        },
      });
    };

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

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

    if (userPreferences?.marketOrder && onOpenConfirmationModal) {
      onOpenConfirmationModal(placeOrder);

      return;
    }

    placeOrder();
  };

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

export { useSubmit };
