import { Injectable } from '@angular/core';
import { MessageService } from 'primeng/api';
import { TranslateService } from '@ngx-translate/core';
import { ToastOptions } from 'src/app/model/utils/toast-options';

@Injectable({
  providedIn: 'root',
})
export class ToastService {
  constructor(
    private translate: TranslateService,
    private messageService: MessageService
  ) {}

  private toast(
    type: string,
    summary: string,
    detail?: string,
    additionalDetail?: string,
    options?: ToastOptions
  ) {
    this.messageService.add({
      severity: type,
      summary: summary ? this.translate.instant(summary) : undefined,
      detail: `${detail ? this.translate.instant(detail) : ''} ${additionalDetail ?? ''}`,
      life: options?.life,
      sticky: options?.sticky,
      closable: options?.closable,
      contentStyleClass: 'whitespace-pre-wrap',
    });
  }

  success(summary: string, detail?: string, options?: ToastOptions) {
    this.toast('success', summary, detail, undefined, options);
  }

  info(summary: string, detail?: string, options?: ToastOptions) {
    this.toast('info', summary, detail, undefined, options);
  }

  warn(summary: string, detail?: string, options?: ToastOptions) {
    this.toast('warn', summary, detail, undefined, options);
  }

  error(
    summary: string,
    detail?: string,
    error?: string,
    options?: ToastOptions
  ) {
    this.toast('error', summary, detail, error, options);
  }
}
