import {APP_INITIALIZER, forwardRef, NgModule} from "@angular/core";
import {NgbModule} from "@ng-bootstrap/ng-bootstrap";
import {CommonModule, registerLocaleData} from "@angular/common";
import localeRu from '@angular/common/locales/ru';
import {FormsModule} from "@angular/forms";
import {AgGridModule} from "ag-grid-angular";
import {
  BpmnModule,
  ComponentsModule,
  CoreModule,
  ProgressIndicationService,
  SecurityModule
} from "@webbpm/base-package";
import {AppHeaderComponent} from "./component/app-header.component";
import {AppFooterComponent} from "./component/app-footer.component";
import {AccessDeniedComponent} from "./component/access-denied.component";
import {RouterModule} from "@angular/router";
import {InternationalPhoneNumberModule} from "ngx-international-phone-number";
import {AppProgressIndicationComponent} from "./component/app-progress-indication.component";
import {AppProgressIndicationService} from "./service/app-progress-indication.service";
import {TextWithDialogLinks} from "../../ervu/component/textwithdialoglinks/TextWithDialogLinks";
import {LogOutComponent} from "./component/logout.component";
import {LoadForm} from "../../ervu/component/container/LoadForm";
import {InMemoryStaticGrid} from "../../ervu/component/grid/InMemoryStaticGrid";
import {AuthenticationService} from "../security/authentication.service";
import {AuditService} from "../../ervu/service/AuditService";
import {HomeLandingComponent} from "./component/home-landing.component";

registerLocaleData(localeRu);
export const DIRECTIVES = [
  forwardRef(() => AppHeaderComponent),
  forwardRef(() => AppFooterComponent),
  forwardRef(() => LogOutComponent),
  forwardRef(() => AccessDeniedComponent),
  forwardRef(() => AppProgressIndicationComponent),
  forwardRef(() => TextWithDialogLinks),
  forwardRef(() => LoadForm),
  forwardRef(() => InMemoryStaticGrid),
  forwardRef(() => HomeLandingComponent)
];

export function checkAuthentication(authService: AuthenticationService): () => Promise<any> {
  return () => authService.checkAuthentication();
}

@NgModule({
            imports: [
              CommonModule,
              FormsModule,
              CoreModule,
              NgbModule,
              BpmnModule,
              SecurityModule,
              ComponentsModule,
              AgGridModule,
              RouterModule,
              InternationalPhoneNumberModule
            ],
            declarations: [
              DIRECTIVES
            ],
            exports: [
              DIRECTIVES
            ],
            providers: [
              AuthenticationService, AuditService,
              {
                provide: APP_INITIALIZER,
                useFactory: checkAuthentication,
                deps: [AuthenticationService],
                multi: true,
              },
              { provide: ProgressIndicationService, useClass: AppProgressIndicationService }
            ],
            bootstrap: [],
            entryComponents: [AppProgressIndicationComponent]
          })
export class AppModule {
}
