import Title from "@/components/Title";
import styles from "./styles.module.scss";
import Answer from "../../components/Answer";
import { useDispatch, useSelector } from "react-redux";
import { actions, selectors } from "@/store";
import { sleep } from "@/services/date";
import { useNavigate } from "react-router-dom";
import routes from "@/routes";
import { answerTimeOut } from "../../data";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";
import { useMemo } from "react";
import WaterSVG from "../../images/SVG/Water";
import FireSVG from "../../images/SVG/Fire";
import AirSVG from "../../images/SVG/Air";
import EarthSVG from "../../images/SVG/Earth";
import { ELottieKeys, useLottie } from "@/hooks/lottie/useLottie";
import { IAnswersSessionCompatibilityV2 } from "@/api/resources/Session";
import { useSession } from "@/hooks/session/useSession";
import { ESourceAuthorization } from "@/api/resources/User";
import DarknessSVG from "../../images/SVG/Darkness";
import LightSVG from "../../images/SVG/Light";

function ElementResonates() {
  const { translate } = useTranslations(ELocalesPlacement.CompatibilityV2);
  const { updateSession } = useSession();
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { elementResonates } = useSelector(selectors.selectCompatibilityV2Answers);
  useLottie({
    preloadKey: ELottieKeys.letScan,
  });

  const answers: {
    id: IAnswersSessionCompatibilityV2["element_resonates"];
    title: string;
    Icon: JSX.Element;
  }[] = useMemo(
    () => [
      {
        id: "air",
        title: translate("/element-resonates.answer3"),
        Icon: <AirSVG />,
      },
      {
        id: "water",
        title: translate("/element-resonates.answer1"),
        Icon: <WaterSVG />,
      },
      {
        id: "fire",
        title: translate("/element-resonates.answer2"),
        Icon: <FireSVG />,
      },
      {
        id: "earth",
        title: translate("/element-resonates.answer4"),
        Icon: <EarthSVG />,
      },
      {
        id: "light",
        title: translate("/element-resonates.answer5"),
        Icon: <LightSVG />,
      },
      {
        id: "darkness",
        title: translate("/element-resonates.answer6"),
        Icon: <DarknessSVG />,
      },
    ],
    [translate]
  );

  const handleClick = async (id: IAnswersSessionCompatibilityV2["element_resonates"]) => {
    dispatch(actions.compatibilityV2Answers.update({ elementResonates: id }));
    updateSession({
      answers: {
        element_resonates: id,
      },
    }, ESourceAuthorization["aura.compatibility.v2"]);
    if (id !== elementResonates) await sleep(answerTimeOut);
    navigate(routes.client.compatibilityV2FavoriteColor());
  };

  return (
    <div className={styles.container}>
      <Title variant="h2" className={styles.title}>
        {translate("/element-resonates.title")}
      </Title>
      {answers.map(({ Icon, ...answers }, index) => (
        <Answer
          key={index}
          answer={answers}
          isSelected={elementResonates === answers.id}
          onClick={() => handleClick(answers.id)}
          className={styles.answer}
        >
          {Icon}
          <p>{answers.title}</p>
        </Answer>
      ))}
    </div>
  );
}

export default ElementResonates;
