import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { ILBHealthMonitorModel } from '@fleio-api/openstack/load-balancing/model/lb-health-monitor.model';
import { LBHealthMonitorsApiService } from '@fleio-api/openstack/load-balancing/health-monitors/lb-health-monitors-api.service';

@Injectable({
  providedIn: 'root'
})
export class LBPoolsListResolver implements Resolve<FleioObjectsList<ILBHealthMonitorModel>> {
  constructor(private lbHealthMonitorsApiService: LBHealthMonitorsApiService) {
  }

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