import React, {Dispatch, SetStateAction, useRef} from "react";
import {Alert, Button, Divider, Modal, ModalProps, Space} from "antd";
import {BotFileManagerFile} from "../../../Store/BotFileManager/Types";
import {getFileExt} from "../../../Util/getFileExt";
import {useTranslation} from "react-i18next";
import Mdicon from "../../Misc/Mdicon";
import PDFViewer from "../../Pages/FileMaganer/PDFViewer";
import TextReader from "../../Pages/FileMaganer/TextReader";
import {getBackendUrl} from "../../../Util/config";

interface IProps extends ModalProps {
    file: BotFileManagerFile | undefined,
    close: Dispatch<SetStateAction<boolean>>,
}

const FilePreview: React.FC<IProps> = React.memo((props: IProps) => {
    const {t} = useTranslation();
    const audioRef = useRef<HTMLAudioElement>(null);
    const videoRef = useRef<HTMLVideoElement>(null);

    const previewComponentHandler = (file: BotFileManagerFile | undefined) => {
        const fileType = getFileExt(file?.name);
        const url = getBackendUrl(`download/botFile/${file?.id}`);

        const mediaContainer = (media: "audio" | "video", fileUrl: string, mimeType?: string) => {
            switch (media) {
            case "audio":
                return <audio ref={audioRef} className="fm-player" controls>
                    <source src={fileUrl} type={mimeType} />
                </audio>;
            default:
                return <video ref={videoRef} className="fm-player" controls>
                    <source src={fileUrl} />
                </video>;
            }
        };

        switch (fileType) {
        case "jpg":
        case "png":
        case "gif":
        case "webp":
        case "svg":
            return <img src={url} alt={file?.name}/>;
        case "mp4":
        case "webm":
        case "mov":
        case "wmv":
            return mediaContainer("video", url);
        case "mp3":
            return mediaContainer("audio", url, "audio/mpeg");
        case "ogg":
            return mediaContainer("video", url);
        case "wav":
            return mediaContainer("audio", url, "audio/wav");
        case "pdf":
            return <PDFViewer url={url} />;
        case "txt":
        case "log":
            return <TextReader url={url} />;
        default:
            return (
                <>
                    <Alert
                        type="warning"
                        description={(
                            <>
                                <span>{t("UNKNOWN_FILE_TYPE_DESCRIPTION")}</span> <span>{`${fileType ? fileType : ""}`}</span>
                            </>
                        )}
                    />

                    <TextReader url={url} />
                </>
            );
        }
    };

    const closeModal = () => props.close(false);

    return (
        <Modal
            {...props}
            onCancel={closeModal}
            destroyOnClose={true}
            footer={(
                <Space>
                    <Button
                        onClick={closeModal}
                        type="primary"
                        danger
                    >
                        {t("MODAL_BUTTON_CANCEL")}
                    </Button>
                    <Button
                        icon={<Mdicon name="download" />}
                        type="primary"
                    >
                        {t("DOWNLOAD")}
                    </Button>
                </Space>
            )}
        >
            <div className="fm-file-preview-container">
                {previewComponentHandler(props.file)}
                <Divider>{props.file?.name}</Divider>
            </div>
        </Modal>
    );
});

FilePreview.displayName = "FilePreview";

export default FilePreview;
