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 { IImageModel } from '../model/image.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';

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

  deactivate(imageId: FleioId): Observable<any> {
    return this.objectPostAction(imageId, 'deactivate', {});
  }

  reactivate(imageId: FleioId): Observable<any> {
    return this.objectPostAction(imageId, 'reactivate', {});
  }

  availableToClient(imageId: FleioId, clientId: FleioId): Observable<{ needsSharing: boolean }> {
    return this.objectGetAction(imageId, 'available_to_client', {client: clientId});
  }

  getSnapshotsForInstance(instanceId: FleioId): Observable<any> {
    return this.getAction(
      'get_snapshots_for_instance',
      {
        instance_uuid: instanceId
      });
  }

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

  copyImage(imageId: FleioId, destinationRegionId: FleioId, destinationName: string, deleteSourceImage: boolean) {
    return this.objectPostAction(imageId, 'copy-image', {
      destination_region_id: destinationRegionId,
      destination_name: destinationName,
      delete_source_image: deleteSourceImage,
    });
  }
}
