import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
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-set-license',
  templateUrl: './invalid-license.component.html',
  styleUrls: ['./invalid-license.component.scss']
})
export class InvalidLicenseComponent implements IBaseDialog, OnInit {
  public coreLimitExceeded: boolean;
  public ramLimitExceeded: boolean;
  public syncNeeded: boolean;
  dialogTitle = null;
  dialogActions: Array<IAction> = [];

  constructor(
    public dialogRef: MatDialogRef<InvalidLicenseComponent>,
    @Inject(MAT_DIALOG_DATA) public data: {
      staff: boolean;
      coreLimitInfo: {
        hypervisor_issue: boolean;
        exceeded: boolean;
        allowed_cores: number;
        detected_cores: number;
      };
      ramLimitInfo: {
        hypervisor_issue: boolean;
        exceeded: boolean;
        allowed_ram: number;
        detected_ram: number;
      };
    },
  ) {
    dialogRef.disableClose = true;
  }

  ngOnInit() {
    this.coreLimitExceeded = !!this.data.coreLimitInfo?.exceeded;
    this.ramLimitExceeded = !!this.data.ramLimitInfo?.exceeded;
    this.syncNeeded = !!this.data.coreLimitInfo?.hypervisor_issue || !!this.data.ramLimitInfo?.hypervisor_issue;
    if (this.data.staff) {
      if (this.syncNeeded) {
        this.dialogTitle = $localize`Sync needed`;
      } else {
        this.dialogTitle = $localize`Set license`;
      }
      // close button is visible only for staff
      this.dialogActions.push(
        new DialogCallbackAction({
          name: $localize`Close`,
          primary: true,
          callback: () => this.dialogRef.close(),
        }),
      );
    } else {
      this.dialogTitle = $localize`Panel suspended`;
    }
  }
}
