namespace ti_common.file {
    'use strict';

    export type Content = string | Blob;
    export enum ContentType {
        Text,
        Blob
    }

    export let ContentTypeHeaderLookup = ["application/text", "application/octet-stream"];
    export let ResponseTypeConfigLookup = [null, 'blob'];


    export interface IFileSerializable {
        name: string;
        path: string;
        size: number;
        type: string;
        isLocal: boolean;
    }

    // base file class
    export abstract class File {

        public name: string;
        public notReadyMessage: string;
        public isReady: boolean = true;
        constructor(
            public path: string,
            public type: string,
            public size: number = null,
            public isLocal: boolean = false
        ) {
            this.name = this.getBaseName();
        }

        private getBaseName(): string {
            this.path = this.path.replace(/\\/g, '/');
            let parts = this.path.split('/');
            return parts[parts.length - 1];
        }

        getDisplayTime(): string {
            return '';
        }

        getDisplaySize(): string {
           	let size = this.size;
            if (typeof size === 'number') {
                if (size > 1048576)
                    return (size / 1048576).toFixed(2) + ' MB';
                else if (size > 1024)
                    return (size / 1024).toFixed(2) + ' KB';
                else
                    return size + ' B';
            } else {
                return 'unknown';
            }
        }

        abstract delete(): angular.IPromise<boolean>;
        // set the file contents
        abstract setContents(contents: Content): angular.IPromise<string>
        // get the contents of the file
        abstract getContents(contentType?: ti_common.file.ContentType): angular.IPromise<Content>;

        toJSON(): IFileSerializable {
            return {
                name: this.name,
                path: this.path,
                size: this.size,
                type: this.type,
                isLocal: this.isLocal
            }
        }

    }

    export interface IFileFactory {
        create(path: string): File;
        createFromJSON(obj: IFileSerializable);
    }

}