import React, {Dispatch, SetStateAction, useState} from "react";
import {Button, Empty, Result, Table, TableColumnProps, TableProps} from "antd";
import {BotDownloadedFile, BotFileManagerFile} from "../../../Store/BotFileManager/Types";
import sendCommand from "../../../Requests/Commands/sendCommands";
import {useTranslation} from "react-i18next";
import File from "./File";
import useBreakpoint from "antd/es/grid/hooks/useBreakpoint";
import i18n from "i18next";
import Mdicon from "../../Misc/Mdicon";
import Spinner from "../../Misc/Spinner";

interface IExplorerFilesProps {
    files: BotFileManagerFile[],
    downloadedFiles: BotDownloadedFile[],
    botId: string,
    downloading: boolean,
    setDownloading: Dispatch<SetStateAction<boolean>>,
    setTreeLoading: Dispatch<SetStateAction<boolean>>
    treeLoading: boolean,
    setPreviewFile: Dispatch<SetStateAction<BotFileManagerFile | undefined>>,
    setShowPreview: Dispatch<SetStateAction<boolean>>
}

const ExplorerFiles: React.FC<IExplorerFilesProps> = (props: IExplorerFilesProps) => {
    const {t} = useTranslation();
    const [buttonID, setButtonID] = useState<string | null>(null);

    const checkFile = (file: BotFileManagerFile) => {
        let _file;

        props.downloadedFiles.filter((dlFile) => {
            if (file.path === dlFile.path) {
                _file = dlFile;
            }
        });

        return _file;
    };

    const screens = useBreakpoint();

    const columns: TableColumnProps<BotFileManagerFile>[] = [
        {
            title: screens.xs ? i18n.t("FOLDER_OR_FILE") : i18n.t("FILE"),
            dataIndex: "name",
            key: "name",
            render: (name: string, file: BotFileManagerFile) => {
                if (screens.xs) {
                    return <File file={file} botId={props.botId} />;
                } else {
                    if (!file.isDir) {
                        return <File file={file} botId={props.botId} />;
                    } else {
                        return false;
                    }
                }
            },
            width: screens.xs ? 150 : 300,
        },
        {
            title: " ",
            dataIndex: "action",
            key: "action",
            render: (action: string, file: BotFileManagerFile) => {
                if (!file.isDir) {
                    if (file.is_exists_on_server) { // preview file
                        return (
                            <div style={{textAlign: "center"}}>
                                <Button
                                    size="small"
                                    type="primary"
                                    icon={<Mdicon name={"visibility"} />}
                                    onClick={() => {
                                        props.setPreviewFile(checkFile(file));
                                        props.setShowPreview(true);
                                    }}
                                />
                            </div>
                        );
                    } else { // download file
                        return (
                            <div style={{textAlign: "center"}}>
                                <Button
                                    type="primary"
                                    className="fm-download-button"
                                    icon={(
                                        <span role="img" className="anticon">
                                            <i className="fa-solid fa-download"/>
                                        </span>
                                    )}
                                    onClick={() => {
                                        sendCommand({
                                            command: "fmmanager",
                                            payload: {
                                                path: file.path,
                                                extra: "dl",
                                            },
                                            botIds: [props.botId],
                                        });

                                        props.setDownloading(true);
                                        setButtonID(file.name);
                                    }}
                                    size="small"
                                    loading={buttonID === file.name && props.downloading}
                                    disabled={buttonID === file.name && props.downloading}
                                />
                            </div>
                        );
                    }
                } else {
                    return (
                        <Button
                            type="primary"
                            icon={<Mdicon name="subdirectory_arrow_right" />}
                            size="small"
                            onClick={() => {
                                sendCommand({
                                    command: "fmmanager",
                                    payload: {
                                        path: file.path,
                                        extra: "ls",
                                    },
                                    botIds: [props.botId],
                                });

                                props.setTreeLoading(true);
                            }} />
                    );
                }
            },
            width: 5,
        },
    ];

    const tableProps: TableProps<BotFileManagerFile> = {
        columns: columns,
        pagination: false,
        size: "small",
        className: "not-responsive fm-antd-table",
        locale: {emptyText: <Empty description={t("NO_DATA")} />},
        loading: {
            spinning: props.treeLoading,
            indicator: (
                <div className="fm-table-spinner">
                    <Spinner />
                </div>
            ),
        },
        rowKey: "name",
    };

    if (props.files.length !== 0) {
        return (
            <div className="explorer-content">
                {screens.xs ? (
                    <Table {...tableProps} dataSource={props.files}/>
                ) : (
                    <Table {...tableProps} dataSource={props.files.filter((file) => !file.isDir)}/>
                )}
            </div>
        );
    } else {
        return (
            <Result status="warning" title={t("NO_FS_DATA")}/>
        );
    }
};

export default ExplorerFiles;
