import React, { useState } from "react";
import styled from "styled-components";
import WarningIcon from "../icons/warning";
import classNames from "classnames";
import TranslateDropdown from "./translateDropdown";
import useLanguages from "../api/useLanguage";
import useTranslation from "../../../../hooks/useTranslation.jsx";
import wordStore from "../../../../mc/wordStore.json";
const EvalutionFeedback = ({
  title = "Feedback",
  hideWarning = false,
  highlightText = "",
  withBorder = false,
  isPersonal,
}) => {
  const { findText } = useTranslation(wordStore);
  const [text, setWord] = useState(null);
  const { translateLangData, handleTranslate } = useLanguages();
  return (
    <Container
      className={classNames({
        "with-border": withBorder,
      })}
    >
      <Header>
        <p>{title}</p>
        {!hideWarning ? (
          <div className="highlight_warning">
            <WarningIcon />
            <p>{findText("Introduced words are highlighted in green")}</p>
          </div>
        ) : isPersonal ? (
          <TranslateDropdown
            loading={translateLangData?.loading}
            onChange={async (lang) => {
              const code = lang?.code;
              if (code === "en" || !lang) {
                setWord(null);
                return;
              }
              const res = await handleTranslate({
                texts: [highlightText],
                source_language: "en",
                target_language: code,
              });
              if (res?.data) {
                setWord(res?.data?.data?.[0]?.translation);
              } else {
                setWord(null);
              }
            }}
          />
        ) : null}
      </Header>
      <HighlightedText
        dangerouslySetInnerHTML={{
          __html: text || highlightText,
        }}
      />
    </Container>
  );
};
export default EvalutionFeedback;
const Container = styled.div`
  &.with-border {
    padding-bottom: 15px;
    border-bottom: 1px solid #dfe5e5;
  }
`;
const Header = styled.div`
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  > p {
    color: #7c8080;
    font-size: 16px;
    font-weight: 600;
  }
  > .highlight_warning {
    background-color: #fff9e8;
    display: flex;
    align-items: center;
    height: 34px;
    padding: 0 20px;
    border-radius: 20px;
    gap: 8px;
    p {
      color: #7c8080;
      font-size: 14px;
    }
  }
`;
export const HighlightedText = styled.p`
  font-size: 14px;
  span {
    color: #30d468;
  }
`;
