import { useState } from "react";
import { useParams } from "react-router-dom";
import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
import ButtonsOptions from "../ButtonsOptions";
import RatingStar from "../../../assets/icons/questionsPage/ratingStar";
import SwitchRating from "./switchRating";
import { questionStore, updateQuestionsList } from "@root/questions";
import CustomTextField from "@ui_kit/CustomTextField";

interface Props {
  totalIndex: number;
}

export default function RatingOptions({ totalIndex }: Props) {
  const [switchState, setSwitchState] = useState("setting");
  const quizId = Number(useParams().quizId);
  const { listQuestions } = questionStore();
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down(790));

  const SSHC = (data: string) => {
    setSwitchState(data);
  };

  return (
    <>
      <Box
        sx={{
          width: isMobile ? "auto" : "100%",
          maxWidth: "310px",
          display: "flex",
          padding: "20px",
          flexDirection: "column",
          gap: "20px",
        }}
      >
        <Box sx={{ display: "flex", gap: isMobile ? "10px" : "15px" }}>
          <RatingStar
            onClick={() => {
              updateQuestionsList(quizId, totalIndex, {
                content: {
                  ...listQuestions[quizId][totalIndex].content,
                  ratingExpanded: true,
                },
              });
            }}
            sx={{
              cursor: "pointer",
              ":hover": {
                transform: "scale(1.1)",
                transition: "0.2s",
              },
            }}
          />
          <RatingStar />
          <RatingStar />
          <RatingStar />
          <RatingStar
            onClick={() => {
              updateQuestionsList(quizId, totalIndex, {
                content: {
                  ...listQuestions[quizId][totalIndex].content,
                  ratingExpanded: true,
                },
              });
            }}
            sx={{
              cursor: "pointer",
              ":hover": {
                transform: "scale(1.1)",
                transition: "0.2s",
              },
            }}
          />
        </Box>
        <Box
          sx={{
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between",
          }}
        >
          <Typography
            sx={{
              color: theme.palette.grey2.main,
              fontSize: "16px",
              fontWeight: 400,
            }}
          >
            Негативно
          </Typography>
          <Typography
            sx={{
              color: theme.palette.grey2.main,
              fontSize: "16px",
              fontWeight: 400,
            }}
          >
            Позитивно
          </Typography>
        </Box>
        {listQuestions[quizId][totalIndex].content.ratingExpanded &&
          (listQuestions[quizId][totalIndex].content.ratingDescription ? (
            <Typography>{listQuestions[quizId][totalIndex].content.ratingDescription}</Typography>
          ) : (
            <CustomTextField
              placeholder={"Описание"}
              text={listQuestions[quizId][totalIndex].content.ratingDescription}
              onKeyDown={({ target, key }) => {
                if (key === "Enter") {
                  const currentTarget = target as HTMLInputElement;

                  updateQuestionsList(quizId, totalIndex, {
                    content: {
                      ...listQuestions[quizId][totalIndex].content,
                      ratingDescription: currentTarget.value.substring(0, 20),
                    },
                  });
                }
              }}
              onBlur={({ target }) => {
                updateQuestionsList(quizId, totalIndex, {
                  content: {
                    ...listQuestions[quizId][totalIndex].content,
                    ratingDescription: target.value.substring(0, 20),
                  },
                });
              }}
            />
          ))}
      </Box>
      <ButtonsOptions switchState={switchState} SSHC={SSHC} totalIndex={totalIndex} />
      <SwitchRating switchState={switchState} totalIndex={totalIndex} />
    </>
  );
}
