import { Component } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IHeatStackModel } from '@fleio-api/openstack/orchestration/model/heat-stack.model';
import { FormatHelper } from '@shared/ui-api/helpers/format-helper';
import { ConfigService } from '@shared/config/config.service';
import { HeatStacksApiService } from '@fleio-api/openstack/orchestration/heat-stacks/heat-stacks-api.service';
import { IHeatStackTemplateModel } from '@fleio-api/openstack/orchestration/model/heat-stack-template.model';
import { NotificationService } from '@shared/ui-api/notification.service';

@Component({
  selector: 'app-heat-stack-details-template',
  templateUrl: './heat-stack-details-template.component.html',
  styleUrls: ['./heat-stack-details-template.component.scss']
})
export class HeatStackDetailsTemplateComponent extends DetailsComponentBase<IHeatStackModel> {
  formatHelper = new FormatHelper();
  template: IHeatStackTemplateModel;
  loading = true;

  constructor(
    public config: ConfigService,
    private heatStacksApi: HeatStacksApiService,
    private notification: NotificationService,
  ) {
    super();
  }

  protected tabActivated() {
    super.tabActivated();
    this.loadTemplate();
  }

  loadTemplate() {
    if (this.object && this.object.id) {
      this.heatStacksApi.template(this.object.id).subscribe({
        next: template => {
          this.template = template;
        }, error: error => {
          if (error?.error.detail)
            this.notification.showMessage(error?.error.detail)
        }
      }).add(() => {
        this.loading = false;
      });
    }
  }
}
