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 { IVolumeModel } from '../model/volume.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';
import { IAttachedVolumesForInstanceResponse } from '@openstack-instances-tabs/instance-details-volumes/instance-details-volumes.component';

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

  changeBootableStatus(volumeId: FleioId, newStatus: boolean): Observable<any> {
    return this.objectPostAction(volumeId, 'change_bootable_status', {new_status: newStatus});
  }

  revertToSnapshot(volumeId: FleioId, snapshotId: FleioId): Observable<any> {
    return this.objectPostAction(volumeId, 'revert_to_snapshot', {snapshot_id: snapshotId});
  }

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

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

  getAttachments(instanceId: FleioId): Observable<IAttachedVolumesForInstanceResponse> {
    return this.getAction('get_attachments', {instance_uuid: instanceId});
  }

  uploadToImage(volumeId: FleioId, parameters: Record<string, any>) {
    return this.objectPostAction(volumeId, 'upload_to_image', parameters);
  }

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