import React, {Dispatch, SetStateAction, useEffect} from "react";
import {Button, Divider, Input, Result, Space} from "antd";
import {BotDownloadedFile, BotFileManagerFile} from "../../../Store/BotFileManager/Types";
import {t} from "i18next";
import {getBackendUrl} from "../../../Util/config";
import File from "./File";
import moment from "moment/moment";
import useBreakpoint from "antd/es/grid/hooks/useBreakpoint";

interface IExplorerDownloadedFilesProps {
    downloadedFiles: BotDownloadedFile[],
    botId: string,
    isLoading: boolean,
    setDownloading: Dispatch<SetStateAction<boolean>>,
    setPreviewFile: Dispatch<SetStateAction<BotFileManagerFile | undefined>>,
    setShowPreview: Dispatch<SetStateAction<boolean>>
}

const ExplorerDownloadedFiles: React.FC<IExplorerDownloadedFilesProps> = (props: IExplorerDownloadedFilesProps) => {
    const screen = useBreakpoint();

    useEffect(() => {
        props.setDownloading(false);
    }, [props.downloadedFiles]);

    return (
        <div className="panel-content">
            <Divider orientation="left">{t("DOWNLOADED_FILES")}</Divider>

            <div className="explorer-content downloaded-content">
                {props.isLoading ? (
                    <Result
                        icon={(
                            <span className="anticon">
                                <i className="fa-solid fa-download" />
                            </span>
                        )}
                        className="ant-result-accent"
                        title={t("LOADING_FILES")}
                        subTitle={t("LOADING_FILES_DESCRIPTION")}
                    />
                ) : (
                    <>
                        {props.downloadedFiles && props.downloadedFiles.length !== 0 ? (
                            <>
                                {screen.xs ? (
                                    <>
                                        <table className="fm-table fm-table-downloaded">
                                            <tbody>
                                                {props.downloadedFiles.map((file: BotDownloadedFile, key: number) => {
                                                    return (
                                                        <tr key={key}>
                                                            <td>
                                                                <File
                                                                    file={file}
                                                                    botId={props.botId}
                                                                    setPreviewFile={props.setPreviewFile}
                                                                    setShowPreview={props.setShowPreview}
                                                                    previewFile={true}
                                                                />
                                                            </td>
                                                            <td style={{textAlign: "center"}}>
                                                                <Space>
                                                                    <Button
                                                                        type="primary"
                                                                        className="fm-download-button"
                                                                        icon={(
                                                                            <span role="img" className="anticon">
                                                                                <i className="fa-solid fa-download" />
                                                                            </span>
                                                                        )}
                                                                        href={getBackendUrl(`download/botFile/${file.id}`)}
                                                                        size="small"
                                                                    />
                                                                </Space>
                                                            </td>
                                                        </tr>
                                                    );
                                                })}
                                            </tbody>
                                        </table>
                                    </>

                                ) : (
                                    <>
                                        <table className="fm-table fm-table-downloaded">
                                            <tbody>
                                                {props.downloadedFiles.map((file: BotDownloadedFile, key: number) => {
                                                    return (
                                                        <tr key={key}>
                                                            <td>
                                                                <File
                                                                    file={file}
                                                                    botId={props.botId}
                                                                    setPreviewFile={props.setPreviewFile}
                                                                    setShowPreview={props.setShowPreview}
                                                                    previewFile={true}
                                                                />
                                                            </td>
                                                            <td style={{textAlign: "center"}}>
                                                                <Input value={file.path} readOnly title={file.path} />
                                                            </td>
                                                            <td style={{textAlign: "center"}}>
                                                                {file.size} KB
                                                            </td>
                                                            <td style={{textAlign: "center"}}>
                                                                {moment(file.created_at).format("YY-MM-DD H:m:s")}
                                                            </td>
                                                            <td style={{textAlign: "center"}}>
                                                                <Space>
                                                                    <Button
                                                                        type="primary"
                                                                        className="fm-download-button"
                                                                        icon={(
                                                                            <span role="img" className="anticon">
                                                                                <i className="fa-solid fa-download" />
                                                                            </span>
                                                                        )}
                                                                        href={getBackendUrl(`download/botFile/${file.id}`)}
                                                                        size="small"
                                                                    />
                                                                </Space>
                                                            </td>
                                                        </tr>
                                                    );
                                                })}
                                            </tbody>
                                        </table>
                                    </>
                                )}
                            </>
                        ) : (
                            <Result
                                icon={(
                                    <span className="anticon">
                                        <i className="fa-solid fa-download" />
                                    </span>
                                )}
                                className="ant-result-accent"
                                title={t("DOWNLOADS_EMPTY")}
                                subTitle={t("DOWNLOADS_EMPTY_DESCRIPTION")}
                            />
                        )}
                    </>
                )}
            </div>
        </div>
    );
};

export default ExplorerDownloadedFiles;
