import { Injector, NgModule, LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import locale from '@angular/common/locales/en';
import { BrowserModule, Title } from '@angular/platform-browser';
import { ServiceWorkerModule } from '@angular/service-worker';
import { FaIconLibrary } from '@fortawesome/angular-fontawesome';
<%_ if (authenticationTypeSession && communicationSpringWebsocket) { _%>
import { CookieService } from 'ngx-cookie-service';
<%_ } _%>
import { NgxWebstorageModule, SessionStorageService } from 'ngx-webstorage';
import * as dayjs from 'dayjs';
import { NgbDateAdapter, NgbDatepickerConfig } from '@ng-bootstrap/ng-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { ApplicationConfigService } from 'app/core/config/application-config.service';
import './config/dayjs';
import { SharedModule } from 'app/shared/shared.module';
import { AppRoutingModule } from './app-routing.module';
import { HomeModule } from './home/home.module';
// jhipster-needle-angular-add-module-import JHipster will add new module here
import { NgbDateDayjsAdapter } from './config/datepicker-adapter';
import { fontAwesomeIcons } from './config/font-awesome-icons';
import { httpInterceptorProviders } from 'app/core/interceptor/index';
import { MainComponent } from './layouts/main/main.component';
import { NavbarComponent } from './layouts/navbar/navbar.component';
import {SidebarComponent} from "./layouts/sidebar/sidebar.component";
import { FooterComponent } from './layouts/footer/footer.component';
import { PageRibbonComponent } from './layouts/profiles/page-ribbon.component';
import { ErrorComponent } from './layouts/error/error.component';
import {NZ_ICONS} from "ng-zorro-antd/icon";
import {NZ_ICONS_DEFINITION} from "./config/nz-icon.config";
import {NZ_I18N, en_US} from "ng-zorro-antd/i18n";
import {EntityViewModalComponent} from "./shared/entity/entity-view/entity-view-modal.component";
import {EntityDeleteModalComponent} from "./shared/entity/entity-delete/entity-delete-modal.component";
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { MissingTranslationHandler, TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core';
import { missingTranslationHandler, translatePartialLoader } from './config/translation.config';


export let InjectorInstance: Injector;

@NgModule({
    imports: [
        BrowserAnimationsModule,
        BrowserModule,
        EntityViewModalComponent,
        EntityDeleteModalComponent,
        SharedModule,
        HomeModule,
        // jhipster-needle-angular-add-module JHipster will add new module here
        AppRoutingModule,
        // Set this to true to enable service worker (PWA)
        ServiceWorkerModule.register('ngsw-worker.js', { enabled: false }),
        HttpClientModule,
        NgxWebstorageModule.forRoot({ prefix: '<%= jhiPrefixDashed %>', separator: '-', caseSensitive: true }),
        <%_ if (enableTranslation) { _%>
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: translatePartialLoader,
                deps: [HttpClient],
            },
            missingTranslationHandler: {
                provide: MissingTranslationHandler,
                useFactory: missingTranslationHandler,
            },
        }),
        <%_ } _%>
        NzLayoutModule,
    ],
    providers: [
        Title,
        <%_ if (authenticationTypeSession && communicationSpringWebsocket) { _%>
        CookieService,
        <%_ } _%>
        {
            provide: NZ_I18N,
            useValue: en_US,
        },
        {
            provide: NZ_ICONS,
            useValue: NZ_ICONS_DEFINITION,
        },
        { provide: LOCALE_ID, useValue: 'en' },
        { provide: NgbDateAdapter, useClass: NgbDateDayjsAdapter },
        httpInterceptorProviders,
    ],
    declarations: [
        MainComponent,
        NavbarComponent,
        SidebarComponent,
        ErrorComponent,
        PageRibbonComponent,
        FooterComponent,
    ],
    bootstrap: [MainComponent],
})
export class AppModule {
    constructor(
        applicationConfigService: ApplicationConfigService,
        iconLibrary: FaIconLibrary,
        dpConfig: NgbDatepickerConfig,
        translateService: TranslateService,
        injector: Injector,
        sessionStorageService: SessionStorageService,
    ) {
    <%_ if (applicationTypeMicroservice) { _%>
    applicationConfigService.setMicrofrontend();
    <%_ } _%>
        InjectorInstance = injector;
        applicationConfigService.setEndpointPrefix(SERVER_API_URL);
        registerLocaleData(locale);
        iconLibrary.addIcons(...fontAwesomeIcons);
        dpConfig.minDate = { year: dayjs().subtract(100, 'year').year(), month: 1, day: 1 };
        translateService.setDefaultLang('ru');
        // if user have changed language and navigates away from the application and back to the application then use previously choosed language
        const langKey = sessionStorageService.retrieve('locale') ?? 'ru';
        translateService.use(langKey);
    }
}
