import { Component, Input, OnInit } from '@angular/core';
import { IPluginComponent } from '@shared/plugins/interfaces/plugin-component';
import { MatDialog } from '@angular/material/dialog';
import { ChangeIPDialogComponent } from '../change-ip-dialog/change-ip-dialog.component';
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 { ServicesApiService } from '@fleio-api/billing/services/service-api.service';
import { ConfigService } from '@shared/config/config.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;
  changeIPTooltip = $localize`Change IP address`;
  changeOSTooltip = $localize`Change Operating System`;
  changeNameTooltip = $localize`Change name`;
  refreshLicenseTooltip = $localize`Refresh license`;
  refreshing = false;

  constructor(
    private matDialog: MatDialog,
    private refreshService: RefreshService,
    private notificationService: NotificationService,
    private servicesApi: ServicesApiService,
    public config: ConfigService,
  ) {
  }

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

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

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

  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();
      }
    });
  }

  refreshLicense() {
    if (this.refreshing) {
      return;
    }
    this.refreshing = true;
    this.servicesApi.pluginAction(
      this.data.service_id, 'refresh_license', {},
    ).subscribe({
      next: () => {
        this.notificationService.showMessage($localize`License refreshed.`);
        this.refreshService.refresh();
      }, error: () => {
        this.notificationService.showMessage($localize`Failed to save new IP, please check logs for details.`);
      }
    }).add(() => {
      this.refreshing = false;
    });
  }
}
