import { Component, OnInit } from '@angular/core';
import { ILBPolicyModel } from '@fleio-api/openstack/load-balancing/model/lb-policy.model';
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 { ConfigService } from '@shared/config/config.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PolicyEditFormComponent } from '../tabs/policy-edit-form/policy-edit-form.component';
import { CallbackAction } from '@objects-view/actions/callback-action';

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

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

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

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

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

  ngOnInit(): void {
  }
}
