import { Component, OnInit } from '@angular/core';
import { ILBRuleModel } from '@fleio-api/openstack/load-balancing/model/lb-rule.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 { LBRulesApiService } from '@fleio-api/openstack/load-balancing/rules/lb-rules-api.service';
import { RuleEditFormComponent } from '../tabs/rule-edit-form/rule-edit-form.component';
import { CallbackAction } from '@objects-view/actions/callback-action';

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

  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`Edit rule`,
        }
      },
      tabs: [
        {
          tabName: $localize`Edit`,
          tabIdentifier: 'edit',
          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`Save`, propagateQueryParams: ['region'],}),
      ]
    },
    this.rule,
    {
      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});
  }
}
