import axios from 'axios';
import { IFilesBrowserCommands } from '../interfaces/commands/IFilesBrowserCommands';
import { TSettings } from '../types/TSettings';

declare let settings: TSettings;

export class FilesBrowserCommands implements IFilesBrowserCommands {

    public getDir(path: string): Promise<any> {
        let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        return axios.post(
            settings.fileInputSettings.getDirUrl,
            {
                _token: token,
                path: path,
            }
        )
            .then((resp: any) => {
                return Promise.resolve(resp.data);
            })
            .catch((error: any) => {
                return Promise.reject(error.response.data);
            });
    }

    public createDir(path: string): Promise<any> {
        let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        return axios.post(
            settings.fileInputSettings.createDirUrl,
            {
                _token: token,
                path: path,
            }
        )
            .then((resp: any) => {
                return Promise.resolve(resp.data);
            })
            .catch((error: any) => {
                return Promise.reject(error.response.data);
            });
    }

    public deleteFile(path: string): Promise<any> {
        let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        return axios.post(
            settings.fileInputSettings.deleteFileUrl,
            {
                _token: token,
                path: path,
            }
        )
            .then((resp: any) => {
                return Promise.resolve(resp.data);
            })
            .catch((error: any) => {
                return Promise.reject(error.response.data);
            });
    }

    public uploadFile(path: string, file: File): Promise<any> {
        let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        const formData = new FormData();
        formData.append('_token', token);
        formData.append('path', path);
        formData.append('file', file);
        return axios.post(
            settings.fileInputSettings.uploadFileUrl,
            formData,
            {
                headers: {
                    'Content-Type': 'multipart/form-data', // Обязательно указываем тип контента!
                },
            }
        )
            .then((resp: any) => {
                return Promise.resolve(resp.data);
            })
            .catch((error: any) => {
                return Promise.reject(error.response.data);
            });
    }

    public renameFile(path: string, newPath: string): Promise<any> {
        let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        return axios.post(
            settings.fileInputSettings.renameFileUrl,
            {
                _token: token,
                path: path,
                new_path: newPath,
            }
        )
            .then((resp: any) => {
                return Promise.resolve(resp.data);
            })
            .catch((error: any) => {
                return Promise.reject(error.response.data);
            });
    }

}