import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; // this is needed!
import {Injector, LOCALE_ID, NgModule} from '@angular/core';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateService, TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
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 localeZh from '@angular/common/locales/zh';
import * as $ from 'jquery';
import {environment} from "../environments/environment";
import {FrameService} from "./core/service/frame.service";
import {ToastrService} from "ngx-toastr";
import {MsgService} from "./core/service/msg.service";
import {NgxLoadingModule} from "ngx-loading";

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule,
    BrowserAnimationsModule, // required for ng2-tag-input
    CoreModule,
    LayoutModule,
    SharedModule.forRoot(),
    RoutesModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    }),
    NgxLoadingModule
  ],
  providers: [ {provide: LOCALE_ID, useValue: 'zh-CN'}],
  bootstrap: [AppComponent]
})
export class AppModule {
  /***********************
   * 系统初始化，
   * 1.所有静态变量设置
   * 2.系统登录，获取系统参数、资源、权限、字典等
   */
  constructor(private injector: Injector, private http: HttpClient) {
    // 中文环境
    registerLocaleData(localeZh);

    http.get(`${environment.webRoot}/frame/optr/init.htm`).toPromise().then((data) => {
      if (data['success'] === false) {
        window.location.href = './assets/login/login.html';
        return;
      }
      FrameService.initializeAfterLoin(injector, data['map']);
    }) .catch((res)=>{
      alert('Service is not running！');
      window.location.href = './assets/login/login.html';
    });
  }
}
