import { Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { HttpClient } from '@angular/common/http';
import { FleioApiService } from '@fleio-api/fleio-api.service';
import { IHeatStackModel } from '@fleio-api/openstack/orchestration/model/heat-stack.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs/internal/Observable';
import { IHeatStackTemplateModel } from '@fleio-api/openstack/orchestration/model/heat-stack-template.model';
import { IHeatStackResourcesModel } from '@fleio-api/openstack/orchestration/model/heat-stack-resources.model';
import { IHeatStackEventsModel } from '@fleio-api/openstack/orchestration/model/heat-stack-events.model';
import { ITemplateValidationResultsModel } from '@fleio-api/openstack/orchestration/model/template-validation-results.model';

@Injectable({
  providedIn: 'root',
})
export class HeatStacksApiService extends FleioApiService<IHeatStackModel> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('openstack/heat-stacks'));
  }

  template(stackId: FleioId): Observable<IHeatStackTemplateModel> {
    return this.objectGetAction(stackId, 'template');
  }

  resources(stackId: FleioId): Observable<IHeatStackResourcesModel> {
    return this.objectGetAction(stackId, 'resources');
  }

  events(stackId: FleioId): Observable<IHeatStackEventsModel> {
    return this.objectGetAction(stackId, 'events');
  }

  validateTemplate(templateData: FormData): Observable<ITemplateValidationResultsModel> {
    return this.postActionWithUpload(
      templateData, 'validate_template'
    ) as unknown as Observable<ITemplateValidationResultsModel>;
  }
}
