import { Component, Input, OnInit } from '@angular/core';
import { IPluginComponent } from '@shared/plugins/interfaces/plugin-component';
import { MatDialog } from '@angular/material/dialog';
import { IServiceResources } from '@fleio-api/billing/model/service.model';
import {
  IServiceResources as IDirectAdminServiceResources
} from '@shared/plugins/interfaces/directadmin/service-resources';
import { RefreshService } from '@shared/ui-api/refresh.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { ChangeOSDialogComponent } from '@shared/plugins/common-dialogs/directadmin/change-os-dialog/change-os-dialog.component';
import { ChangeNameDialogComponent } from '@shared/plugins/common-dialogs/directadmin/change-name-dialog/change-name-dialog.component';

@Component({
  selector: 'app-service-details',
  templateUrl: './service-details.component.html',
  styleUrls: ['./service-details.component.scss']
})
export class ServiceDetailsComponent implements IPluginComponent, OnInit {
  @Input()
  public data: IServiceResources;
  public serviceResources: IDirectAdminServiceResources;
  changeNameTooltip = $localize`Change name`;
  changeOSTooltip = $localize`Change Operating System`;

  constructor(
    private matDialog: MatDialog,
    private refreshService: RefreshService,
    private notificationService: NotificationService,
  ) {
  }

  ngOnInit() {
    this.serviceResources = this.data.service_resources;
  }

  dataChanged() {
    this.serviceResources = this.data.service_resources;
  }

  changeOS() {
    this.matDialog.open(ChangeOSDialogComponent, {data: this.data, autoFocus: false,}).afterClosed().subscribe(result => {
      if (result === 'saved') {
        this.notificationService.showMessage($localize`New Operating System saved`);
        this.refreshService.refresh();
      }
    });
  }

  changeName() {
    this.matDialog.open(ChangeNameDialogComponent, {data: this.data, autoFocus: false,}).afterClosed().subscribe(result => {
      if (result === 'saved') {
        this.notificationService.showMessage($localize`New name saved`);
        this.refreshService.refresh();
      }
    });
  }
}
