import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';
import { FormatHelper } from '@shared/ui-api/helpers/format-helper';
import { ConfigService } from '@shared/config/config.service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IAction } from '@objects-view/interfaces/actions/action';
import { ActivatedRoute, Router } from '@angular/router';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { ILBHealthMonitorModel } from '@fleio-api/openstack/load-balancing/model/lb-health-monitor.model';
import {
  LBHealthMonitorsApiService
} from '@fleio-api/openstack/load-balancing/health-monitors/lb-health-monitors-api.service';
import { PoolsUtils } from '../../pools-utils';

@Component({
  selector: 'app-pool-details-healthmonitors',
  templateUrl: './pool-details-healthmonitors.component.html',
  styleUrls: ['./pool-details-healthmonitors.component.scss'],
  providers: [LBHealthMonitorsApiService],
})
export class PoolDetailsHealthmonitorsComponent extends DetailsComponentBase<ILBPoolModel> implements OnInit {
  get loadBalancerId(): FleioId {
    return this.activatedRoute.snapshot.params.loadBalancerId;
  }

  formatHelper = new FormatHelper();
  monitorList: FleioObjectsList<ILBHealthMonitorModel>;
  monitorActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'name', 'type', 'provisioning_status', 'admin_state_up', '(actions)',
  ];

  constructor(
    private lbHealthMonitorsApiService: LBHealthMonitorsApiService,
    public config: ConfigService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, config);
  }


  updateMonitorActions() {
    for (const monitor of this.monitorList.objects) {
      const actions = PoolsUtils.getMonitorActions(
        monitor, this.lbHealthMonitorsApiService, this.config, this.router, this.loadBalancerId, this.object.id,
      );

      this.monitorActions[monitor.id] = actions;
    }
  }

  refreshData() {
    if (this.activatedRoute.snapshot.queryParams.region) {
      this.lbHealthMonitorsApiService.setAdditionalQueryParams({
        pool_id: this.object.id,
        region: this.activatedRoute.snapshot.queryParams.region,
      });
    } else {
      this.lbHealthMonitorsApiService.setAdditionalQueryParams({pool_id: this.object.id});
    }
    this.lbHealthMonitorsApiService.list().subscribe(monitorList => {
      for (const monitor of monitorList.objects) {
        monitor.name = monitor.name || $localize`(none)`;
      }
      this.monitorList = monitorList;
      this.updateMonitorActions();
    });
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.object?.id) {
      if (this.activatedRoute.snapshot.queryParams.region) {
        this.lbHealthMonitorsApiService.setAdditionalQueryParams({
          pool_id: this.object.id,
          region: this.activatedRoute.snapshot.queryParams.region,
        });
      } else {
        this.lbHealthMonitorsApiService.setAdditionalQueryParams({pool_id: this.object.id});
      }
    }
  }
}
