import styles from "./styles.module.scss";
import Title from "@/components/Title";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";
import { getZodiacSignByDate } from "@/services/zodiac-sign";
import { useSelector } from "react-redux";
import { selectors } from "@/store";
import Button from "../../components/Button";
import { useNavigate } from "react-router-dom";
import routes from "@/routes";
import { useMemo } from "react";
import { EUnleashFlags, useUnleash } from "@/hooks/ab/unleash/useUnleash";
import Loader, { LoaderColor } from "@/components/Loader";
import { ELottieKeys, useLottie } from "@/hooks/lottie/useLottie";

function HeadOrHeartResult() {
  const navigate = useNavigate();
  const { translate } = useTranslations(ELocalesPlacement.V1);
  const { headOrHeart } = useSelector(selectors.selectPalmistryV1Answers);
  const { gender } = useSelector(selectors.selectQuestionnaire);

  const { isReady, variant } = useUnleash({
    flag: EUnleashFlags.headOrHeartResultPagePalmistryV1,
  });

  useLottie({
    preloadKey: ELottieKeys.letScan
  })

  const birthdate = useSelector(selectors.selectBirthdate);
  const zodiacSign = getZodiacSignByDate(birthdate);

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

  const handleNext = () => {
    // navigate(`${routes.client.palmistryV1RelateFollowing()}/1`);
    navigate(routes.client.palmistryV1LetScan());
  };

  const imageUrl = useMemo(() => {
    const imageName = headOrHeart === "depends" ? "both" : headOrHeart;
    return `/v1/palmistry/head-or-heart-result/${imageName}.svg`;
  }, [headOrHeart]);

  if (!isReady) return <Loader color={LoaderColor.Black} />;

  return (
    <section className={styles.container}>
      {variant === "v1" && <img
        src={imageUrl}
        alt="result image"
        className={styles.image}
      />}
      <Title variant="h1" className={styles.title}>
        {headOrHeart === "both" &&
          translate("/both.title", {
            wonderful: (
              <b className={styles.highlite}>{translate("/both.wonderful")}</b>
            ),
            zodiacSign: (
              <b className={styles.highlite}>
                {translate(`zodiac_signs.${zodiacSign?.toLowerCase()}`)}
              </b>
            ),
            gender: translate(gender?.toLowerCase()).toLowerCase()
          })}
        {headOrHeart === "head" &&
          translate("/with-head.title", {
            zodiacSign: (
              <b className={styles.highlite}>
                {translate(`zodiac_signs.${zodiacSign?.toLowerCase()}`)}
              </b>
            ),
            gender: translate(gender?.toLowerCase()).toLowerCase()
          })}
        {headOrHeart === "heart" &&
          translate("/with-heart.title", {
            zodiacSign: (
              <b className={styles.highlite}>
                {translate(`zodiac_signs.${zodiacSign?.toLowerCase()}`)}
              </b>
            ),
            gender: translate(gender?.toLowerCase()).toLowerCase()
          })}
        {headOrHeart === "depends" &&
          translate("/depends.title", {
            zodiacSign: (
              <b className={styles.highlite}>
                {translate(`zodiac_signs.${zodiacSign?.toLowerCase()}`)}
              </b>
            ),
            gender: translate(gender?.toLowerCase()).toLowerCase()
          })
        }
      </Title>
      <div className={styles["buttons-container"]}>
        <Button
          className={`${styles.button} ${styles["back-button"]}`}
          onClick={handleBack}
        >
          {translate("back")}
        </Button>
        <Button
          className={`${styles.button} ${styles["next-button"]}`}
          onClick={handleNext}
        >
          {translate("next")}
        </Button>
      </div>
    </section>
  );
}

export default HeadOrHeartResult;
