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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { KeycloakService } from 'keycloak-angular';
import { initializeKeycloak } from './core/init-keycloak';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
import { FooterComponent } from 'src/app/components/footer/footer.component';
import { HeaderComponent } from 'src/app/components/header/header.component';
import { NotFoundComponent } from 'src/app/components/error/not-found/not-found.component';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { Logger } from 'src/app/service/logger.service';
import { SharedModule } from 'src/app/modules/shared/shared.module';
import { SpinnerComponent } from 'src/app/components/spinner/spinner.component';
import { ConfirmationService, MessageService } from 'primeng/api';
import { DialogService } from 'primeng/dynamicdialog';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { SidebarComponent } from './components/sidebar/sidebar.component';
import { ErrorHandlingService } from 'src/app/service/error-handling.service';
import { HomeComponent } from './components/home/home.component';
import { SettingsService } from 'src/app/service/settings.service';

if (environment.production) {
  Logger.enableProductionMode();
} else {
  Logger.enableDevMode();
}

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

export function appInitializerFactory(
  keycloak: KeycloakService,
  settings: SettingsService
): () => Promise<void> {
  return (): Promise<void> =>
    new Promise<void>((resolve, reject) => {
      settings
        .loadSettings()
        .then(() => initializeKeycloak(keycloak, settings))
        .then(() => resolve())
        .catch(error => reject(error));
    });
}

@NgModule({
  declarations: [
    AppComponent,
    FooterComponent,
    HeaderComponent,
    NotFoundComponent,
    SpinnerComponent,
    SidebarComponent,
    HomeComponent,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    SharedModule,
    TranslateModule.forRoot({
      defaultLanguage: 'en',
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient],
      },
    }),
  ],
  exports: [
    BrowserModule,
    BrowserAnimationsModule,
    SharedModule,
    AppRoutingModule,
  ],
  providers: [
    MessageService,
    ConfirmationService,
    DialogService,
    KeycloakService,
    {
      provide: APP_INITIALIZER,
      useFactory: appInitializerFactory,
      multi: true,
      deps: [KeycloakService, SettingsService],
    },
    {
      provide: ErrorHandler,
      useClass: ErrorHandlingService,
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}
