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 { LBListenersApiService } from '@fleio-api/openstack/load-balancing/listeners/lb-listeners-api.service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { ILBListenerModel } from '@fleio-api/openstack/load-balancing/model/lb-listener.model';
import { IAction } from '@objects-view/interfaces/actions/action';
import { ActivatedRoute, Router } from '@angular/router';
import { ListenerUtils } from '../../lb-listeners/listener-utils';

@Component({
  selector: 'app-load-balancer-details-listeners',
  templateUrl: './load-balancer-details-listeners.component.html',
  styleUrls: ['./load-balancer-details-listeners.component.scss'],
  providers: [LBListenersApiService],
})
export class LoadBalancerDetailsListenersComponent extends DetailsComponentBase<ILoadBalancerModel> implements OnInit {
  formatHelper = new FormatHelper();
  listenerList: FleioObjectsList<ILBListenerModel>;
  listenerActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'name', 'protocol', 'protocol_port', 'operating_status', 'provisioning_status', 'admin_state_up', '(actions)',
  ];
  cannotRetrieve: boolean;

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

  updateListenerActions() {
    for (const listener of this.listenerList.objects) {
      const actions = ListenerUtils.getListenerActions(
        listener, this.lbListenersApiService, this.config, this.router, this.object.id,
      );

      this.listenerActions[listener.id] = actions;
    }
  }

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

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