import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { ILBListenerModel } from '@fleio-api/openstack/load-balancing/model/lb-listener.model';
import { FormatHelper } from '@shared/ui-api/helpers/format-helper';
import { ConfigService } from '@shared/config/config.service';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IAction } from '@objects-view/interfaces/actions/action';
import { ActivatedRoute, Router } from '@angular/router';
import { ILBPolicyModel } from '@fleio-api/openstack/load-balancing/model/lb-policy.model';
import { LBPoliciesApiService } from '@fleio-api/openstack/load-balancing/policies/lb-policies-api.service';
import { ListenerUtils } from '../../listener-utils';

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

  formatHelper = new FormatHelper();
  policyList: FleioObjectsList<ILBPolicyModel>;
  policyActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'name', 'position', 'action', 'operating_status', 'provisioning_status', 'admin_state_up', '(actions)',
  ];

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

  updatePolicyActions() {
    for (const policy of this.policyList.objects) {
      const actions = ListenerUtils.getPolicyActions(
        policy, this.lbPoliciesApiService, this.config, this.router, this.loadBalancerId, this.object.id,
      );

      this.policyActions[policy.id] = actions;
    }
  }

  refreshData() {
    if (this.activatedRoute.snapshot.queryParams.region) {
      this.lbPoliciesApiService.setAdditionalQueryParams({
        listener_id: this.object.id,
        region: this.activatedRoute.snapshot.queryParams.region,
      });
    } else {
      this.lbPoliciesApiService.setAdditionalQueryParams({listener_id: this.object.id});
    }
    this.lbPoliciesApiService.list().subscribe(policyList => {
      this.policyList = policyList;
      this.updatePolicyActions();
    });
  }

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

