import { useNavigate } from "react-router-dom";
import { useUpdateStep } from "@/components/ChatsPath/hooks/useUpdateStep";
import routes from "@/routes";
import { IAnswer } from "@/components/ChatsPath/data";
import Answers from "@/components/ChatsPath/components/Answers";
import InputAnswerModal from "@/components/ChatsPath/components/InputAnswerModal";
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { actions, selectors } from "@/store";
import { yourGoalAnswers } from "@/components/ChatsPath/data/yourGoalAnswers";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function YourGoal() {
  const { translate } = useTranslations(ELocalesPlacement.Chats);
  useUpdateStep(10);
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { goal } = useSelector(selectors.selectAnswers);

  const [answer, setAnswer] = useState("");

  const [open, setOpen] = useState(false);

  const handleOpen = () => setOpen(true);
  const handleClose = () => setOpen(false);

  const handleNext = () => {
    navigate(routes.client.chatsQuizSomethingWorrying());
  };

  const answers: IAnswer[] = yourGoalAnswers[goal]?.map((answer) => {
    return {
      ...answer,
      name: translate(answer.name as string),
      onClick: answer.value === "other" ? handleOpen : handleNext,
    };
  });

  const handleChangeAnswer = (event: React.ChangeEvent<HTMLInputElement>) => {
    setAnswer(event.target.value);
  };

  const handleModalNext = () => {
    dispatch(
      actions.chats.updateAnswers({
        yourGoal: answer,
      })
    );
    handleNext();
  };

  return (
    <>
      <InputAnswerModal
        title={translate("/quiz/yourGoal.modal_title")}
        placeholder={translate("/quiz/yourGoal.modal_placeholder")}
        buttonText={translate("/quiz/yourGoal.modal_button")}
        open={open}
        onClose={handleClose}
        inputProps={{
          value: answer,
          onChange: handleChangeAnswer,
        }}
        buttonProps={{
          disabled: !answer.length,
          onClick: handleModalNext,
        }}
      />
      <h2 className={`chats-answers-title`}>
        {translate("/quiz/yourGoal.title")}
      </h2>
      <Answers answers={answers} />
    </>
  );
}

export default YourGoal;
