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 {LogOutComponent} from "./component/logout.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 {FileUploadModule} from "ng2-file-upload";
import {ErvuFileUpload} from "../../ervu/component/fileupload/ErvuFileUpload";
import {InMemoryStaticGrid} from "../../ervu/component/grid/InMemoryStaticGrid";
import {ErvuDownloadFileButton} from "../../ervu/component/button/ErvuDownloadFileButton";
import {AuthenticationService} from "../security/authentication.service";
import {AuditService} from "../../ervu/service/AuditService";
import {
  ErvuFileUploadWithAdditionalFiles
} from "../../ervu/component/fileupload/ErvuFileUploadWithAdditionalFiles";
import {ValidateFileService} from "../../ervu/service/ValidateFileService";

registerLocaleData(localeRu);
export const DIRECTIVES = [
  forwardRef(() => AppHeaderComponent),
  forwardRef(() => AppFooterComponent),
  forwardRef(() => LogOutComponent),
  forwardRef(() => AccessDeniedComponent),
  forwardRef(() => AppProgressIndicationComponent),
  forwardRef(() => ErvuFileUpload),
  forwardRef(() => ErvuFileUploadWithAdditionalFiles),
  forwardRef(() => ErvuDownloadFileButton),
  forwardRef(() => InMemoryStaticGrid),
];

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

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