import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { UniversalModule, isBrowser, isNode, AUTO_PREBOOT } from 'angular2-universal/browser'; // for AoT we need to manually split universal packages

import {AppComponent} from "./app.component";
import {AppModule} from "./app.module";
import {CacheService} from "./core/cache.service";

// Will be merged into @angular/platform-browser in a later release
// see https://github.com/angular/angular/pull/12322
import { Meta } from './helpers/angular2-meta';

// import * as LRU from 'modern-lru';

export function getLRU(lru?: any) {
    // use LRU for node
    // return lru || new LRU(10);
    return lru || new Map();
}
export function getRequest() {
    return Zone.current.get('req') || {};
}
export function getResponse() {
    return Zone.current.get('res') || {};
}


// TODO(gdi2290): refactor into Universal
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';

/*
 export const THIS_APP_CONFIG  = {
 apiEndpoint: 'api.heroes.com',
 title: 'Dependency Injection'
 };*/

@NgModule({
    bootstrap: [ AppComponent ],
    imports: [
        UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
        FormsModule,
        RouterModule.forRoot([], { useHash: false }),
        AppModule,
    ],
    providers: [
        { provide: 'isBrowser', useValue: isBrowser },
        { provide: 'isNode', useValue: isNode },

        { provide: 'req', useFactory: getRequest },
        { provide: 'res', useFactory: getResponse },
        { provide: 'LRU', useFactory: getLRU, deps: [] },
        /*{ provide: APP_CONFIG, useValue: THIS_APP_CONFIG}*/
        CacheService,

        Meta,

        // { provide: AUTO_PREBOOT, useValue: false } // turn off auto preboot complete
    ]
})
export class MainModule {
    constructor(public cache: CacheService) {
        // TODO(gdi2290): refactor into a lifecycle hook
        this.doRehydrate();
    }

    doRehydrate() {
        let defaultValue = {};
        let serverCache = this._getCacheValue(CacheService.KEY, defaultValue);
        this.cache.rehydrate(serverCache);
    }

    _getCacheValue(key: string, defaultValue: any): any {
        // browser
        const win: any = window;
        if (win[UNIVERSAL_KEY] && win[UNIVERSAL_KEY][key]) {
            let serverCache = defaultValue;
            try {
                serverCache = JSON.parse(win[UNIVERSAL_KEY][key]);
                if (typeof serverCache !== typeof defaultValue) {
                    console.log('Angular Universal: The type of data from the server is different from the default value type');
                    serverCache = defaultValue;
                }
            } catch (e) {
                console.log('Angular Universal: There was a problem parsing the server data during rehydrate');
                serverCache = defaultValue;
            }
            return serverCache;
        } else {
            console.log('Angular Universal: UNIVERSAL_CACHE is missing');
        }
        return defaultValue;
    }
}
