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 { IHeatStackResourcesModel } from '@fleio-api/openstack/orchestration/model/heat-stack-resources.model';

@Component({
  selector: 'app-heat-stack-details-resources',
  templateUrl: './heat-stack-details-resources.component.html',
  styleUrls: ['./heat-stack-details-resources.component.scss']
})
export class HeatStackDetailsResourcesComponent extends DetailsComponentBase<IHeatStackModel> {
  formatHelper = new FormatHelper();
  resources: IHeatStackResourcesModel;
  loading = true;
  displayedColumns = [
    'name', 'resource_id', 'resource_type', 'updated_time', 'resource_status', 'resource_status_reason',
  ];

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

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

  loadResources() {
    this.loading = true;
    this.heatStacksApi.resources(this.object.id).subscribe(resources => {
      for (const resource of resources.resources) {
        resource.resource_status = this.formatHelper.formatStatus(resource.resource_status);
        resource.resource_status_reason = resource.resource_status_reason.replace(/\\n/g, '\n');
        switch (resource.resource_type) {
          case 'OS::Cinder::Volume':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/volumes', resource.physical_resource_id,
            );
            break;
          case 'OS::Nova::Server':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/instances', resource.physical_resource_id,
            );
            break;
          case 'OS::Neutron::Router':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/routers', resource.physical_resource_id,
            );
            break;
          case 'OS::Neutron::Port':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/ports', resource.physical_resource_id,
            );
            break;
          case 'OS::Neutron::Net':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/networks', resource.physical_resource_id,
            );
            break;
          case 'OS::Neutron::FloatingIP':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/floating-ips', resource.physical_resource_id,
            );
            break;
          case 'OS::Magnum::Cluster':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/clusters', resource.physical_resource_id,
            );
            break;
          case 'OS::Magnum::ClusterTemplate':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/cluster-templates', resource.physical_resource_id,
            );
            break;
          case 'OS::Glance::Image':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/images', resource.physical_resource_id,
            );
            break;
          case 'OS::Designate::Zone':
            resource.resourceLink = this.config.getPanelUrl(
              'openstack/zones', resource.physical_resource_id,
            );
            break;
        }
      }
      this.resources = resources;
    }).add(() => {
      this.loading = false;
    });
  }
}
