import { NgModule, ErrorHandler, LOCALE_ID } from '@angular/core';
import {
    HttpClient,
    HttpClientModule,
    HTTP_INTERCEPTORS,
} from '@angular/common/http';
import { registerLocaleData } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';

import {
    TranslateModule,
    TranslateLoader,
    TranslateService,
} from '@ngx-translate/core';

import { AppRoutingModule } from '@app/app-routing.module';
import { AppComponent } from '@app/app.component';

import { ErrorHandlerProvider } from '@shared/providers';
import {
    AppInsightService,
    appInsightsProvider,
    baseHrefProvider,
    LocalizationService,
    settingsProvider,
    windowProvider,
} from '@shared/services';
import { TranslateHttpLoaderFactory } from '@shared/factories';

import { SharedModule } from '@shared';
import { FlightsModule } from '@modules/flights.module';

import localeRu from '@angular/common/locales/ru';
import localeEn from '@angular/common/locales/en';
import localeEs from '@angular/common/locales/es';
import localeFr from '@angular/common/locales/fr';
import localeIt from '@angular/common/locales/it';
import localeJa from '@angular/common/locales/ja';
import localeKo from '@angular/common/locales/ko';
import localeZh from '@angular/common/locales/zh';
import localeDe from '@angular/common/locales/de';

import { ScrollingModule } from '@angular/cdk/scrolling';

import { LayoutModule } from '@angular/cdk/layout';

import { AppInterceptor } from '@shared/interceptor/app.interceptor';

import { MomentModule } from 'ngx-moment';

registerLocaleData(localeRu);
registerLocaleData(localeZh);
registerLocaleData(localeEn);
registerLocaleData(localeEs);
registerLocaleData(localeFr);
registerLocaleData(localeIt);
registerLocaleData(localeJa);
registerLocaleData(localeKo);
registerLocaleData(localeDe);

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        //BrowserModule.withServerTransition({ appId: 'serverApp' }),
        BrowserAnimationsModule,
        AppRoutingModule,
        FormsModule,
        SharedModule,
        FlightsModule,
        HttpClientModule,
        ScrollingModule,
        LayoutModule,
        MomentModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: TranslateHttpLoaderFactory,
                deps: [HttpClient],
            },
        }),
    ],
    providers: [
        baseHrefProvider,
        {
            provide: LOCALE_ID,
            deps: [LocalizationService],
            useFactory: (locale: LocalizationService) => locale.Language,
        },
        {
            provide: ErrorHandler,
            useClass: ErrorHandlerProvider,
        },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: AppInterceptor,
            multi: true,
        },
        appInsightsProvider,
        windowProvider,
        settingsProvider,
    ],
    bootstrap: [AppComponent],
})
export class AppModule {
    constructor(
        appInsights: AppInsightService,
        translate: TranslateService,
        locale: LocalizationService,
    ) {
        appInsights.configure();
        locale.init(translate);
    }
}
