import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';

import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {MastodonApiModule} from "projects/mastodon-api/src/public-api";
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import {SharedModule} from './shared/shared.module';
import {ActionReducer, StoreModule} from '@ngrx/store';
import {StoreDevtoolsModule} from "@ngrx/store-devtools";
import {applicationStateReducer, authenticationStateReducer} from "./shared/state/store/reducers";
import {EffectsModule} from "@ngrx/effects";
import {NgProgressModule} from "ngx-progressbar";
import {NgProgressHttpModule} from "ngx-progressbar/http";
import {AuthorizationModule} from "./authorization/authorization.module";
import {ApplicationStateEffects, AuthorizationStateEffects, fromAuthorize} from "./shared/state/store";
import {HeaderComponent} from './layout/header/header.component';
import {FooterComponent} from './layout/footer/footer.component';
import {ToastModule} from "primeng/toast";
import {InputSwitchModule} from "primeng/inputswitch";
import {FormsModule} from "@angular/forms";


export function handlePersistentState(reducer: ActionReducer<any>): ActionReducer<any> {
    return function (state, action) {
        let nextState = reducer(state, action);
        if (action.type === fromAuthorize.logout.type) {
            localStorage.removeItem('authenticationState');
        }
        if (
            action.type === fromAuthorize.registerApplicationSuccess.type ||
            action.type === fromAuthorize.getAccessTokenSuccess.type
        ) {
            localStorage.setItem('authenticationState', JSON.stringify(nextState));
        }
        if (action.type === fromAuthorize.loadLocalStorage.type) {
            const savedState = JSON.parse(localStorage.getItem('authenticationState') || '{}');
            nextState = {...nextState, ...savedState};
        }
        return nextState;
    }
}

export const metaReducers = [handlePersistentState];

@NgModule({
    bootstrap: [AppComponent],
    declarations: [
        AppComponent,
        HeaderComponent,
        FooterComponent,
    ],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        AppRoutingModule,
        MastodonApiModule.forRoot(),
        AuthorizationModule.forRoot(),
        StoreModule.forRoot({}),
        StoreModule.forFeature('authentication', authenticationStateReducer, {metaReducers}),
        StoreModule.forFeature('application', applicationStateReducer),
        EffectsModule.forRoot(),
        EffectsModule.forFeature(ApplicationStateEffects),
        EffectsModule.forFeature(AuthorizationStateEffects),
        StoreDevtoolsModule.instrument({
            maxAge: 25,
            logOnly: false,
            autoPause: true,
            features: {
                pause: false,
                lock: true,
                persist: true
            }
        }),
        SharedModule,
        NgProgressModule,
        NgProgressHttpModule,
        ToastModule,
        InputSwitchModule,
        FormsModule,
    ],
    providers: []
})
export class AppModule {
}
