import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
import { Box, ButtonBase, Typography, useTheme } from "@mui/material";
import { uploadQuestionImage } from "@root/questions/actions";
import { CropModal } from "@ui_kit/Modal/CropModal";
import UploadBox from "@ui_kit/UploadBox";
import { type DragEvent } from "react";
import UploadIcon from "../../../assets/icons/UploadIcon";
import { UploadImageModal } from "./UploadImageModal";
import { setCropModal } from "@root/cropModal";
import { useCurrentQuiz } from "@root/quizes/hooks";
import { useDisclosure } from "../../../utils/useDisclosure";


type UploadImageProps = {
    question: AnyTypedQuizQuestion;
};

export default function UploadImage({ question }: UploadImageProps) {
    const theme = useTheme();
    const quizQid = useCurrentQuiz()?.qid;
    const [isCropModalOpen, openCropModal, closeCropModal] = useDisclosure();
    const [isImageUploadOpen, openImageUploadModal, closeImageUploadModal] = useDisclosure();

    const handleImageUpload = async (files: FileList | null) => {
        if (!files?.length) return;

        const url = await uploadQuestionImage(question.id, quizQid, files[0], (question, url) => {
            question.content.back = url;
            question.content.originalBack = url;
        });
        closeImageUploadModal();
        setCropModal(files[0], url);
        openCropModal();
    };

    const handleDrop = (event: DragEvent<HTMLDivElement>) => {
        event.preventDefault();
        event.stopPropagation();

        handleImageUpload(event.dataTransfer.files);
    };

    function handleCropModalSaveClick(imageBlob: Blob) {
        uploadQuestionImage(question.id, quizQid, imageBlob, (question, url) => {
            question.content.back = url;
        });
    }

    return (
        <Box sx={{ padding: "20px" }}>
            <Typography
                sx={{
                    fontWeight: 500,
                    color: theme.palette.grey3.main,
                    mt: "11px",
                    mb: "14px",
                }}
            >
                Загрузить изображение
            </Typography>
            <ButtonBase
                onClick={openImageUploadModal}
                sx={{
                    width: "100%",
                    maxWidth: "260px",
                    height: "120px",
                }}
            >
                {question.content.back ?
                    <img
                        src={question.content.back}
                        alt="question background"
                        style={{
                            width: "100%",
                            height: "100%",
                            objectFit: "scale-down",
                            display: "block",
                        }}
                    />
                    :
                    <UploadBox
                        handleDrop={handleDrop}
                        sx={{ maxWidth: "260px" }}
                        icon={<UploadIcon />}
                        text="5 MB максимум"
                    />
                }
            </ButtonBase>
            <UploadImageModal isOpen={isImageUploadOpen} onClose={closeImageUploadModal} imgHC={handleImageUpload} />
            <CropModal isOpen={isCropModalOpen} onClose={closeCropModal} onSaveImageClick={handleCropModalSaveClick} />
        </Box>
    );
}
