import React, {Dispatch, SetStateAction} from "react";
import sendCommand from "../../../Requests/Commands/sendCommands";
import {getFileExt, getFileExtIcon} from "../../../Util/getFileExt";
import {Space} from "antd";
import {BotFileManagerFile} from "../../../Store/BotFileManager/Types";

interface IFile {
    file: {
        name: string,
        isDir?: boolean,
        path: string,
    },
    botId: string,
    setPreviewFile?: Dispatch<SetStateAction<BotFileManagerFile | undefined>>,
    setShowPreview?: Dispatch<SetStateAction<boolean>>,
    setTreeLoading?: Dispatch<SetStateAction<boolean>>,
    previewFile?: boolean,
}

const File: React.FC<IFile> = (props: IFile) => {
    return (
        <Space>
            <div className="fm-content-flex" title={props.file.name}>
                <div className="fm-content-icon">
                    {props.file.isDir ? (
                        <i className="fm-icon fa-solid fa-folder" style={{color: "#eea83d", fontSize: 30}}/>
                    ) : (
                        <>
                            {getFileExtIcon(getFileExt(props.file.name))}
                        </>
                    )}
                </div>
                <div className="fm-content-body">
                    {props.file.isDir ? (
                        <a onClick={() => {
                            sendCommand({
                                command: "fmmanager",
                                payload: {
                                    path: props.file.path,
                                    extra: "ls",
                                },
                                botIds: [props.botId],
                            });

                            props.setTreeLoading && props.setTreeLoading(true);
                        }}>
                            {props.file.name}
                        </a>
                    ) : (
                        <>
                            {props.previewFile ? (
                                <a onClick={() => {
                                    props.setShowPreview && props.setShowPreview(true);
                                    props.setPreviewFile && props.setPreviewFile(props.file as BotFileManagerFile);
                                }} style={{color: "inherit"}}>
                                    {props.file.name}
                                </a>
                            ) : (
                                <>{props.file.name}</>
                            )}
                        </>
                    )}
                </div>
            </div>
        </Space>
    );
};

export default File;
