import { NgModule, APP_INITIALIZER } from '@angular/core';

import axios from 'axios';

import { AXIOS_INSTANCE } from '@ucap/ng-core';
import { UCAP_NATIVE_SERVICE } from '@ucap/ng-native';

import { environment } from '@environments';

import { AppAuthenticationGuard } from './guards/app-authentication.guard';

import { AppSessionResolver } from './resolvers/app-session.resolver';

import { AppAuthenticationService } from './services/app-authentication.service';
import { AppNativeService } from './services/app-native.service';
import { AppService } from './services/app.service';

const GUARDS = [AppAuthenticationGuard];
const RESOLVERS = [AppSessionResolver];
const SERVICES = [AppService, AppAuthenticationService, AppNativeService];

const axiosFactory = () => {
  const i = axios.create();

  i.defaults.timeout = 15 * 1000;

  return i;
};

const appInit = (appService: AppService) => {
  return () => appService.initialize();
};

@NgModule({
  imports: [],
  exports: [],
  providers: [
    {
      provide: AXIOS_INSTANCE,
      useFactory: axiosFactory,
      deps: [],
      multi: false
    },
    {
      provide: UCAP_NATIVE_SERVICE,
      useClass: environment.productConfig.nativeServiceClass,
      deps: [AXIOS_INSTANCE],
      multi: false
    },
    {
      provide: APP_INITIALIZER,
      useFactory: appInit,
      deps: [AppService],
      multi: true
    },
    ...SERVICES,
    ...GUARDS,
    ...RESOLVERS
  ]
})
export class AppProviderModule {}
