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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ComponentsModule } from './modules/components.module';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgxsModule } from '@ngxs/store';
import { NgxsReduxDevtoolsPluginModule } from '@ngxs/devtools-plugin';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
import { ProcessState } from './core/store/state/process.state';
import { BreadcrumbsState } from './core/store/state/breadcrumbs.state';
import { FilterState } from './core/store/state/filter.state';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { SharedModule } from './shared/shared.module';
import { AutocompleteState } from './core/store/state/autocomplete.state';

import { ScrollingModule } from '@angular/cdk/scrolling';
import { NotifierState } from './core/store/state/notifier.state';
import { environment } from '../environments/environment';
import { ModalModule, IconModule, OfflineOverlayModule } from '@libs/ui';
import { ProductState } from './core/store/state/product.state';
import { CustomerState } from './core/store/state/customer.state';
import { CartState } from './core/store/state/cart.state';
import { AppState } from './core/store/state/app.state';
import { CartEntryState } from './core/store/state/cart-entry.state';
import { BranchState } from './core/store/state/branches.state';
import { SsoModule, SsoInterface } from 'sso';
import { SsoAuthorizationInterceptor, HttpErrorHandlerInterceptor } from './core/interceptors';
import { DatePipe } from '@angular/common';
import { CountryState } from './core/store/state/countries.state';
import { HimaSalesErrorHandler } from './core/error/hima-sales.error-handler';
import { CollectingShelfState } from './core/store/state/collecting-shelf.state';
import 'apps/sales/src/app/core/utils/app.prototypes';
import { VatState } from './core/store/state/vat.state';
import { SupplierState } from './core/store/state/supplier.state';
import { GoodsInState } from './core/store/state/goods-in.state';
import { BranchProcessState } from './core/store/state/branch-process.state';
import { RemissionModule, RemissionModuleOptions } from '@isa/remission';
import { RemissionState } from './core/store/state/remission.state';
import { NgIdleKeepaliveModule } from '@ng-idle/keepalive';
import { AppSwaggerModule } from './app-swagger.module';
import { AppConfiguration } from './app-configuration';
import { FormsState } from './core/store/state/forms.state';

import localeDe from '@angular/common/locales/de';
import localeDeExtra from '@angular/common/locales/extra/de';
import { registerLocaleData } from '@angular/common';
import { RemissionStateHandler } from './core/store/handlers/remission.handlers';
import { SsoConfigurationService } from './core/services/sso-configuration.service';
import { OverlayModule } from '@angular/cdk/overlay';
import { AppStoreModule } from './app-store.module';
import { ModalDialogueModule } from './core/overlay/component';
import { AppModalModule } from './app-modal.module';
import { OverlaysModule } from './core/overlay/overlays.module';
import { DateAdapter, NativeDateAdapter } from '@isa-ui/core/date';
registerLocaleData(localeDe, localeDeExtra);
registerLocaleData(localeDe, 'de', localeDeExtra);

const states = [
  AppState,
  ProcessState,
  BreadcrumbsState,
  FilterState,
  AutocompleteState,
  NotifierState,
  ProductState,
  CustomerState,
  CartState,
  CartEntryState,
  BranchState,
  CountryState,
  CollectingShelfState,
  VatState,
  SupplierState,
  BranchProcessState,
  GoodsInState,
  RemissionState,
  FormsState,
];

export function noop() {
  return function () {};
}

export function remissionModuleOptionsFactory(config: AppConfiguration): RemissionModuleOptions {
  return config.remissionModuleOptions;
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    AppStoreModule,
    AppSwaggerModule,
    AppModalModule,
    ComponentsModule,
    HttpClientModule,
    NgxsModule.forRoot(states, { developmentMode: !environment.production }),
    NgxsReduxDevtoolsPluginModule.forRoot({ name: 'ISA NGXS Store' }),
    NgxsLoggerPluginModule,
    FormsModule,
    ReactiveFormsModule,
    SharedModule,
    ScrollingModule,
    IconModule,
    ModalModule.forRoot(),
    SsoModule.forRoot(environment.production),
    OfflineOverlayModule,
    NgIdleKeepaliveModule.forRoot(),
    RemissionModule.forRoot(undefined),
    OverlayModule,
    OverlaysModule,
    ModalDialogueModule,
  ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: noop,
      deps: [RemissionStateHandler],
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: SsoAuthorizationInterceptor,
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpErrorHandlerInterceptor,
      multi: true,
    },
    {
      provide: RemissionModuleOptions,
      useFactory: remissionModuleOptionsFactory,
      deps: [AppConfiguration],
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpErrorHandlerInterceptor,
      multi: true,
    },
    DatePipe,
    {
      provide: ErrorHandler,
      useClass: HimaSalesErrorHandler,
    },
    { provide: LOCALE_ID, useValue: 'de-DE' },
    {
      provide: SsoInterface,
      useClass: SsoConfigurationService,
    },
    {
      provide: DateAdapter,
      useClass: NativeDateAdapter,
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}
