import { useMemo } from "react";

import {
  GetSettlementV1PrivateCommissionPolicyQueryParams,
  useGetPaymentV1PrivateEpayrequestChargeCommissionPolicy,
  useGetSettlementV1PrivateCommissionPolicy,
} from "../../services";

export const useCommission = ({
  userWalletNumber,
  mode,
}: {
  userWalletNumber: GetSettlementV1PrivateCommissionPolicyQueryParams["userWalletNumber"];
  mode: "deposit" | "withdraw";
}) => {
  const { data: settlementData } = useGetSettlementV1PrivateCommissionPolicy(
    {
      moneyNetworkSymbol: "Shaparak",
      userWalletNumber,
    },
    { enabled: !!userWalletNumber && mode === "withdraw" },
  );
  const { data: depositData } =
    useGetPaymentV1PrivateEpayrequestChargeCommissionPolicy(
      {
        moneyNetworkSymbol: "Shaparak",
        userWalletNumber,
      },
      { enabled: !!userWalletNumber && mode === "deposit" },
    );
  const data = useMemo(() => {
    if (mode === "withdraw" && settlementData) {
      return settlementData;
    }
    if (mode === "deposit" && depositData) {
      return depositData;
    }
  }, [depositData, mode, settlementData]);

  const getCommission = (amount?: number) => {
    if (!data) {
      return;
    }
    const {
      commissionType,
      fixedValue,
      percent,
      minValue,
      maxValue,
      noCommissionAmount,
      stepSize,
    } = data;
    if (amount !== undefined && noCommissionAmount !== undefined) {
      const getCommissionByPercentType = () => {
        if (
          percent !== undefined &&
          minValue !== undefined &&
          maxValue !== undefined
        ) {
          const commissionedAmount = amount - noCommissionAmount;
          const commission = (commissionedAmount * percent) / 100;

          if (commission < minValue) {
            return minValue;
          } else if (commission > Number(maxValue || Number.MAX_SAFE_INTEGER)) {
            return maxValue;
          } else {
            return commission;
          }
        }
      };
      const getCommissionByPercentPlusFixedType = () => {
        if (
          percent !== undefined &&
          minValue !== undefined &&
          fixedValue !== undefined
        ) {
          const commissionedAmount = amount - noCommissionAmount;
          const commission = commissionedAmount * (percent / 100) + fixedValue;

          if (commission < minValue) {
            return minValue;
          } else if (commission > Number(maxValue || Number.MAX_SAFE_INTEGER)) {
            return maxValue;
          } else {
            return commission;
          }
        }
      };

      if (amount <= noCommissionAmount) return 0;

      if (commissionType === "Fixed") {
        return fixedValue;
      }

      if (commissionType === "Percentage") {
        return getCommissionByPercentType();
      }

      if (commissionType === "Step") {
        if (stepSize !== undefined) {
          if (amount <= stepSize) {
            return getCommissionByPercentType();
          }

          if (fixedValue !== undefined) {
            const steps = Math.ceil(amount / stepSize);
            return steps * fixedValue;
          }
        }
      }

      if (commissionType === "PercentagePlusFixed") {
        return getCommissionByPercentPlusFixedType();
      }
    }
  };

  return {
    getCommission,
  };
};
