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 { RuleEditFormComponent } from '../tabs/rule-edit-form/rule-edit-form.component';
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 { LBRulesApiService } from '@fleio-api/openstack/load-balancing/rules/lb-rules-api.service';

@Component({
  selector: 'app-rule-create',
  templateUrl: './rule-create.component.html',
  styleUrls: ['./rule-create.component.scss'],
  providers: [LBRulesApiService],
})
export class RuleCreateComponent implements OnInit {
  get loadBalancerId(): FleioId {
    return this.activatedRoute.snapshot.params.loadBalancerId;
  }

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

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

  objectController = new StaticObjectController(
    {
      header: {
        title: {
          text: $localize`Create rule`,
        }
      },
      tabs: [
        {
          tabName: $localize`Create`,
          tabIdentifier: 'create',
          component: RuleEditFormComponent,
        },
      ],
      detailsActions: [
        new RouterLinkAction({
            name: $localize`Back`,
            routerUrl: this.config.getPanelUrl(
              `openstack/load-balancers/${this.loadBalancerId}/listeners/${this.listenerId}/policy`,
              this.policyId,
            ),
            fragment: 'l7-rules',
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ),
        new CallbackAction({name: $localize`Create`, propagateQueryParams: ['region'],}),
      ]
    },
    null,
    {
      createOptions: this.activatedRoute.snapshot.data.createOptions,
    }
  );

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

  ngOnInit(): void {
    this.lbRulesApiService.setAdditionalQueryParams({policy_id: this.policyId});
  }
}
