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 { ILBListenerModel } from '@fleio-api/openstack/load-balancing/model/lb-listener.model';
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 { ListenerUtils } from '../../listener-utils';
import { ILBRuleModel } from '@fleio-api/openstack/load-balancing/model/lb-rule.model';
import { LBRulesApiService } from '@fleio-api/openstack/load-balancing/rules/lb-rules-api.service';

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

  get policyId(): FleioId {
    return this.activatedRoute.snapshot.params.policyId;
  }

  formatHelper = new FormatHelper();
  ruleList: FleioObjectsList<ILBRuleModel>;
  ruleActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'type', 'key', 'value', 'invert', 'compare_type', 'operating_status', 'provisioning_status',
    'admin_state_up', '(actions)',
  ];

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

  updateRuleActions() {
    for (const rule of this.ruleList.objects) {
      const actions = ListenerUtils.getRuleActions(
        rule, this.lbRulesApiService, this.config, this.router, this.loadBalancerId, this.object.id,
        this.policyId,
      );

      this.ruleActions[rule.id] = actions;
    }
  }

  refreshData() {
    if (this.activatedRoute.snapshot.queryParams.region) {
      this.lbRulesApiService.setAdditionalQueryParams({
        policy_id: this.policyId,
        region: this.activatedRoute.snapshot.queryParams.region,
      });
    } else {
      this.lbRulesApiService.setAdditionalQueryParams({policy_id: this.policyId});
    }
    this.lbRulesApiService.list().subscribe(ruleList => {
      this.ruleList = ruleList;
      this.updateRuleActions();
    });
  }

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

