import {IFileInputBus} from '../interfaces/bus/IFileInputBus';
import {IFactory as IComponentsFactory} from '../interfaces/components/IFactory';
import {TFileType, TFilesBrowserIcon} from '../types/TFilesBrowserIcon';

export class FileInputBus implements IFileInputBus {

    protected componentsFactory:IComponentsFactory;

    public setComponentsFactory(factory:IComponentsFactory)
    {
        this.componentsFactory = factory;
    }

    public execBrowserModal():Promise<string>
    {
        return this.componentsFactory.getFileInputFactory().getBrowserModal().show();
    }

    public updateFileBrowser(icons:TFilesBrowserIcon[], path:string)
    {
        this.componentsFactory.getFileInputFactory().getBrowserModal().setCurrentPath(path);
        this.componentsFactory.getFileInputFactory().getBrowserModal().update(icons);
    }

    public execItemContextMenu(x:number, y:number, icon:TFilesBrowserIcon)
    {
        this.componentsFactory.getFileInputFactory().getFileContextMenu().show(x, y, icon);
    }

    public execItemModal(name:string = ''):Promise<string>
    {
        return new Promise<string>((resolve:any, reject:any)=>{
            let dirName = prompt('Имя', name);
            if(dirName){
                resolve(dirName);
            }else{
                reject();
            }
        });
    }

    public hideItemContextMenu()
    {
        this.componentsFactory.getFileInputFactory().getFileContextMenu().hide();
    }

    public deletedFile(path:string)
    {
        this.componentsFactory.getFileInputFactory().getBrowserModal().deleteFile(path);
    }

    public renamedFile(path:string, newName:string)
    {
        this.componentsFactory.getFileInputFactory().getBrowserModal().renameFile(path, newName);
    }

    public createdDir(path:string, name:string)
    {
        let dirData:TFilesBrowserIcon = {
            type: TFileType.dir,
            name: name,
            path: path,
            filemtime: '',

        }
        this.componentsFactory.getFileInputFactory().getBrowserModal().newItem(dirData);
    }

    public uploadedFile(path:string, name:string)
    {
        let fileData:TFilesBrowserIcon = {
            type: TFileType.file,
            name: name,
            path: path,
            filemtime: '',
        }
        this.componentsFactory.getFileInputFactory().getBrowserModal().newItem(fileData);
    }

    public uploadedImg(path:string, name:string)
    {
        let fileData:TFilesBrowserIcon = {
            type: TFileType.img,
            name: name,
            path: path,
            filemtime: '',
        }
        this.componentsFactory.getFileInputFactory().getBrowserModal().newItem(fileData);
    }

}