import { Component, Inject, OnInit, ViewChild } from '@angular/core';
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 { UntypedFormBuilder } from '@angular/forms';
import { ConfigService } from '@shared/config/config.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { IInstanceCreateOptions } from '@fleio-api/openstack/model/instance-create-options';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DialogCallbackAction } from '@objects-view/actions/dialog-callback-action';

@Component({
  selector: 'app-instance-unshelve-dialog',
  templateUrl: './instance-unshelve-dialog.component.html',
  styleUrls: ['./instance-unshelve-dialog.component.scss']
})
export class InstanceUnshelveDialogComponent implements IBaseDialog, OnInit {
  @ViewChild('formErrors') formErrors;
  loading = false;
  instanceUnshelveForm = this.formBuilder.group({
    availabilityZone: [''],
  });
  dialogTitle = $localize`Unshelve instance ${this.data.instance.name || this.data.instance.id}`;
  dialogActions: Array<IAction> = [
    new DialogCallbackAction({
      name: $localize`Unshelve instance`,
      primary: true,
      callback: () => this.unshelve(),
    }),
  ];
  dialogForm = this.instanceUnshelveForm;
  availabilityZones: Array<string>;
  showAZSelect = false;

  constructor(
    public dialogRef: MatDialogRef<InstanceUnshelveDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: { instance: IInstanceModel },
    private instancesApiService: InstancesApiService,
    private formBuilder: UntypedFormBuilder,
    private config: ConfigService,
    private notificationService: NotificationService,
  ) {
  }

  public unshelve() {
    this.loading = true;
    this.instancesApiService.unshelve(
      this.data.instance.id, this.instanceUnshelveForm.controls.availabilityZone.value
    ).subscribe({
      next: () => {
        this.notificationService.showMessage($localize`Unshelve instance sent`);
        this.dialogRef.close(true);
      }, error: (error) => {
        this.formErrors.setBackendErrors(error.error);
        this.dialogRef.close(false);
      }
    }).add(() => {
      this.loading = false;
    });
  }

  ngOnInit() {
    this.loading = true;
    this.instancesApiService.createOptions({
      region: this.data?.instance?.region,
    }).subscribe({
      next: (createOptions: IInstanceCreateOptions) => {
        this.availabilityZones = createOptions.availability_zones;
        this.showAZSelect = (
          this.config.authService.feature('openstack.instances.availability_zones') &&
          this.availabilityZones?.length && this.data.instance.status === 'shelved_offloaded'
        );
      }
    }).add(() => {
      this.loading = false;
    });
  }
}
