import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { ILoadBalancerModel } from '@fleio-api/openstack/load-balancing/model/load-balancer.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 { LBPoolsApiService } from '@fleio-api/openstack/load-balancing/pools/lb-pools-api.service';
import { PoolsUtils } from '../../lb-pools/pools-utils';
import { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';

@Component({
  selector: 'app-load-balancer-details-pools',
  templateUrl: './load-balancer-details-pools.component.html',
  styleUrls: ['./load-balancer-details-pools.component.scss'],
  providers: [LBPoolsApiService],
})
export class LoadBalancerDetailsPoolsComponent extends DetailsComponentBase<ILoadBalancerModel> implements OnInit {
  formatHelper = new FormatHelper();
  poolList: FleioObjectsList<ILBPoolModel>;
  poolActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'name', 'protocol', 'lb_algorithm', 'operating_status', 'provisioning_status', 'admin_state_up', '(actions)',
  ];
  cannotRetrieve: boolean;

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

  updatePoolActions() {
    for (const pool of this.poolList.objects) {
      const actions = PoolsUtils.getPoolActions(
        pool, this.lbPoolsApiService, this.config, this.router, this.object.id,
      );

      this.poolActions[pool.id] = actions;
    }
  }

  refreshData() {
    if (this.activatedRoute.snapshot.queryParams.region) {
      this.lbPoolsApiService.setAdditionalQueryParams({
        loadbalancer_id: this.object.id,
        region: this.activatedRoute.snapshot.queryParams.region,
      });
    } else {
      this.lbPoolsApiService.setAdditionalQueryParams({loadbalancer_id: this.object.id});
    }
    this.lbPoolsApiService.list().subscribe({
      next: poolList => {
        this.poolList = poolList;
        this.updatePoolActions();
      }, error: () => {
        this.cannotRetrieve = true;
        this.stopRefreshTimer();
      }
    });
  }

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