import { Component, OnDestroy } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { SettingsOpenstackApiService } from '@fleio-api/core/settings-openstack-api.service';
import { IRegionServices } from '@fleio-api/core/model/region-services.model';

@Component({
  selector: 'app-discovered-services',
  templateUrl: './discovered-services.component.html',
  styleUrls: ['./discovered-services.component.scss']
})
export class DiscoveredServicesComponent
  extends DetailsComponentBase<IBaseFleioObjectModel> implements OnDestroy {

  servicesVersions: IRegionServices[];
  needsSync: boolean;
  loadingServices: boolean;
  needServiceTxt = $localize`Some Fleio functionality needs this service`;
  compatibleTxt = $localize`Version is compatible with Fleio`;
  notCompatibleTxt = $localize`Version is not compatible with Fleio`;

  constructor(private settingsOpenstackApi: SettingsOpenstackApiService) {
    super();
  }

  initTabData() {
    this.loadingServices = true;
    this.settingsOpenstackApi.getServicesVersions().subscribe(serviceVersions => {
      this.servicesVersions = serviceVersions;
      this.needsSync = !!serviceVersions.find(s => s.found_services_count === 0);
    }).add(() => {
      this.loadingServices = false;
    });
  }
}
