import { Box, Button, LinearProgress, Paper, Typography } from "@mui/material";
import { Question, questionStore } from "@root/questions";
import { decrementCurrentQuestionIndex, incrementCurrentQuestionIndex, useQuizPreviewStore } from "@root/quizPreview";
import { useParams } from "react-router-dom";
import ArrowLeft from "../../assets/icons/questionsPage/arrowLeft";
import Variant from "./QuizPreviewQuestionTypes/Variant";
import { FC, useEffect } from "react";
import Images from "./QuizPreviewQuestionTypes/Images";
import Varimg from "./QuizPreviewQuestionTypes/Varimg";
import Emoji from "./QuizPreviewQuestionTypes/Emoji";
import Text from "./QuizPreviewQuestionTypes/Text";
import Select from "./QuizPreviewQuestionTypes/Select";
import Date from "./QuizPreviewQuestionTypes/Date";
import Number from "./QuizPreviewQuestionTypes/Number";
import File from "./QuizPreviewQuestionTypes/File";
import Page from "./QuizPreviewQuestionTypes/Page";
import Rating from "./QuizPreviewQuestionTypes/Rating";


type QuizQuestionType = "variant" | "images" | "varimg" | "emoji" | "text" | "select" | "date" | "number" | "file" | "page" | "rating";

const QuestionPreviewComponentByType: Record<QuizQuestionType, FC<{ question: Question; }>> = {
    variant: Variant,
    images: Images,
    varimg: Varimg,
    emoji: Emoji,
    text: Text,
    select: Select,
    date: Date,
    number: Number,
    file: File,
    page: Page,
    rating: Rating,
};

export default function QuizPreviewLayout() {
    const quizId = useParams().quizId ?? 0;
    const listQuestions = questionStore(state => state.listQuestions);
    const currentQuizStep = useQuizPreviewStore(state => state.currentQuestionIndex);

    const quizQuestions: Question[] | undefined = listQuestions[quizId];
    const maxCurrentQuizStep = quizQuestions?.length > 0 ? quizQuestions.length - 1 : 0;
    const currentProgress = Math.floor((currentQuizStep / maxCurrentQuizStep) * 100);
    
    const currentQuestion = quizQuestions[currentQuizStep];
    const QuestionComponent = currentQuestion
        ? QuestionPreviewComponentByType[currentQuestion.type as QuizQuestionType]
        : null;

    const questionElement = QuestionComponent
        ? <QuestionComponent key={currentQuestion.id} question={currentQuestion} />
        : null;

    useEffect(function resetCurrentQuizStep() {
        if (currentQuizStep > maxCurrentQuizStep) {
            decrementCurrentQuestionIndex();
        }
    }, [currentQuizStep, maxCurrentQuizStep]);

    return (
        <Paper sx={{
            height: "100%",
            display: "flex",
            flexDirection: "column",
            flexGrow: 1,
            borderRadius: "12px",
            pointerEvents: "auto",
        }}>
            <Box sx={{
                p: "16px",
                whiteSpace: "break-spaces",
                overflowY: "auto",
                flexGrow: 1,
            }}>
                {questionElement}
            </Box>
            <Box sx={{
                mt: "auto",
                p: "16px",
                display: "flex",
                borderTop: "1px solid #E3E3E3",
                alignItems: "center",
            }}>
                <Box sx={{
                    flexGrow: 1,
                    display: "flex",
                    flexDirection: "column",
                    gap: 1,
                }}>
                    <Typography>
                        {quizQuestions.length > 0
                            ? `Вопрос ${currentQuizStep + 1} из ${quizQuestions.length}`
                            : "Нет вопросов"
                        }
                    </Typography>
                    {quizQuestions.length > 0 &&
                        <LinearProgress
                            variant="determinate"
                            value={currentProgress}
                            sx={{
                                "&.MuiLinearProgress-colorPrimary": {
                                    backgroundColor: "fadePurple.main",
                                },
                                "& .MuiLinearProgress-barColorPrimary": {
                                    backgroundColor: "brightPurple.main",
                                },
                            }}
                        />
                    }
                </Box>
                <Box sx={{
                    ml: 2,
                    display: "flex",
                    gap: 1,
                }}>
                    <Button
                        variant="outlined"
                        onClick={decrementCurrentQuestionIndex}
                        disabled={currentQuizStep === 0}
                        sx={{ px: 1, minWidth: 0 }}
                    >
                        <ArrowLeft />
                    </Button>
                    <Button
                        variant="contained"
                        onClick={() => incrementCurrentQuestionIndex(maxCurrentQuizStep)}
                        disabled={currentQuizStep >= maxCurrentQuizStep}
                    >Далее</Button>
                </Box>
            </Box>
        </Paper>
    );
}
