import moment from "moment";
import { DatePicker } from "@mui/x-date-pickers";
import { Box, Typography, useTheme } from "@mui/material";

import { useQuizViewStore, updateAnswer } from "@stores/quizView/store";

import type { QuizQuestionDate } from "../../../model/questionTypes/date";
import CalendarIcon from "@icons/CalendarIcon";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";

import { quizThemes } from "@utils/themes/Publication/themePublication";
import { useQuizData } from "@utils/hooks/useQuizData";
import { useQuizId } from "../../../contexts/QuizIdContext";

type DateProps = {
    currentQuestion: QuizQuestionDate;
};

export const Date = ({ currentQuestion }: DateProps) => {
    const theme = useTheme();
    const qid = useQuizId();
    const { settings } = useQuizData();
    const { answers } = useQuizViewStore();
    const answer = answers.find(
        ({ questionId }) => questionId === currentQuestion.id
    )?.answer as string;
    const currentAnswer = moment(answer) || moment();

    return (
        <Box>
            <Typography variant="h5" color={theme.palette.text.primary}>
                {currentQuestion.title}
            </Typography>
            <Box
                sx={{
                    display: "flex",
                    flexDirection: "column",
                    width: "100%",
                    marginTop: "20px",
                }}
            >
                <DatePicker
                    slots={{
                        openPickerIcon: () => (
                            <CalendarIcon
                                sx={{
                                    "& path": { stroke: theme.palette.primary.main },
                                    "& rect": { stroke: theme.palette.primary.main },
                                }}
                            />
                        ),
                    }}
                    value={currentAnswer}
                    onChange={async (date) => {
                        console.log(date);
                        if (!date) {
                            return;
                        }

                        try {
                            await sendAnswer({
                                questionId: currentQuestion.id,
                                body: moment(date).format("YYYY.MM.DD"),
                                qid,
                            });

                            updateAnswer(
                                currentQuestion.id,
                                date,
                                0
                            );
                        } catch (e) {
                            enqueueSnackbar("ответ не был засчитан");
                        }
                    }}
                    slotProps={{
                        openPickerButton: {
                            sx: {
                                p: 0,
                            },
                            "data-cy": "open-datepicker",
                        },
                        layout: {
                            sx: { backgroundColor: theme.palette.background.default },
                        },
                    }}
                    sx={{
                        "& .MuiInputBase-root": {
                            backgroundColor: quizThemes[settings.cfg.theme].isLight
                                ? "white"
                                : theme.palette.background.default,
                            borderRadius: "10px",
                            maxWidth: "250px",
                            pr: "22px",
                            "& input": {
                                py: "11px",
                                pl: "20px",
                                lineHeight: "19px",
                            },
                            "& fieldset": {
                                borderColor: "#9A9AAF",
                            },
                        },
                    }}
                />
            </Box>
        </Box>
    );
};
