import { Component, Inject, OnInit } from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
import { NanoDialogBase } 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";

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

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

  ngOnInit(): void {}

  copy(token: string): void {
    this.clipboardService
      .copy(token)
      .then(() => {
        this.dialogService.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")
        );
      });
  }
}
