import {LOCALE_ID, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {HttpClientModule, HTTP_INTERCEPTORS, HttpClient} from '@angular/common/http';

import {AppRoutingModule} from './app-routing.module';
import {JwtInterceptor, ErrorInterceptor} from './_helpers';
import {AppComponent} from './app.component';
import {AlertComponent} from './_components';
import {HomeComponent} from './_views/home';
import {NgbModule} from "@ng-bootstrap/ng-bootstrap";

import {environment} from "@environments/environment";
import {MatCardModule} from "@angular/material/card";
import {TranslateLoader, TranslateModule} from "@ngx-translate/core";
import {TranslateHttpLoader} from "@ngx-translate/http-loader";
import {NgOptimizedImage, registerLocaleData} from "@angular/common";
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import {TwoColumnComponent} from "./_components/layout/two-column/two-column.component";
import {PropertyInterceptor} from "@app/_helpers/property.interceptor";
import {MatPaginatorModule} from "@angular/material/paginator";
import {MatSortModule} from "@angular/material/sort";
import {MatTableModule} from "@angular/material/table";
import {MatOptionModule} from "@angular/material/core";
import {MatAutocompleteModule} from "@angular/material/autocomplete";
import {MatFormFieldModule} from "@angular/material/form-field";
import {MatInputModule} from "@angular/material/input";
import {ToggleComponent} from './_components/toggle/toggle.component';
import {ProfileComponent} from './_views/profile/profile.component';
import {ApiModule, Configuration, ConfigurationParameters} from "@app/core/api/v1";
import {SearchInputComponent} from './_components/search-input/search-input.component';
import {PagingComponent} from '@app/_components/paging/paging.component';
import {UsersComponent} from './_views/user/users.component';
import {UserDetailComponent} from './_views/user/user-detail/user-detail.component';
import {UserListComponent} from './_views/user/user-list/user-list.component';
import {LinkedLabelComponent} from './_components/linked-label/linked-label.component';
import {LoadingInterceptor} from "@app/_helpers/loading-interceptor.service";
import {SearchSelectComponent} from './_components/search-select/search-select.component';
import {ListComponent} from './_components/list/list.component';
import {MatTabsModule} from "@angular/material/tabs";
import {DashboardComponent} from './_views/dashboard/dashboard.component';
import localeDe from '@angular/common/locales/de';
import { FilterBarComponent } from './_components/filter-bar/filter-bar.component';
import { LocationComponent } from './_views/location/location.component';
import { ZoneComponent } from './_views/zone/zone.component';
import { ShippingCompanyComponent } from './_views/shipping-company/shipping-company.component';
import { TripComponent } from './_views/trip/trip.component';
import { VesselComponent } from './_views/vessel/vessel.component';
import { BaseDataComponent } from './_views/base-data/base-data.component';
import { LocationListComponent } from './_views/location/location-list/location-list.component';
import { ZoneListComponent } from './_views/zone/zone-list/zone-list.component';
import { VesselListComponent } from './_views/vessel/vessel-list/vessel-list.component';
import { ShippingCompanyListComponent } from './_views/shipping-company/shipping-company-list/shipping-company-list.component';
import { LocationDetailComponent } from './_views/location/location-detail/location-detail.component';
import { ZoneDetailComponent } from './_views/zone/zone-detail/zone-detail.component';
import { VesselDetailComponent } from './_views/vessel/vessel-detail/vessel-detail.component';
import { ShippingCompanyDetailComponent } from './_views/shipping-company/shipping-company-detail/shipping-company-detail.component';
import { LocationFormComponent } from './_views/location/location-form/location-form.component';
import { ZoneFormComponent } from './_views/zone/zone-form/zone-form.component';
import { VesselFormComponent } from './_views/vessel/vessel-form/vessel-form.component';
import { ShippingCompanyFormComponent } from './_views/shipping-company/shipping-company-form/shipping-company-form.component';
import { TripListComponent } from './_views/trip/trip-list/trip-list.component';
import { TripDetailComponent } from './_views/trip/trip-detail/trip-detail.component';
import { TripFormComponent } from './_views/trip/trip-form/trip-form.component';
import {DatetimePickerComponent} from "@app/_components/datetime-picker/datetime-picker.component";
import { UserTripComponent } from './_views/user-trip/user-trip.component';
import { UserTripListComponent } from './_views/user-trip/user-trip-list/user-trip-list.component';
import { UserTripDetailComponent } from './_views/user-trip/user-trip-detail/user-trip-detail.component';
import { UserTripFormComponent } from './_views/user-trip/user-trip-form/user-trip-form.component';
import { UserTripEventComponent } from './_views/user-trip-event/user-trip-event.component';
import { UserTripEventListComponent } from './_views/user-trip-event/user-trip-event-list/user-trip-event-list.component';
import { UserFormComponent } from './_views/user/user-form/user-form.component';
import { ImageUploadComponent } from './_components/image-upload/image-upload.component';
import { TripLocationListComponent } from './_views/trip/trip-location-list/trip-location-list.component';
import { TripLocationFormComponent } from './_views/trip/trip-location-form/trip-location-form.component';

registerLocaleData(localeDe, 'de-DE');

export function apiConfigFactory(): Configuration {
    const params: ConfigurationParameters = {
        basePath: environment.basePath,
        withCredentials: false,
        credentials: {
            'JWT': () => localStorage.getItem('token') || undefined
        }
    };
    return new Configuration(params);
}

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http);
}

