import { Component, OnInit } from '@angular/core';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { StaticObjectController } from '@objects-view/static-object-controller';
import { PoolsUtils } from '../pools-utils';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { ConfigService } from '@shared/config/config.service';
import { ActivatedRoute, Router } from '@angular/router';
import { LBPoolsApiService } from '@fleio-api/openstack/load-balancing/pools/lb-pools-api.service';
import { ILBMemberModel } from '@fleio-api/openstack/load-balancing/model/lb-member.model';
import { MemberEditFormComponent } from '../tabs/member-edit-form/member-edit-form.component';
import { LBMembersApiService } from '@fleio-api/openstack/load-balancing/members/lb-members-api.service';

@Component({
  selector: 'app-member-edit',
  templateUrl: './member-edit.component.html',
  styleUrls: ['./member-edit.component.scss']
})
export class MemberEditComponent implements OnInit {
  get member(): ILBMemberModel {
    return this.activatedRoute.snapshot.data.member;
  }

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

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

  objectController = new StaticObjectController(
    {
      header: {
        title: {
          text: this.member?.name ? $localize`Edit member ${this.member?.name}` : $localize`Edit member (none)`,
        }
      },
      tabs: [
        {
          tabName: $localize`Edit`,
          tabIdentifier: 'edit',
          component: MemberEditFormComponent,
        },
      ],
      actions: this.member ? PoolsUtils.getMemberActions(
        this.member, this.lbMembersApiService, this.config, this.router, this.loadBalancerId, this.poolId,
      ) : [],
      detailsActions: [
        new RouterLinkAction({
            name: $localize`Back`,
            routerUrl: this.config.getPanelUrl(
              `openstack/load-balancers/${this.loadBalancerId}/pools`,
              this.poolId,
            ),
            fragment: 'members',
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ),
        new CallbackAction({name: $localize`Save`, propagateQueryParams: ['region'],}),
      ]
    },
    this.member,
    {
      createOptions: this.activatedRoute.snapshot.data.createOptions,
    }
  );

  constructor(
    public config: ConfigService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private lbPoolsApiService: LBPoolsApiService,
    private lbMembersApiService: LBMembersApiService,
  ) {
  }

  ngOnInit(): void {
  }
}
