import { Component, OnInit } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { ClipboardService } from '@shared/ui-api/clipboard.service';
import { Observable, of } from 'rxjs';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { ILBRuleModel } from '@fleio-api/openstack/load-balancing/model/lb-rule.model';
import { ILBRuleCreateOptionsModel } from '@fleio-api/openstack/load-balancing/model/lb-rule-create-options.model';
import { LBRulesApiService } from '@fleio-api/openstack/load-balancing/rules/lb-rules-api.service';

@Component({
  selector: 'app-rule-edit-form',
  templateUrl: './rule-edit-form.component.html',
  styleUrls: ['./rule-edit-form.component.scss']
})
export class RuleEditFormComponent extends DetailsFormBase<ILBRuleModel> implements OnInit {
  createOptions: ILBRuleCreateOptionsModel;

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

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

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

  ruleForm = this.formBuilder.group({
    invert: [true],
    type: [null, Validators.required],
    compare_type: [null, Validators.required],
    key: [null],
    value: [null],
    admin_state_up: [false],
  });
  loading = false;

  constructor(
    private formBuilder: UntypedFormBuilder,
    private lbRulesApiService: LBRulesApiService,
    private router: Router,
    private config: ConfigService,
    public clipboard: ClipboardService,
    private activatedRoute: ActivatedRoute,
  ) {
    super();
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.objectController.actionCallback = () => this.saveRule();
      this.createOptions = this.objectController.additionalObjects.createOptions;
    }

    if (this.object && this.object.id) {
      this.ruleForm.patchValue(this.object);
    }

    this.ruleForm.controls.key.disable();
    this.ruleForm.controls.type.valueChanges.subscribe(type => {
      switch (type) {
        case 'HEADER':
        case 'COOKIE':
          this.ruleForm.controls.key.enable();
          break;
        default:
          this.ruleForm.controls.key.disable();
          break;
      }
    });
  }

  saveRule(): Observable<IActionResult> {
    const value = this.ruleForm.value;
    value.listener_id = this.listenerId;
    this.loading = true;
    this.createOrUpdate(
      this.lbRulesApiService,
      value,
    ).subscribe(() => {
      this.router.navigate(
        [
          this.config.getPanelUrl(
            `openstack/load-balancers/${this.loadBalancerId}/listeners/${this.listenerId}/policy/${this.policyId}`,
          )
        ], {
          fragment: 'l7-rules',
          queryParamsHandling: 'merge',
        }
      );
    }).add(() => {
      this.loading = false;
    });

    return of(null);
  }
}
