import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IInstanceModel } from '@fleio-api/openstack/model/instance.model';
import { MatDialog } from '@angular/material/dialog';
import { map } from 'rxjs/operators';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { NotificationService } from '@shared/ui-api/notification.service';
import { IVolumeModel } from '@fleio-api/openstack/model/volume.model';
import { VolumesApiService } from '@fleio-api/openstack/volume/volumes-api.service';
import { Observable } from 'rxjs';
import { InstancesApiService } from '@fleio-api/openstack/instance/instances-api.service';
import { InstanceDetailsAttachVolumeDialogComponent } from '@shared/common-dialogs/openstack/instances/instance-details-attach-volume-dialog/instance-details-attach-volume-dialog.component';
import { ConfigService } from '@shared/config/config.service';

export interface IAttachedVolumesForInstanceResponse {
  objects: {
    volume: IVolumeModel;
  }[];
}

@Component({
  selector: 'app-instance-details-snapshots',
  templateUrl: './instance-details-volumes.component.html',
  styleUrls: ['./instance-details-volumes.component.scss']
})
export class InstanceDetailsVolumesComponent extends DetailsComponentBase<IInstanceModel> implements OnInit {
  attachedVolumesResponse: IAttachedVolumesForInstanceResponse | null;
  loading = null;
  displayedColumns: string[] = ['display_name', 'status', 'size', 'has_snapshots', 'actions'];
  permissions: Record<string, unknown>;
  detachVolumeText = $localize`Detach volume`;

  constructor(
    private notificationService: NotificationService,
    private volumesApiService: VolumesApiService,
    private instancesApiService: InstancesApiService,
    private matDialog: MatDialog,
    public config: ConfigService,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, config);
    this.matDialog = matDialog;
  }

  private loadVolumes() {
    if (this.loading === null) {
      // refresh only the first time
      this.loading = true;
    }
    this.volumesApiService.getAttachments(this.object.id).pipe().subscribe(
      (response) => {
        this.attachedVolumesResponse = response;
        this.loading = false;
      });
  }

  public detachVolume(volumeId) {
    const dialogResult$ = this.notificationService.confirmDialog({
      title: $localize`Detach volume ${volumeId}?`,
      message: $localize`Are you sure?`,
    });
    dialogResult$.subscribe(dialogResult => {
      if (dialogResult?.button === 'yes') {
        this.instancesApiService.detachVolume(this.object.id, volumeId).pipe(map(success => {
          if (success) {
            this.notificationService.showMessage($localize`Volume detach scheduled.`);
            this.boostRefreshTimer();
          } else {
            this.notificationService.showMessage($localize`Could not detach volume.`);
          }
        })).subscribe();
      }
    });
  }

  public openAttachVolumeDialog() {
    return this.matDialog.open(
      InstanceDetailsAttachVolumeDialogComponent, {
        data: {
          instanceId: this.object.id,
          instanceName: this.object.name,
          project: this.object.project,
          region: this.object.region
        },
        autoFocus: false,
      }).afterClosed().pipe(map(result => {
      if (result === false || typeof result === 'undefined') {
        return;
      }
      this.notificationService.showMessage($localize`Attaching volume...`);
      this.boostRefreshTimer();
      return {message: result} as IActionResult;
    })).subscribe();
  }

  protected refreshData() {
    this.loadVolumes();
  }

  allowedAction(action: string) {
    if (this.object) {
      return this.object.allowed_actions.indexOf(action) > -1;
    }
    return false;
  }

  ngOnInit() {
    super.ngOnInit(this.config.getDefaultRefreshInterval(true));
    if (this.objectController) {
      this.permissions = this.objectController.additionalObjects.permissions;
    }
    this.attachedVolumesResponse = null;
  }

}
