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 { 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 { ILBPoolModel } from '@fleio-api/openstack/load-balancing/model/lb-pool.model';
import { PoolEditFormComponent } from '../tabs/pool-edit-form/pool-edit-form.component';

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

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

  get listenerId(): FleioId {
    return this.activatedRoute.snapshot.queryParams.listenerId;
  }

  objectController = new StaticObjectController(
    {
      header: {
        title: {
          text: this.pool?.name || $localize`n/a`,
          subText: this.pool?.provisioning_status,
        }
      },
      tabs: [
        {
          tabName: $localize`Edit`,
          tabIdentifier: 'edit',
          component: PoolEditFormComponent,
        },
      ],
      detailsActions: [
        new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPanelUrl(
              'openstack/load-balancers',
              this.loadBalancerId
            ) + (this.listenerId ? `/listeners/${this.listenerId}` : ''),
            router: this.router,
            fragment: 'pools',
            propagateQueryParams: ['region'],
          }
        ),
        new CallbackAction({name: $localize`Save`, propagateQueryParams: ['region'],}),
      ]
    },
    this.pool,
    {
      createOptions: this.activatedRoute.snapshot.data.createOptions,
    }
  );

  constructor(
    public config: ConfigService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
  ) {
  }

  ngOnInit(): void {
  }
}
