import React, { useEffect, useState } from "react";
import {
  QuestionSection,
  QuestionSectionAction,
  QuestionSectionWithAnalysis,
  TitleAndSubtitle,
} from "../style";
import Response from "../components/response";
import AnalysisButton from "../components/analysisButton";
import useReportUtils from "../../hooks/useRreportUtils";
import QuestionDropdown from "../components/questionDropdown";
import ResponseAudioV2 from "../components/responseAudio.v2";
import useTranslation from "../../../../hooks/useTranslation.jsx";
import wordStore from "../../../../mc/wordStore.json";
const EssayUnscripted = ({
  data,
  aiData,
  setToggle,
  answerData,
  isPersonal,
  analysisLoading,
}) => {
  const { findText } = useTranslation(wordStore);
  const [analysis, setAnalysis] = useState(null);
  const { setupAnalysis, getTrabScriptData } = useReportUtils();
  useEffect(() => {
    if (data) {
      const models = setupAnalysis(
        aiData,
        answerData?.answer?.data?.data?.recording,
        answerData?.ai_score,
      );
      setAnalysis(models);
    }
  }, [data, answerData]);
  const transcript = getTrabScriptData(analysis?.models);
  return (
    <QuestionSection>
      <QuestionDropdown>
        <>
          <li>
            <h3>{findText("Instruction")}</h3>
            <p>{data?.question_data?.instruction}</p>
          </li>
          <li>
            <h3>{findText("Essay topic")}</h3>
            <p>{data?.question_data?.question_title}</p>
          </li>
        </>
      </QuestionDropdown>

      <QuestionSectionWithAnalysis>
        {!analysisLoading && (
          <Response
            title=""
            response={
              analysis?.models?.length > 0 && transcript?.length > 0
                ? transcript
                : undefined
            }
            isAiWordFlow
            isPersonal={isPersonal}
            defaultError={!answerData?.answer?.data?.data?.recording}
          />
        )}
        <QuestionSectionAction>
          {answerData?.answer?.data?.data && (
            <ResponseAudioV2
              key={answerData?.answer?.data?.data?.recording?.id}
              streamUrl={answerData?.answer?.data?.data?.recording?.url}
              url={
                answerData?.answer?.data?.data?.recording?.uploaded_media_url
              }
              audioData={answerData?.answer?.data?.data?.recording}
            />
          )}
          <AnalysisButton
            loading={analysisLoading}
            disabled={!analysis?.models?.length > 0 || analysisLoading}
            onClick={() => {
              setToggle(analysis);
            }}
          />
        </QuestionSectionAction>
      </QuestionSectionWithAnalysis>
    </QuestionSection>
  );
};
export default EssayUnscripted;
