import { BrowserModule } from "@angular/platform-browser";
import { APP_INITIALIZER, NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { DatePipe } from '@angular/common';

import { AppComponent } from "./app.component";
import { environment } from "../environments/environment";

import { SummaryComponent } from "./summary/summary.component";
import { ListOfJumpsComponent } from "./list-of-jumps/list-of-jumps.component";
import { ListOfDzsComponent } from "./list-of-dzs/list-of-dzs.component";
import { NewJumpComponent } from "./new-jump/new-jump.component";
import { ListOfAircraftsComponent } from "./list-of-aircrafts/list-of-aircrafts.component";
import { ListOfJumpTypesComponent } from "./list-of-jump-types/list-of-jump-types.component";
import { ListOfGearsComponent } from "./list-of-gears/list-of-gears.component";
import { NewAircraftComponent } from "./new-aircraft/new-aircraft.component";
import { NewGearComponent } from "./new-gear/new-gear.component";
import { NewDropZoneComponent } from "./new-drop-zone/new-drop-zone.component";
import { NewJumpTypeComponent } from "./new-jump-type/new-jump-type.component";
import { DefaultComponent } from "./default/default.component";
import { LoginComponent } from "./login/login.component";
import { CreateUserComponent } from "./create-user/create-user.component";
import { LoginUserComponent } from "./login-user/login-user.component";

import { DateService } from "../services/date.service";
import { AircraftService } from "../services/aircraft.service";
import { DropzoneService } from "../services/dropzone.service";
import { GearService } from "../services/gear.service";
import { JumpService } from "../services/jump.service";
import { JumpTypeService } from "../services/jump-type.service";
import { StatsService } from "../services/stats.service";
import { ServiceComm } from "../services/service-comm.service";
import { RequestCache } from "../services/request-cache.service";
import { AuthGuardService } from "../services/auth-guard.service";
import { ImageService } from "../services/image.service";
import { ConfigurationHelper } from "../services/configuration-helper";
import { ServiceRefData } from "../services/service-ref-data.service";

import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { MatAutocompleteModule } from "@angular/material/autocomplete";
import { MatButtonModule } from "@angular/material/button";
import { MatCheckboxModule } from "@angular/material/checkbox";
import { MatOptionModule, MatNativeDateModule } from "@angular/material/core";
import { MatDatepickerModule } from "@angular/material/datepicker";
import { MatFormFieldModule } from "@angular/material/form-field";
import { MatIconModule } from "@angular/material/icon";
import { MatInputModule } from "@angular/material/input";
import { MatPaginatorModule } from "@angular/material/paginator";
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner";
import { MatSelectModule } from "@angular/material/select";
import { MatTableModule } from "@angular/material/table";
import { MatTabsModule } from "@angular/material/tabs";
import { MatDialogModule } from "@angular/material/dialog";
import { MatCardModule } from "@angular/material/card";
import { MatRadioModule } from "@angular/material/radio";
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatListModule } from '@angular/material/list';
import { MatToolbarModule } from '@angular/material/toolbar';

import { CachingInterceptor } from "../interceptor/caching.interceptor";
//import { BasicAuthInterceptor } from '../interceptor/basic-auth.interceptor';
import { JwtAuthInterceptor } from "../interceptor/jwt-auth.interceptor";
import { ErrorInterceptor } from "../interceptor/error.interceptor";
import { UserProfileComponent } from "./user-profile/user-profile.component";
import { ListOfImagesComponent } from "./list-of-images/list-of-images.component";
import { JumpInfosComponent } from './jump-infos/jump-infos.component';

const appRoutes: Routes = [
  { path: "", component: DefaultComponent, canActivate: [AuthGuardService] },
  {
    path: "summary",
    component: SummaryComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "jumps",
    component: ListOfJumpsComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "dzs",
    component: ListOfDzsComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "newjump",
    component: NewJumpComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "aircrafts",
    component: ListOfAircraftsComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "jumpTypes",
    component: ListOfJumpTypesComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "gears",
    component: ListOfGearsComponent,
    canActivate: [AuthGuardService]
  },
  {
    path: "user",
    component: UserProfileComponent,
    canActivate: [AuthGuardService]
  },
  { path: "login", component: LoginComponent },
];

// Déclaration de la fonction d'initialisation de la configuration
export function initConfig(configService: ConfigurationHelper) {
  return () => configService.load(environment.env);
}

@NgModule({
  declarations: [
    AppComponent,
    SummaryComponent,
    ListOfJumpsComponent,
    ListOfDzsComponent,
    NewJumpComponent,
    ListOfAircraftsComponent,
    ListOfJumpTypesComponent,
    ListOfGearsComponent,
    NewAircraftComponent,
    NewGearComponent,
    NewDropZoneComponent,
    NewJumpTypeComponent,
    DefaultComponent,
    LoginComponent,
    CreateUserComponent,
    LoginUserComponent,
    UserProfileComponent,
    ListOfImagesComponent,
    JumpInfosComponent,
  ],
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: !environment.production, relativeLinkResolution: 'legacy' } // <-- debugging purposes only
    ),
    ReactiveFormsModule,
    FormsModule,
    BrowserModule,
    BrowserAnimationsModule,
    MatPaginatorModule,
    MatTableModule,
    MatSelectModule,
    MatOptionModule,
    MatFormFieldModule,
    MatCheckboxModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatInputModule,
    MatButtonModule,
    MatIconModule,
    MatAutocompleteModule,
    MatProgressSpinnerModule,
    MatTabsModule,
    MatDialogModule,
    MatCardModule,
    MatRadioModule,
    MatSidenavModule,
    MatListModule,
    MatToolbarModule
  ],
  exports: [HttpClientModule],
  providers: [
    ImageService,
    AircraftService,
    DropzoneService,
    GearService,
    JumpService,
    JumpTypeService,
    StatsService,
    ServiceComm,
    DateService,
    RequestCache,
    ConfigurationHelper,
    DatePipe,
    ServiceRefData,
    { provide: APP_INITIALIZER, useFactory: initConfig, deps: [ConfigurationHelper], multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: JwtAuthInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }
    // { provide: HTTP_INTERCEPTORS, useClass: CachingInterceptor, multi: true }
  ],
  bootstrap: [AppComponent],
})
export class AppModule {
}
