import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { VolumeSnapshotsApiService } from '@fleio-api/openstack/volume/volume-snapshots-api.service';
import { IVolumeSnapshotModel } from '@fleio-api/openstack/model/volume-snapshot.model';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { AuthService } from '@shared/auth/auth.service';

@Injectable({
  providedIn: 'root'
})
export class VolumeSnapshotsResolver  {
  constructor(
    private volumeSnapshotsApiService: VolumeSnapshotsApiService,
    private authService: AuthService,
  ) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<IVolumeSnapshotModel>> | Promise<FleioObjectsList<IVolumeSnapshotModel>> |
    FleioObjectsList<IVolumeSnapshotModel> {
    if (this.authService.feature('openstack.volumes.snapshots')) {
      return this.volumeSnapshotsApiService.list({volume__id: route.params.id}).pipe(
        catchError(() => of(null))
      );
    } else {
      return of(null);
    }
  }
}