@NgModule({
    imports: [
        ApiModule.forRoot(apiConfigFactory),
        BrowserModule,
        BrowserAnimationsModule,
        TranslateModule.forRoot({
            defaultLanguage: 'en',
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient]
            }
        }),
        ReactiveFormsModule,
        HttpClientModule,
        NgbModule,
        AppRoutingModule,
        MatCardModule,
        NgOptimizedImage,
        MatPaginatorModule,
        MatSortModule,
        MatTableModule,
        MatOptionModule,
        MatAutocompleteModule,
        MatFormFieldModule,
        MatInputModule,
        MatTabsModule,
        TranslateModule,
        FormsModule,
    ],
    declarations: [
        AppComponent,
        AlertComponent,
        HomeComponent,
        TwoColumnComponent,
        ToggleComponent,
        ProfileComponent,
        SearchInputComponent,
        PagingComponent,
        DatetimePickerComponent,
        UsersComponent,
        UserDetailComponent,
        UserListComponent,
        LinkedLabelComponent,
        SearchSelectComponent,
        ListComponent,
        DashboardComponent,
        FilterBarComponent,
        LocationComponent,
        ZoneComponent,
        ShippingCompanyComponent,
        TripComponent,
        VesselComponent,
        BaseDataComponent,
        LocationListComponent,
        ZoneListComponent,
        VesselListComponent,
        ShippingCompanyListComponent,
        LocationDetailComponent,
        ZoneDetailComponent,
        VesselDetailComponent,
        ShippingCompanyDetailComponent,
        LocationFormComponent,
        ZoneFormComponent,
        VesselFormComponent,
        ShippingCompanyFormComponent,
        TripListComponent,
        TripDetailComponent,
        TripFormComponent,
        UserTripComponent,
        UserTripListComponent,
        UserTripDetailComponent,
        UserTripFormComponent,
        UserTripEventComponent,
        UserTripEventListComponent,
        UserFormComponent,
        ImageUploadComponent,
        TripLocationListComponent,
        TripLocationFormComponent,
    ],
    providers: [
        {provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true},
        {provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true},
        {provide: HTTP_INTERCEPTORS, useClass: PropertyInterceptor, multi: true},
        {provide: HTTP_INTERCEPTORS, useClass: LoadingInterceptor, multi: true},
        {provide: LOCALE_ID, useValue: 'de-DE'},
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}
