import { useState } from "react";
import { useParams } from "react-router-dom";
import { Box, Typography } from "@mui/material";
import ButtonsOptions from "../ButtonsOptions";
import CustomNumberField from "@ui_kit/CustomNumberField";
import SwitchSlider from "./switchSlider";
import { questionStore, updateQuestionsList } from "@root/questions";

interface Props {
  totalIndex: number;
}

export default function SliderOptions({ totalIndex }: Props) {
  const [switchState, setSwitchState] = useState("setting");
  const [stepError, setStepError] = useState("");
  const quizId = Number(useParams().quizId);
  const { listQuestions } = questionStore();

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

  return (
    <>
      <Box
        sx={{
          width: "100%",
          maxWidth: "640px",
          display: "flex",
          padding: "20px",
          flexDirection: "column",
          gap: "20px",
        }}
      >
        <Box sx={{ gap: "10px", display: "flex", flexDirection: "column" }}>
          <Typography>Выбор значения из диапазона</Typography>
          <Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}>
            <CustomNumberField
              placeholder={"0"}
              min={0}
              max={99}
              value={
                listQuestions[quizId][totalIndex].content.range.split("—")[0]
              }
              onChange={({ target }) => {
                const clonContent = listQuestions[quizId][totalIndex].content;
                clonContent.range = `${target.value}—${
                  listQuestions[quizId][totalIndex].content.range.split("—")[1]
                }`;
                updateQuestionsList(quizId, totalIndex, {
                  content: clonContent,
                });
              }}
              onBlur={({ target }) => {
                const min = Number(target.value);
                const max = Number(
                  listQuestions[quizId][totalIndex].content.range.split("—")[1]
                );

                if (min >= max) {
                  const clonContent = listQuestions[quizId][totalIndex].content;
                  clonContent.range = `${max - 1 >= 0 ? max - 1 : 0}—${
                    listQuestions[quizId][totalIndex].content.range.split(
                      "—"
                    )[1]
                  }`;
                  updateQuestionsList(quizId, totalIndex, {
                    content: clonContent,
                  });
                }
              }}
            />
            <Typography>—</Typography>
            <CustomNumberField
              placeholder={"100"}
              min={0}
              max={100}
              value={
                listQuestions[quizId][totalIndex].content.range.split("—")[1]
              }
              onChange={({ target }) => {
                const clonContent = listQuestions[quizId][totalIndex].content;
                clonContent.range = `${
                  listQuestions[quizId][totalIndex].content.range.split("—")[0]
                }—${target.value}`;
                updateQuestionsList(quizId, totalIndex, {
                  content: clonContent,
                });
              }}
              onBlur={({ target }) => {
                const min = Number(
                  listQuestions[quizId][totalIndex].content.range.split("—")[0]
                );
                const max = Number(target.value);

                if (max <= min) {
                  const clonContent = listQuestions[quizId][totalIndex].content;
                  clonContent.range = `${
                    listQuestions[quizId][totalIndex].content.range.split(
                      "—"
                    )[0]
                  }—${min + 1 >= 100 ? 100 : min + 1}`;
                  updateQuestionsList(quizId, totalIndex, {
                    content: clonContent,
                  });
                }
              }}
            />
          </Box>
        </Box>
        <Box
          sx={{
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between",
            gap: "50px",
          }}
        >
          <Box sx={{ width: "100%" }}>
            <Typography>Начальное значение</Typography>
            <CustomNumberField
              placeholder={"50"}
              min={0}
              value={String(listQuestions[quizId][totalIndex].content.start)}
              onChange={({ target }) => {
                const clonContent = listQuestions[quizId][totalIndex].content;
                clonContent.start = Number(target.value);
                updateQuestionsList(quizId, totalIndex, {
                  content: clonContent,
                });
              }}
              onBlur={({ target }) => {
                const start = Number(target.value);
                const min = Number(
                  listQuestions[quizId][totalIndex].content.range.split("—")[0]
                );

                if (start < min) {
                  updateQuestionsList(quizId, totalIndex, {
                    content: {
                      ...listQuestions[quizId][totalIndex].content,
                      start: min,
                    },
                  });
                }
              }}
            />
          </Box>
          <Box sx={{ width: "100%" }}>
            <Typography>Шаг</Typography>
            <CustomNumberField
              min={0}
              max={100}
              placeholder={"1"}
              error={stepError}
              value={String(listQuestions[quizId][totalIndex].content.step)}
              onChange={({ target }) => {
                const clonContent = listQuestions[quizId][totalIndex].content;
                clonContent.step = Number(target.value);
                updateQuestionsList(quizId, totalIndex, {
                  content: clonContent,
                });
              }}
              onBlur={({ target }) => {
                const min = Number(
                  listQuestions[quizId][totalIndex].content.range.split("—")[0]
                );
                const max = Number(
                  listQuestions[quizId][totalIndex].content.range.split("—")[1]
                );
                const range = max - min;
                const step = Number(target.value);

                if (step > max) {
                  updateQuestionsList(quizId, totalIndex, {
                    content: {
                      ...listQuestions[quizId][totalIndex].content,
                      step: max,
                    },
                  });
                }

                if (range % step) {
                  setStepError(
                    `Шаг должен делить без остатка диапазон ${max} - ${min} = ${
                      max - min
                    }`
                  );
                } else {
                  setStepError("");
                }
              }}
            />
          </Box>
        </Box>
      </Box>
      <ButtonsOptions
        switchState={switchState}
        SSHC={SSHC}
        totalIndex={totalIndex}
      />
      <SwitchSlider switchState={switchState} totalIndex={totalIndex} />
    </>
  );
}
