import { Component, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IInstanceModel } from '@fleio-api/openstack/model/instance.model';
import { ConfigService } from '@shared/config/config.service';
import { map } from 'rxjs/operators';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { MatDialog } from '@angular/material/dialog';
import { EditPtrRecordDialogComponent } from '@openstack-instances-dialogs/edit-ptr-record-dialog/edit-ptr-record-dialog.component';
import { NotificationService } from '@shared/ui-api/notification.service';
import { OpenstackUtils } from '@fleio-api/openstack/openstack-utils';

@Component({
  selector: 'app-enduser-instance-details-info',
  templateUrl: './enduser-instance-details-info.component.html',
  styleUrls: ['./enduser-instance-details-info.component.scss']
})
export class EnduserInstanceDetailsInfoComponent extends DetailsComponentBase<IInstanceModel> implements OnInit {
  ramDisplay;
  constructor(
    public config: ConfigService,
    private matDialog: MatDialog,
    private notificationService: NotificationService,
  ) {
    super();
  }

  ngOnInit(refreshTimerInterval?: number) {
    super.ngOnInit(refreshTimerInterval);
    this.ramDisplay = OpenstackUtils.MiBToUserFriendly(this.object.flavor.memory_mb, true);
  }

  getPTR(ipAddress) {
    return this.matDialog.open(
      EditPtrRecordDialogComponent, {
        data: {
          region: this.object.region,
          ip: ipAddress
        },
        autoFocus: false,
    }).afterClosed().pipe(map(result => {
      if (result === true || result === 'true') {
        this.boostRefreshTimer();
        this.notificationService.showMessage($localize`PTR updated.`);
      }
      return {message: result} as IActionResult;
    })).subscribe();
  }
}
