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 { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.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 { PoolsUtils } from '../../pools-utils';
import { ILBMemberModel } from '@fleio-api/openstack/load-balancing/model/lb-member.model';
import { LBMembersApiService } from '@fleio-api/openstack/load-balancing/members/lb-members-api.service';

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

  formatHelper = new FormatHelper();
  memberList: FleioObjectsList<ILBMemberModel>;
  memberActions: { [id: string]: IAction[] } = {};
  displayedColumns = [
    'name', 'address', 'protocol_port', 'weight', 'backup',
    'operating_status', 'provisioning_status', 'admin_state_up', '(actions)',
  ];

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


  updateMemberActions() {
    for (const member of this.memberList.objects) {
      const actions = PoolsUtils.getMemberActions(
        member, this.lbMembersApiService, this.config, this.router, this.loadBalancerId, this.object.id,
      );

      this.memberActions[member.id] = actions;
    }
  }

  refreshData() {
    if (this.activatedRoute.snapshot.queryParams.region) {
      this.lbMembersApiService.setAdditionalQueryParams({
        pool_id: this.object.id,
        region: this.activatedRoute.snapshot.queryParams.region,
      });
    } else {
      this.lbMembersApiService.setAdditionalQueryParams({pool_id: this.object.id});
    }
    this.lbMembersApiService.list().subscribe(memberList => {
      for (const member of memberList.objects) {
        member.name = member.name || $localize`(none)`;
        member.admin_state_up_display = member.admin_state_up ? $localize`Yes` : $localize`No`;
        member.backup_display = member.backup ? $localize`Yes` : $localize`No`;
      }
      this.memberList = memberList;
      this.updateMemberActions();
    });
  }

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