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 { IClusterModel } from '@fleio-api/openstack/model/cluster.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { IApiUserModel } from '@fleio-api/openstack/model/api-user.model';
import {Observable} from 'rxjs';
import { IClusterResizeOptionsModel } from '@fleio-api/openstack/model/cluster-resize-options.model';

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

  resizeCluster(objectId: FleioId, params: {
    node_count: number;
    nodes_to_remove: string;
  }) {
    return this.objectPostAction(objectId, 'resize_cluster', params);
  }

  upgradeCluster(objectId: FleioId, clusterTemplate: string) {
    return this.objectPostAction(objectId, 'upgrade', {
      cluster_template: clusterTemplate
    });
  }

  getFile(objectId: FleioId, action: string, apiUser: IApiUserModel, password: string) {
    return this.objectPostAction(objectId, action, {
      os_user_api_username: apiUser.name,
      os_user_api_password: password,
      os_user_api_domain_id: apiUser.domain.id
    });
  }

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

  resizeOptions(clusterId: FleioId): Observable<IClusterResizeOptionsModel> {
    return this.objectGetAction(clusterId, 'resize_options');
  }
}
