import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { IInstanceModel } from '@fleio-api/openstack/model/instance.model';
import { InstancesApiService } from '@fleio-api/openstack/instance/instances-api.service';
import { IInstanceMigrateOptionsModel } from '@fleio-api/openstack/model/instance-migrate-options.model';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DialogCallbackAction } from '@objects-view/actions/dialog-callback-action';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';

@Component({
  selector: 'app-instance-migrate-dialog',
  templateUrl: './instance-migrate-dialog.component.html',
  styleUrls: ['./instance-migrate-dialog.component.scss']
})
export class InstanceMigrateDialogComponent implements IBaseDialog, OnInit {
  @ViewChild('formErrors') formErrors;
  loading = false;
  instanceMigrateForm = this.formBuilder.group({
    hypervisor: ['', Validators.required],
    block_migration: [false, Validators.required],
    current_hypervisor: [null],
    live_migrate: [true, Validators.required],
    over_commit: [false, Validators.required],
  });
  dialogTitle = $localize`Migrate instance ${this.data?.instance?.name || this.data?.instance?.id}`;
  dialogActions: Array<IAction> = [
    new DialogCallbackAction({
      name: $localize`Migrate instance`,
      primary: true,
      callback: () => this.migrate(),
    }),
  ];
  dialogForm = this.instanceMigrateForm;
  backendErrors = {};
  migrateOptions: IInstanceMigrateOptionsModel | null;
  fieldRequiredTxt = $localize`This field is required!`;

  constructor(
    public dialogRef: MatDialogRef<InstanceMigrateDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: { instance: IInstanceModel },
    private instancesApiService: InstancesApiService,
    private formBuilder: UntypedFormBuilder,
  ) {
  }

  public migrate() {
    this.loading = true;
    if (!this.instanceMigrateForm.valid) {
      this.loading = false;
      return;
    }
    const value = this.instanceMigrateForm.value;
    if (value.hypervisor) {
      value.hypervisor = value.hypervisor.name;
    }
    if (this.data && this.data.instance) {
      this.instancesApiService.migrate(this.data.instance.id, value).pipe().subscribe(() => {
        this.loading = false;
        this.dialogRef.close(true);
      }, error => {
        this.loading = false;
        this.backendErrors = error.error;
        this.formErrors.setBackendErrors(this.backendErrors);
      });
    }
  }

  ngOnInit() {
    this.loading = true;
    this.migrateOptions = null;
    if (this.data && this.data.instance) {
      this.instancesApiService.getMigrateOptions(
        this.data.instance.id,
      ).pipe().subscribe((response: IInstanceMigrateOptionsModel) => {
        this.loading = false;
        this.migrateOptions = response;
        if (this.migrateOptions.hypervisors.length) {
          this.instanceMigrateForm.controls.hypervisor.setValue(this.migrateOptions.hypervisors[0]);
        }
      });
    }
  }

}
