import { Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { FleioApiService } from '../../fleio-api.service';
import { HttpClient } from '@angular/common/http';
import { IPortModel } from '../model/port.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PortsApiService extends FleioApiService<IPortModel> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('openstack/ports'));
  }

  addIp(portId: FleioId, parameters: Record<string, any>): Observable<any> {
    return this.objectPostAction(portId, 'add_ip', parameters);
  }

  automaticAddIps(portId: FleioId, region: FleioId, fixedIps): Observable<any> {
    return this.objectPostAction(portId, 'automatic_add_ips', {region, fixed_ips: fixedIps});
  }

  removeIp(portId: FleioId, ip, region: string): Observable<any> {
    return this.objectPostAction(
      portId,
      'remove_ip',
      {
        fixed_ips: [{ip_address: ip.ip_address, subnet_id: ip.subnet_id}],
        region,
      });
  }
}
