import { useState, useEffect } from "react";
import {
  Box,
  FormControl,
  MenuItem,
  Select,
  SelectChangeEvent,
  Typography,
  useTheme,
} from "@mui/material";
import ButtonsOptions from "../ButtonsOptions";

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

import InfoIcon from "../../../assets/icons/InfoIcon";
import ArrowDown from "../../../assets/icons/ArrowDownIcon";
import SwitchUpload from "./switchUpload";

interface Props {
  totalIndex: number;
}

const DESIGN_TYPES = [
  { name: "Все типы файлов", value: "all" },
  { name: "Изображения", value: "picture" },
  { name: "Видео", value: "video" },
  { name: "Аудио", value: "audio" },
  { name: "Документ", value: "document" },
];

export default function UploadFile({ totalIndex }: Props) {
  const [switchState, setSwitchState] = useState("setting");
  const { listQuestions } = questionStore();
  const theme = useTheme();

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

  const handleChange = ({ target }: SelectChangeEvent) => {
    const clonContent = listQuestions[totalIndex].content;
    clonContent.type = target.value;
    updateQuestionsList(totalIndex, { content: clonContent });
  };

  useEffect(() => {
    const isTypeSetted = DESIGN_TYPES.find(
      ({ value }) => value === listQuestions[totalIndex].content.type
    );

    if (!isTypeSetted) {
      const clonContent = listQuestions[totalIndex].content;
      clonContent.type = DESIGN_TYPES[0].value;
      updateQuestionsList(totalIndex, { content: clonContent });
    }
  }, []);

  return (
    <>
      <Box
        sx={{
          width: "100%",
          maxWidth: "640px",
          display: "flex",
          padding: "20px",
          flexDirection: "column",
          gap: "20px",
        }}
      >
        <Box sx={{ gap: "10px", display: "flex" }}>
          <FormControl
            fullWidth
            size="small"
            sx={{
              width: "100%",
              minWidth: "200px",
              height: "48px",
            }}
          >
            <Select
              id="category-select"
              variant="outlined"
              value={listQuestions[totalIndex].content.type}
              displayEmpty
              onChange={handleChange}
              sx={{
                height: "48px",
                borderRadius: "8px",
                "& .MuiOutlinedInput-notchedOutline": {
                  border: `1px solid ${theme.palette.brightPurple.main} !important`,
                },
              }}
              MenuProps={{
                PaperProps: {
                  sx: {
                    mt: "8px",
                    p: "4px",
                    borderRadius: "8px",
                    border: "1px solid #EEE4FC",
                    boxShadow: "0px 8px 24px rgba(210, 208, 225, 0.4)",
                  },
                },
                MenuListProps: {
                  sx: {
                    py: 0,
                    display: "flex",
                    flexDirection: "column",
                    gap: "8px",
                    "& .Mui-selected": {
                      backgroundColor: theme.palette.background.default,
                      color: theme.palette.brightPurple.main,
                    },
                  },
                },
              }}
              inputProps={{
                sx: {
                  color: theme.palette.brightPurple.main,
                  display: "flex",
                  alignItems: "center",
                  px: "9px",
                  gap: "20px",
                },
              }}
              IconComponent={(props) => <ArrowDown {...props} />}
            >
              {DESIGN_TYPES.map(({ name, value }) => (
                <MenuItem
                  key={value}
                  value={value}
                  sx={{
                    display: "flex",
                    alignItems: "center",
                    gap: "20px",
                    p: "4px",
                    borderRadius: "5px",
                    color: theme.palette.grey2.main,
                  }}
                >
                  {name}
                </MenuItem>
              ))}
            </Select>
          </FormControl>
        </Box>
        <Box sx={{ display: "flex", alignItems: "center", gap: "12px" }}>
          <Typography
            sx={{
              fontWeight: 400,
              fontSize: "16px",
              lineHeight: "18.96px",
              color: theme.palette.grey2.main,
            }}
          >
            Пользователь может загрузить любой собственный файл
          </Typography>
          <InfoIcon />
        </Box>
      </Box>
      <ButtonsOptions
        switchState={switchState}
        SSHC={SSHC}
        totalIndex={totalIndex}
      />
      <SwitchUpload switchState={switchState} totalIndex={totalIndex} />
    </>
  );
}
