import Title from "@/components/Title";
import styles from "./styles.module.css";
import MainButton from "@/components/MainButton";
import { useNavigate } from "react-router-dom";
import routes from "@/routes";
import { useSelector } from "react-redux";
import { selectors } from "@/store";
import Header from "../../components/Header";
import { textVariables } from "../../data/textVariables";
import { DotLottieReact } from "@lottiefiles/dotlottie-react";
import { ELottieKeys, useLottie } from "@/hooks/lottie/useLottie";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function QuestionnaireIntermediatePage() {
  const navigate = useNavigate();
  const { translate } = useTranslations(ELocalesPlacement.V1);
  const { problems } = useSelector(selectors.selectQuestionnaire);
  const path = window.location.href.split("/").filter(Boolean).pop();
  const backgroundImages =
    textVariables[path as keyof typeof textVariables]?.backgroundImages;
  const backgroundImage =
    backgroundImages && backgroundImages[problems]
      ? backgroundImages[problems]
      : "/couple_holding_hands_1.webp";

  const { animationData } = useLottie({
    loadKey: ELottieKeys.key,
  });

  const handleBack = () => {
    navigate(-1);
  };

  const handleNext = () => {
    if (path === "works-traits") {
      return navigate(
        `${routes.client.questionnaireV1()}/personalityTraits/relateToStatement`
      );
    }
    navigate(`${routes.client.questionnaireV1()}/partnerProfile/partnerGender`);
  };

  return (
    <section className={`${styles.page} page`}>
      <div
        className={styles["background-image"]}
        style={{
          backgroundImage: `url(${backgroundImage})`,
        }}
      />
      <Header
        isBackButtonVisible={false}
        classNameTitle={styles["header-title"]}
      />
      <div className={`${styles["lottie-animation"]} ym-hide-content`}>
        {animationData && (
          <DotLottieReact data={animationData} autoplay loop={false} />
        )}
      </div>
      <div>
        {path && (
          <Title variant="h1" className={styles.title}>
            {translate(
              (textVariables[path as keyof typeof textVariables].titles[
                problems
              ] || "") as string
            )}
          </Title>
        )}
        {textVariables[path as keyof typeof textVariables].description}
      </div>
      <div className={styles["buttons-container"]}>
        <MainButton
          className={`${styles.button} ${styles["back-button"]}`}
          onClick={handleBack}
        >
          {translate("back")}
        </MainButton>
        <MainButton
          className={`${styles.button} ${styles["next-button"]}`}
          onClick={handleNext}
        >
          {translate("next")}
        </MainButton>
      </div>
    </section>
  );
}

export default QuestionnaireIntermediatePage;
