import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; // this is needed!
import {enableProdMode, Injector, LOCALE_ID, NgModule} from '@angular/core';
import {HttpClient, HttpClientModule} from '@angular/common/http';

import {AppComponent} from './app.component';

import {CoreModule} from './core/core.module';
import {LayoutModule} from './layout/layout.module';
import {SharedModule} from './shared/shared.module';
import {RoutesModule} from './routes/routes.module';
import {HashLocationStrategy, LocationStrategy, registerLocaleData} from '@angular/common';
import {environment} from '../environments/environment';
import * as $ from 'jquery';
import {AjaxService} from './core/ajax/ajax.service';
import {FrameService} from './core/frame/frame.service';
import localeZh from '@angular/common/locales/zh';
import {DomSanitizer} from '@angular/platform-browser';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule,
    BrowserAnimationsModule,
    CoreModule,
    SharedModule.forRoot(),
    LayoutModule,
    RoutesModule
  ],
  providers: [{provide: LocationStrategy, useClass: HashLocationStrategy},{ provide: LOCALE_ID, useValue: 'zh-CN' }],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(private injector: Injector, private httpClient: HttpClient,  private sanitizer: DomSanitizer) {

    // 中文环境
    registerLocaleData(localeZh);
    AjaxService.injector = this.injector;
    AjaxService.http = this.httpClient;
    AjaxService.sanitizer = this.sanitizer;
    // if (!environment.production) {
    //   // develop mode ,auto login
    //   $.ajax({
    //     url: environment.webRoot + 'frame/optr/login.htm',
    //     async: false,
    //     type: 'POST',
    //     dataType: 'json',
    //     data: {
    //       loginName: 'admin',
    //       loginPass : '123456',
    //       loginCode : '1234'
    //     },
    //     success: function (data) {
    //       console.log('develop model ,auto login success！');
    //     },
    //     error : function () {
    //       alert('develop model, tomcat not startup!');
    //     }
    //   });
    // }

    // login success, get param with not async
    let init: boolean = false;
    $.ajax({
      url: environment.webRoot + 'frame/optr/init.htm',
      async: false,
      type: 'POST',
      dataType: 'json',
      success: function (data) {
        if (data.success === false) {
          window.location.href = './assets/login/login.html';
          return;
        }
        FrameService.instance(data.map);
        init = true;
      },
      error: function () {
        alert('服务端未正常运行，请联系系统管理员！')
        window.location.href = './assets/login/login.html';
        return;
      }
    });

    if (init === false) {
      // window.location.href = './assets/login/login.html';
      return;
    }
  }
}
