import routes, { palmistryV2Prefix } from "@/routes";
import { Route, Routes, useNavigate } from "react-router-dom";
import LayoutPalmistryV2 from "./LayoutPalmistryV2";
import NotFoundPage from "@/components/NotFoundPage";
import TrialPayment from "@/components/PalmistryV2/pages/TrialPayment";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { actions } from "@/store";
import SaveOff from "@/components/PalmistryV2/pages/SaveOff";
import SecretDiscount from "@/components/PalmistryV2/pages/SecretDiscount";
import OnboardingPage from "@/components/pages/ABDesign/v1/pages/Onboarding";
import { PrivateOutlet } from "@/components/App";
import AdditionalPurchasesPalmistry from "@/components/palmistry/AdditionalPurchases";
import SkipTrial from "@/components/palmistry/AdditionalPurchases/pages/SkipTrial";
import AddConsultant from "@/components/palmistry/AdditionalPurchases/pages/AddConsultant";
import AddGuides from "@/components/palmistry/AdditionalPurchases/pages/AddGuides";
import CheckSubscriptionOutlet from "./CheckSubscriptionOutlet";
import PaymentPage from "@/components/Payment/nmi/PaymentPage";
import { ELocalesPlacement } from "@/locales";

const removePrefix = (path: string) => path.replace(palmistryV2Prefix, "");

function PalmistryV2Routes() {
  const dispatch = useDispatch();
  const navigate = useNavigate();

  useEffect(() => {
    dispatch(actions.palmistry.update({ fromRedesign: true }));
  }, [dispatch]);

  function onPaymentError(error?: string | null): void {
    if (error === "Product not found") {
      return navigate(routes.client.palmistryV2TrialPayment());
    }
  }

  function onPaymentSuccess(): void {
    setTimeout(() => {
      navigate(routes.client.palmistryV2SkipTrial());
    }, 1500);
  }

  function onPopState(): void {
    if (
      document.location.toString() ===
      `${window.location.origin}${routes.client.palmistryV2TrialPayment()}`
    ) {
      navigate(routes.client.palmistryV2SaveOff());
    }
  }

  function onBack(): void {
    navigate(routes.client.palmistryV2SaveOff());
  }

  function onPaymentErrorDiscount(error?: string | null): void {
    if (error === "Product not found") {
      return navigate(routes.client.palmistryV2SecretDiscount());
    }
  }

  function onPaymentSuccessDiscount(): void {
    setTimeout(() => {
      navigate(routes.client.palmistryV2SkipTrial());
    }, 1500);
  }

  return (
    <Routes>
      <Route
        path={removePrefix(routes.client.palmistryV2Onboarding())}
        element={<OnboardingPage />}
      />
      <Route element={<PrivateOutlet />}>
        <Route element={<AdditionalPurchasesPalmistry />}>
          <Route
            path={removePrefix(routes.client.palmistryV2SkipTrial())}
            element={<SkipTrial />}
          />
          <Route
            path={removePrefix(routes.client.palmistryV2AddConsultant())}
            element={
              <AddConsultant
                funnel={ELocalesPlacement.EmailMarketingPalmistryV2}
                paymentPlacement="add_consultant"
              />
            }
          />
          <Route
            path={removePrefix(routes.client.palmistryV2AddGuides())}
            element={
              <AddGuides
                funnel={ELocalesPlacement.EmailMarketingPalmistryV2}
                paymentPlacement="add_guides"
              />
            }
          />
        </Route>
      </Route>
      <Route
        element={
          <CheckSubscriptionOutlet
            subscribedReturnUrl={routes.client.palmistryV2SkipTrial()}
          />
        }
      >
        <Route
          path={removePrefix(routes.client.palmistryV2PaymentModal())}
          element={
            <PaymentPage
              funnel={ELocalesPlacement.EmailMarketingPalmistryV2}
              paymentPlacement="main"
              onError={onPaymentError}
              onSuccess={onPaymentSuccess}
              onBack={onBack}
              onPopState={onPopState}
            />
          }
        />
        <Route
          path={removePrefix(
            routes.client.palmistryV2SecretDiscountPaymentModal()
          )}
          element={
            <PaymentPage
              funnel={ELocalesPlacement.EmailMarketingPalmistryV2}
              paymentPlacement="main_secret_discount"
              onError={onPaymentErrorDiscount}
              onSuccess={onPaymentSuccessDiscount}
            />
          }
        />
      </Route>
      <Route
        path={removePrefix(routes.client.palmistryV2SecretDiscount())}
        element={<SecretDiscount />}
      />
      <Route element={<LayoutPalmistryV2 />}>
        {/* <Route
          element={
            <CheckSubscriptionOutlet
              subscribedReturnUrl={routes.client.skipTrial()}
            />
          }
        > */}
        <Route
          path={removePrefix(routes.client.palmistryV2TrialPayment())}
          element={<TrialPayment />}
        />
        <Route
          path={removePrefix(routes.client.palmistryV2SaveOff())}
          element={<SaveOff />}
        />
        {/* <Route
            path={removePrefix(routes.client.palmistryV1Payment())}
            element={<Payment />}
          /> */}
        <Route path="*" element={<NotFoundPage />} />
      </Route>
      {/* </Route> */}
    </Routes>
  );
}

export default PalmistryV2Routes;
