import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { NanoDialogBaseComponent } from 'src/app/shared/dialogs/nano-base-dialog';
import { DialogService } from 'src/app/shared/services/dialog.service';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
import { LicenseBundleRecord } from 'src/app/shared/server-services/query-records/nano-records';
import { ClipboardService } from 'src/app/shared/services/clipboard.service';
import { SnackBarService } from 'src/app/shared/services/snackbar.service';

@Component({
  selector: 'app-bundled-licenses-dialog',
  templateUrl: './bundled-licenses-dialog.component.html',
  styleUrls: ['./bundled-licenses-dialog.component.scss'],
})
export class BundledLicensesDialogComponent extends NanoDialogBaseComponent<BundledLicensesDialogComponent> {
  public tokens: string[];

  constructor(
    public dialogRef: MatDialogRef<BundledLicensesDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public licenseBundle: LicenseBundleRecord,
    private dialogService: DialogService,
    private clipboardService: ClipboardService,
    private snackbarService: SnackBarService
  ) {
    super(dialogRef);
    this.tokens = licenseBundle.tokens;
  }

  copy(token: string): void {
    this.clipboardService
      .copy(token)
      .then(() => {
        this.snackbarService.showSnackbar(marker('License key copied!'));
      })
      .catch((err) => {
        console.error('clipboard err', err);
        this.dialogService.openAlertDialog(
          marker('Copy Error'),
          marker('Could not copy to the clipboard')
        );
      });
  }
}
