import { Folder, File, Drive } from '../Models';
import axios, { AxiosPromise, AxiosRequestConfig } from 'axios';
import { CachingService } from ".";


export class FileService {
    private baseUrl: string = '';
    private _axiosRequestConfig: AxiosRequestConfig;
    private config: any = require('../config.json');

    constructor() {
        let cachingService: CachingService = new CachingService();
        this._axiosRequestConfig = {
            headers: {
                'Content-Type': 'application/json',
                'TenantId': cachingService.read('TenantId'),
                'Language': cachingService.readPersistant('Language'),
                'Authorization': 'Bearer ' + cachingService.read('Token'),
                'UserId': cachingService.read('UserId')
            }
        };
        this.baseUrl = this.config.baseUrl;

    }

    getRequestHeaders():any{
        return this._axiosRequestConfig.headers;
    }

    listFiles(): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/ListFiles', this._axiosRequestConfig)
    }

    deleteFile(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/DeleteFile/' + id, this._axiosRequestConfig)
    }

    getFile(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/GetFile/' + id, this._axiosRequestConfig)
    }

    saveFile(file: File): Promise<File> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/File/SaveFile/', file, this._axiosRequestConfig).then((response) => {
                resolve(response.data as File);
            }, (err) => {
                reject(err);
            });
        });
    }

    downloadFileContent(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/DownloadFileContent/' + id, this._axiosRequestConfig)
    }

    uploadFileContent(formData: any): Promise<File> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/File/UploadFileContent/', formData, this._axiosRequestConfig).then((response) => {
                resolve(response.data as File);
            }, (err) => {
                reject(err);
            });
        });
    }

    getUploadFileContentEndpoint(recordId: number): string {
        return this.baseUrl + '/api/AAAID/UploadFileContent/?recordId=' + recordId;
    }

    getDeleteFileEndpoint(id: number): string {
        return this.baseUrl + '/api/File/DeleteFile/' + id;
    }

    getFullPath(file: File | null) {
        if (file == null)
            return "";

        return this.baseUrl + "/" + file.path;
    }


    listFilesInFolder(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/ListFilesInFolder/' + id, this._axiosRequestConfig)
    }

    deleteFolder(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/DeleteFolder/' + id, this._axiosRequestConfig)
    }

    getFolder(id: number): Promise<Folder> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/File/GetFolder/' + id, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Folder);
            }, (err) => {
                reject(err);
            });
        });
    }

    saveFolder(folder: Folder): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/File/SaveFolder', folder, this._axiosRequestConfig)
    }

    listDrives(): Promise<Drive[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/File/ListDrives', this._axiosRequestConfig).then((response) => {
                resolve(response.data as Drive[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    deleteDrive(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/File/DeleteDrive/' + id, this._axiosRequestConfig)
    }

    getDrive(id: number): Promise<Drive> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/File/GetDrive/' + id, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Drive);
            }, (err) => {
                reject(err);
            });
        });
        //return axios.get(this.baseUrl + '/api/File/GetDrive/' + id, this._axiosRequestConfig);
    }

    saveDrive(drive: Drive): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/File/SaveDrive', drive, this._axiosRequestConfig)
    }
}