declare var Promise;
declare var fetch;

export class Base {
    /*
    * Application base class: initializes global '_abl' namespace
    * */
    private _environment: any = { type: 'js', namespace: '_abl'};
    private _settings: any = {};
    public settings: any = {};

    constructor() {
        // browser
        if (typeof window !== 'undefined') {
            this._environment.type = 'browser';
            if (window['angular']) {
                this._environment.framework = 'angular-js';
            }
            if (!window['angular']) {
                this._environment.framework = 'angular';
            }
            if (!window[this._environment.namespace]) {
                (<any>window)[this._environment.namespace] = {};
            }
        }
        // nodejs
        if (typeof window === 'undefined') {
            this._environment.type = 'nodejs';
            this._environment.framework = 'nodejs';
        }
        this._environment.date = new Date();
        this._environment.started = Math.round(+new Date() / 1000);
    }

    protected init(settings: any) {
        const self = this;
        return new Promise(function(resolve, reject) {
            if (typeof settings === 'string') {
                fetch(settings)
                    .then(function(response) {
                        return response.json();
                    })
                    .then(function(json) {
                        self.settings = json;
                        resolve();
                    });
            } else {
                self.settings = settings;
                resolve();
            }
        });
    }

    // todo: load namespace library
    // loadLibrary(paths){
    //     let self = this;
    //     let classes = [];
    //     _.each(paths,function(oath){
    //         classes.push(self.classRef());
    //     });
    //     console.log(classes);
    // }
    //
    // classRef(path:string){
    //     _.each(this._namespace,function(ns){
    //         _.each(ns,function(v,k){
    //             console.log(k+' = '+v);
    //             if(k==path){
    //                 return v;
    //             }
    //         });
    //     });
    // }
    //
    // set classPath(path:string){
    //     let atPath = '';
    //     let parts = path.split('.');
    //     if(this._namespace[path]){
    //         console.error('classPath: '+path+'is already defined.');
    //         return;
    //     }
    //     _.each(parts,function(p){
    //         atPath += p+'.'
    //     });
    //     console.log(atPath);
    //    // this._namespace[path]=
    //
    // }

}
