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

import TropfyIcon from "../../../assets/icons/questionsPage/tropfyIcon";
import FlagIcon from "../../../assets/icons/questionsPage/FlagIcon";
import HeartIcon from "../../../assets/icons/questionsPage/heartIcon";
import LikeIcon from "../../../assets/icons/questionsPage/likeIcon";
import LightbulbIcon from "../../../assets/icons/questionsPage/lightbulbIcon";
import HashtagIcon from "../../../assets/icons/questionsPage/hashtagIcon";
import StarIconMini from "../../../assets/icons/questionsPage/StarIconMini";

interface Props {
  totalIndex: number;
}

export type ButtonRatingFrom = {
  name: string;
  icon: JSX.Element;
};

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 buttonRatingForm: ButtonRatingFrom[] = [
    { name: "star", icon: <StarIconMini color={theme.palette.grey2.main} /> },
    { name: "trophie", icon: <TropfyIcon color={theme.palette.grey2.main} /> },
    { name: "flag", icon: <FlagIcon color={theme.palette.grey2.main} /> },
    { name: "heart", icon: <HeartIcon color={theme.palette.grey2.main} /> },
    { name: "like", icon: <LikeIcon color={theme.palette.grey2.main} /> },
    {
      name: "bubble",
      icon: <LightbulbIcon color={theme.palette.grey2.main} />,
    },
    { name: "hashtag", icon: <HashtagIcon color={theme.palette.grey2.main} /> },
  ];

  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: "25px" }}>
          {Array.from(
            { length: listQuestions[quizId][totalIndex].content.steps },
            (_, index) => index
          ).map((itemNumber) => (
            <Box
              {...(itemNumber === 0 ||
              itemNumber === listQuestions[quizId][totalIndex].content.steps - 1
                ? {
                    onClick: () => {
                      updateQuestionsList(quizId, totalIndex, {
                        content: {
                          ...listQuestions[quizId][totalIndex].content,
                          ratingExpanded: true,
                        },
                      });
                    },
                    sx: {
                      cursor: "pointer",
                      transform: "scale(1.5)",
                      ":hover": {
                        transform: "scale(1.7)",
                        transition: "0.2s",
                      },
                    },
                  }
                : { sx: { transform: "scale(1.5)" } })}
            >
              {
                buttonRatingForm.find(
                  ({ name }) =>
                    listQuestions[quizId][totalIndex].content.form === name
                )?.icon
              }
            </Box>
          ))}
        </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} />
    </>
  );
}
