import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { IVolumeBackupModel } from '../model/volume-backup.model';
import { FleioObjectsList } from '../../fleio-objects-list';
import { VolumeBackupsApiService } from './volume-backups-api.service';
import { AuthService } from '@shared/auth/auth.service';

@Injectable({
  providedIn: 'root'
})
export class VolumeBackupsResolver  {
  constructor(
    private volumeBackupsApiService: VolumeBackupsApiService,
    private authService: AuthService,
  ) {
  }

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