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

declare let settings:TSettings;

export class AppCommands implements IAppCommands {

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

    public newItem(dirId:string, type:string, name:string):Promise<any>
    {
        let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        return axios.post(
            settings.newItemUrl,
            {
                _token:token,
                'dir-id':dirId,
                'object':{
                    'type': type,
                },
                name:name,
            }
        )
        .then((resp:any)=>{
            return Promise.resolve(resp.data);
        })
        .catch((error:any)=>{
            return Promise.reject(error.response.data);
        });
    }

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

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

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

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

}