import { Component, OnInit } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { ISecurityGroupModel } from '@fleio-api/openstack/model/security-group.model';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { SecurityGroupsApiService } from '@fleio-api/openstack/security-groups/security-groups-api.service';
import { of } from 'rxjs';
import { ISecurityGroupCreateOptionsModel } from '@fleio-api/openstack/model/security-group-create-options.model';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { ValidatorsHelper } from '@shared/ui-api/helpers/validators-helper';

@Component({
  selector: 'app-security-group-edit-form',
  templateUrl: './security-group-edit-form.component.html',
  styleUrls: ['./security-group-edit-form.component.scss']
})
export class SecurityGroupEditFormComponent extends DetailsFormBase<ISecurityGroupModel> implements OnInit {
  securityGroupForm = this.formBuilder.group({
    region: ['', Validators.required],
    name: ['', Validators.required],
    description: ['']
  });
  createOptions: ISecurityGroupCreateOptionsModel;
  isCreateForm = false;
  selectClient = false;
  createInClientProject = true;

  constructor(
    private formBuilder: UntypedFormBuilder,
    private securityGroupsApiService: SecurityGroupsApiService,
    private router: Router,
    private config: ConfigService,
    private notificationService: NotificationService,
  ) {
    super();
  }

  changeDestination(value) {
    // createInClientProject
    if (value === false) {
      this.securityGroupForm.controls.client.setValue('');
      this.securityGroupForm.controls.client.clearValidators();
      this.securityGroupForm.controls.client.reset();
    } else {
      this.securityGroupForm.controls.client.setValidators(Validators.required);
    }
  }

  saveSecGroup() {
    let value = this.securityGroupForm.value;
    if (this.isCreateForm) {
      if (value.client && value.client.id) {
        value.client = value.client.id;
      }
    } else {
      const name = value.name;
      const description = value.description;
      value = this.object;
      value.name = name;
      value.description = description;
    }
    if (this.isCreateForm && !this.createInClientProject) {
      value.create_as_admin = true;
    }
    this.createOrUpdate(
      this.securityGroupsApiService,
      value,
      true
    ).subscribe(() => {
      if (!this.isCreateForm) {
        this.notificationService.showMessage($localize`Security group update scheduled`);
      }
      this.redirectAfterCreate('openstack/security-groups', this.router, this.config);
    });
    return of(null);
  }

  ngOnInit(): void {
    super.ngOnInit();
    if (this.objectController) {
      this.objectController.actionCallback = () => this.saveSecGroup();
    }
    if (this.config.authService.feature('ui.openstack.security-groups.create.client')) {
      this.selectClient = true;
      this.securityGroupForm.addControl(
        'client',
        this.formBuilder.control(
          '', [Validators.required, ValidatorsHelper.requiredObjectValidator]
        )
      );
    }
    if (this.object && this.object.id) {
      this.securityGroupForm.patchValue(this.object);
      this.securityGroupForm.controls.region.disable();
      if (this.selectClient) {
        this.securityGroupForm.controls.client.disable();
      }
    } else {
      this.isCreateForm = true;
      if (this.objectController) {
        this.createOptions = this.objectController.additionalObjects.createOptions;
      }
      if (this.createOptions && this.createOptions.selected_region) {
        this.securityGroupForm.controls.region.setValue(this.createOptions.selected_region);
      }
    }
  }

}
