import { Component, Inject, OnInit } from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
import { TranslateService } from "@ngx-translate/core";
import { DialogService } from "../../services/dialog.service";
import { NanoDialogBase } from "../nano-base-dialog";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";
import { ClipboardService } from "../../services/clipboard.service";

@Component({
  selector: "app-general-alert",
  templateUrl: "./general-alert-dialog.component.html",
  styleUrls: ["./general-alert-dialog.component.scss"],
})
export class GeneralAlertDialogComponent
  extends NanoDialogBase<GeneralAlertDialogComponent>
  implements OnInit
{
  alertData: GeneralAlertData;
  showError: boolean = false;

  constructor(
    public dialogRef: MatDialogRef<GeneralAlertDialogComponent>,
    @Inject(MAT_DIALOG_DATA)
    public generalAlertData: GeneralAlertData,
    public translate: TranslateService,
    public dialogService: DialogService,
    private clipboardService: ClipboardService
  ) {
    super(dialogRef);
    this.alertData = generalAlertData;
  }

  ngOnInit(): void {}

  copyError(): void {
    this.clipboardService
      .copy(this.alertData.errorMessage)
      .then(() => {
        this.dialogService.showSnackbar(marker("Error message copied!"));
      })
      .catch((err) => {
        console.error("clipboard err", err);
        this.dialogService.openAlertDialog(
          marker("Copy Error"),
          marker("Could not copy to the clipboard")
        );
      });
  }
}

interface GeneralAlertData {
  title: string;
  text: string;
  errorMessage: string;
}
