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 { IZoneModel } from '@fleio-api/openstack/zone/model/zone.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';
import { IRecordListModel } from '@fleio-api/openstack/zone/model/record-list.model';
import { IRecordsetModel } from '@fleio-api/openstack/zone/model/recordset.model';
import { FloatingIPPTRModel } from '@fleio-api/openstack/zone/model/floating-ip-ptr.model';

@Injectable({
  providedIn: 'root'
})
export class ZonesApiService extends FleioApiService<IZoneModel> {
  // noinspection JSUnusedGlobalSymbols
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('openstack/dns'));
  }

  listRecords(zoneId: FleioId): Observable<IRecordListModel> {
    return this.objectGetAction(zoneId, 'list_records');
  }

  synchronizeRecords(zoneId: FleioId, recordsets: IRecordsetModel[]): Observable<IRecordListModel> {
    return this.objectPostAction(zoneId, 'synchronize_records', recordsets);
  }

  getPtrFromIp(ip: any, region: string): Observable<{
    record: any;
    zone_id: string;
  }> {
    return this.getAction('get_ptr_from_ip', {
      ip,
      region_name: region
    });
  }

  createOrUpdatePTR(ptr: any) {
    return this.postAction('create_or_update_ptr', ptr);
  }

  setFloatingIpPTR(floatingIpID, ptrdname, description, ttl,) {
    return this.postAction('set_floating_ip_ptr', {
      floating_ip_id: floatingIpID,
      ptrdname,
      description,
      ttl,
    });
  }

  getFloatingIpPTR(floatingIpID): Observable<FloatingIPPTRModel> {
    return this.getAction('get_floating_ip_ptr', {
      floating_ip_id: floatingIpID
    });
  }

  getPrice(data): Observable<any> {
    return this.getAction('get_price', data);
  }
}
