import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { TranslateService } from '@ngx-translate/core';
import { NanoDialogBaseComponent } from '../nano-base-dialog';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
import { ClipboardService } from '../../services/clipboard.service';
import { SnackBarService } from '../../services/snackbar.service';

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

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

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

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