import routes, { emailMarketingV1Prefix } from "@/routes";
import { Outlet, Route, Routes, useNavigate } from "react-router-dom";
import NotFoundPage from "@/components/NotFoundPage";
import MarketingLanding from "@/components/EmailMarketing/v1/pages/MarketingLanding";
import styles from "./styles.module.scss";
import SpecialOffer from "@/components/EmailMarketing/v1/pages/SpecialOffer";
import SecretDiscount from "@/components/EmailMarketing/v1/pages/SecretDiscount";
import SaveOff from "@/components/EmailMarketing/v1/pages/SaveOff";
import CheckSubscriptionOutlet from "./CheckSubscriptionOutlet";
import PaymentPage from "@/components/Payment/nmi/PaymentPage";
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 OnboardingPage from "@/components/pages/ABDesign/v1/pages/Onboarding";
import { ELocalesPlacement } from "@/locales";

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

function MarketingLandingV1Routes() {
  const navigate = useNavigate();

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

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

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

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

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

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

  return (
    <Routes>
      <Route
        path={removePrefix(routes.client.emailMarketingV1Onboarding())}
        element={<OnboardingPage />}
      />
      <Route element={<PrivateOutlet />}>
        <Route element={<AdditionalPurchasesPalmistry />}>
          <Route
            path={removePrefix(routes.client.emailMarketingV1SkipTrial())}
            element={<SkipTrial />}
          />
          <Route
            path={removePrefix(routes.client.emailMarketingV1AddConsultant())}
            element={
              <AddConsultant
                funnel={ELocalesPlacement.EmailMarketingCompatibilityV1}
                paymentPlacement="add_consultant"
              />
            }
          />
          <Route
            path={removePrefix(routes.client.emailMarketingV1AddGuides())}
            element={
              <AddGuides
                funnel={ELocalesPlacement.EmailMarketingCompatibilityV1}
                paymentPlacement="add_guides"
              />
            }
          />
        </Route>
      </Route>
      <Route
        element={
          <CheckSubscriptionOutlet
            subscribedReturnUrl={routes.client.compatibilityV2SkipTrial()}
          />
        }
      >
        <Route
          path={removePrefix(routes.client.emailMarketingV1PaymentModal())}
          element={
            <PaymentPage
              funnel={ELocalesPlacement.EmailMarketingCompatibilityV1}
              paymentPlacement="main"
              onError={onPaymentError}
              onSuccess={onPaymentSuccess}
              onBack={onBack}
              onPopState={onPopState}
            />
          }
        />
        <Route
          path={removePrefix(
            routes.client.emailMarketingV1SecretDiscountPaymentModal()
          )}
          element={
            <PaymentPage
              funnel={ELocalesPlacement.EmailMarketingCompatibilityV1}
              paymentPlacement="main_secret_discount"
              onError={onPaymentErrorDiscount}
              onSuccess={onPaymentSuccessDiscount}
            />
          }
        />
      </Route>
      <Route
        element={
          <div className={styles.container}>
            <Outlet />
          </div>
        }
      >
        <Route
          path={removePrefix(routes.client.emailMarketingV1Landing())}
          element={<MarketingLanding />}
        />
        <Route
          path={removePrefix(routes.client.emailMarketingV1SpecialOffer())}
          element={<SpecialOffer />}
        />
        <Route
          path={removePrefix(routes.client.emailMarketingV1SaveOff())}
          element={<SaveOff />}
        />
        <Route
          path={removePrefix(routes.client.emailMarketingV1SecretDiscount())}
          element={<SecretDiscount />}
        />
        <Route path="*" element={<NotFoundPage />} />
      </Route>
    </Routes>
  );
}

export default MarketingLandingV1Routes;
