import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';
import { LBPoolsApiService } from '@fleio-api/openstack/load-balancing/pools/lb-pools-api.service';

@Injectable({
  providedIn: 'root'
})
export class LBPoolsListResolver  {
  constructor(private lbPoolsApiService: LBPoolsApiService) {
  }

  // noinspection JSUnusedLocalSymbols
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<ILBPoolModel>> | Promise<FleioObjectsList<ILBPoolModel>> | FleioObjectsList<ILBPoolModel> {
    let queryParams;
    this.lbPoolsApiService.setAdditionalQueryParams(route.queryParams);
    if (route.params.loadBalancerId) {
      queryParams = {loadbalancer_id: route.params.loadBalancerId};
    } else {
      queryParams = {};
    }
    return this.lbPoolsApiService.list(
      queryParams
    ).pipe(catchError(() => of(null)));
  }
}
