import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AppLocalStorageService } from '@shared/ui-api/app-local-storage.service';
import { ConfigService } from '@shared/config/config.service';
import { Router } from '@angular/router';
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-license-warning-dialog',
  templateUrl: './license-warning-dialog.component.html',
  styleUrls: ['./license-warning-dialog.component.scss']
})
export class LicenseWarningDialogComponent implements IBaseDialog, OnInit {
  permanentlyHideWasSet: boolean;
  dialogTitle = null;
  dialogActions: Array<IAction> = [];

  constructor(
    public dialogRef: MatDialogRef<LicenseWarningDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: {
      licenseDaysLeft: number;
      showCoresWarning: boolean;
      coreDaysLeft: number;
      maxCores: number;
      coresInUse: number;
      showRamWarning: boolean;
      ramDaysLeft: number;
      maxRam: number;
      ramInUse: number;
      clientsExpiring: boolean;
      maxClients: number;
      clientsInUse: number;
    },
    private appLocalStorageService: AppLocalStorageService,
    private config: ConfigService, private router: Router,
    ) {
  }

  permanentlyHide() {
    this.appLocalStorageService.setLicenseWarningHide(true);
    this.dialogRef.close(true);
  };

  goToLicense() {
    this.router.navigateByUrl(this.config.getPanelUrl('settings/general#license')).then(() => {
      this.dialogRef.close(true);
    });
  }

  ngOnInit() {
    if (this.data?.licenseDaysLeft >= 0) {
      this.dialogTitle = $localize`License about to expire`;
    } else if (this.data?.licenseDaysLeft < 0) {
      this.dialogTitle = $localize`License expired`;
    }
    // used to hide warning button if dialog was manually opened and auto show is disabled
    this.permanentlyHideWasSet = this.appLocalStorageService.getLicenseWarningHide();
    if (this.permanentlyHideWasSet === false) {
      this.dialogActions.push(
        new DialogCallbackAction({
          name: $localize`Permanently hide`,
          primary: false,
          callback: () => this.permanentlyHide(),
        }),
      );
    }
    this.dialogActions.push(
      new DialogCallbackAction({
        name: $localize`Go to license page`,
        primary: true,
        callback: () => this.goToLicense(),
      }),
    );
  }
}
