import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AdminLayout } from './admin-layout.component';
import { LOCALE_ID } from '@angular/core';
import { NZ_I18N, zh_CN, en_US } from 'ng-zorro-antd/i18n';
import { registerLocaleData, ViewportScroller } from '@angular/common';
import zh from '@angular/common/locales/zh';
import en from '@angular/common/locales/en';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { IconsProviderModule } from './icons-provider.module';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzFormModule } from 'ng-zorro-antd/form';

import { AppComponent } from './app.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MenuComponent } from './menu.component';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
import { NzAvatarModule } from 'ng-zorro-antd/avatar';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzMessageModule } from 'ng-zorro-antd/message';
import { Router, RouteReuseStrategy, Scroll } from '@angular/router';
import { filter } from 'rxjs';
import { MyLayoutComponent } from './my-layout.component';
import { NzNoAnimationModule } from 'ng-zorro-antd/core/no-animation';
import { NzBadgeModule } from 'ng-zorro-antd/badge';
import { NzDrawerModule } from 'ng-zorro-antd/drawer';
import { UtilsModule } from 'src/app/lib/utils/utils.module';



registerLocaleData(zh);
registerLocaleData(en);

@NgModule({
  declarations: [
    AdminLayout,
    AppComponent,
    MenuComponent,
    MyLayoutComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpClientModule,
    BrowserAnimationsModule,
    //NoopAnimationsModule,
    IconsProviderModule,
    NzLayoutModule,
    NzMenuModule,
    NzFormModule,
    NgbModule,
    NzDropDownModule,
    NzAvatarModule,
    NzButtonModule,
    NzMessageModule,
    NzNoAnimationModule,
    NzBadgeModule,
    NzDrawerModule,
    UtilsModule,
  ],

  // providers: [
  //   { provide: NZ_I18N, useValue: zh_CN },
  //   //{ provide: RouteReuseStrategy, useClass: CacheRouteReuseStrategy }
  // ],
  /** 根据 LOCALE_ID 自动切换 ng-zorro-antd 语言 **/
  providers: [
    {
      provide: NZ_I18N,
      useFactory: (localId: string) => {
        switch (localId) {
          case 'en':
            return en_US;
          /** 与 angular.json i18n/locales 配置一致 **/
          case 'zh':
            return zh_CN;
          default:
            return zh_CN;
        }
      },
      deps: [LOCALE_ID]
    }
  ],

  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(router: Router, scroller: ViewportScroller) {
    router.events.pipe(
      filter((e: any): e is Scroll => e instanceof Scroll)
    ).subscribe(e => {
      if (e.position) {
        console.log(`scrollTo:${e.position}`);
        setTimeout(() => {
          scroller.scrollToPosition(e.position);
        }, 100);
      }
    })

  }
}
