import { useState } from "react";
import { useParams } from "react-router-dom";
import { Box, Typography, Tooltip, useTheme, useMediaQuery } from "@mui/material";
import { useDebouncedCallback } from "use-debounce";

import CustomTextField from "@ui_kit/CustomTextField";
import ButtonsOptions from "../ButtonsOptions";
import SwitchTextField from "./switchTextField";

import { questionStore, updateQuestionsList } from "@root/questions";

import InfoIcon from "../../../assets/icons/InfoIcon";

interface Props {
  totalIndex: number;
}
export default function OwnTextField({ totalIndex }: Props) {
  const [switchState, setSwitchState] = useState("setting");
  const quizId = Number(useParams().quizId);
  const { listQuestions } = questionStore();
  const theme = useTheme();
  const isWrapperColumn = useMediaQuery(theme.breakpoints.down(980));
  const isMobile = useMediaQuery(theme.breakpoints.down(790));
  const isFigmaTablte = useMediaQuery(theme.breakpoints.down(990));
  const debounced = useDebouncedCallback((value) => {
    const clonContent = listQuestions[quizId][totalIndex].content;
    clonContent.placeholder = value;
    updateQuestionsList(quizId, totalIndex, { content: clonContent });
  }, 1000);

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

  return (
    <>
      <Box
        sx={{
          width: "auto",
          maxWidth: "745px",
          display: "flex",
          pb: "20px",
          pl: "20px",
          pr: "20px",
          flexDirection: "column",
          gap: isMobile ? "18px" : "20px",
        }}
      >
        <CustomTextField
          placeholder={"Пример ответа"}
          text={listQuestions[quizId][totalIndex].content.placeholder}
          onChange={({ target }) => debounced(target.value)}
          sx={{ maxWidth: "640px", width: "100%" }}
        />
        <Box sx={{ display: "flex", alignItems: "center", gap: "12px" }}>
          <Typography
            sx={{
              fontWeight: 400,
              fontSize: "16px",
              lineHeight: "18.96px",
              color: theme.palette.grey2.main,
            }}
          >
            Пользователю будет дано поле для ввода значения
          </Typography>
          <Tooltip title="Будет использоваться для ввода значения." placement="top">
            <Box>
              <InfoIcon />
            </Box>
          </Tooltip>
        </Box>
      </Box>
      <ButtonsOptions switchState={switchState} SSHC={SSHC} totalIndex={totalIndex} />
      <SwitchTextField switchState={switchState} totalIndex={totalIndex} />
    </>
  );
}
