import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
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 { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';
import { IAction } from '@objects-view/interfaces/actions/action';
import { LBPoolsApiService } from '@fleio-api/openstack/load-balancing/pools/lb-pools-api.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PoolsUtils } from '../../../lb-pools/pools-utils';
import { ILBListenerModel } from '@fleio-api/openstack/load-balancing/model/lb-listener.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';

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

  formatHelper = new FormatHelper();
  poolList: FleioObjectsList<ILBPoolModel>;
  poolActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'name', 'protocol', 'lb_algorithm', 'operating_status', 'provisioning_status', 'admin_state_up', '(actions)',
  ];

  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.loadBalancerId, this.object.id,
      );

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

  refreshData() {
    if (this.activatedRoute.snapshot.queryParams.region) {
      this.lbPoolsApiService.setAdditionalQueryParams({
        loadbalancer_id: this.loadBalancerId,
        listener_id: this.object.id,
        region: this.activatedRoute.snapshot.queryParams.region,
      });
    } else {
      this.lbPoolsApiService.setAdditionalQueryParams({
        loadbalancer_id: this.loadBalancerId,
        listener_id: this.object.id,
      });
    }
    this.lbPoolsApiService.list().subscribe(poolList => {
      this.poolList = poolList;
      this.updatePoolActions();
    });
  }

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

