import { Injectable } from "@angular/core";
import { MatDialog } from "@angular/material/dialog";
import { MatSnackBar } from "@angular/material/snack-bar";
import { GeneralAlertDialogComponent } from "../dialogs/general-alert-dialog/general-alert-dialog.component";
import { GeneralConfirmDialogComponent } from "../dialogs/general-confirm-dialog/general-confirm-dialog.component";
import { TranslateService } from "@ngx-translate/core";
import { forkJoin, Observable } from "rxjs";
import { AccountService } from "../server-services/account.service";
import {
  GeneralInputDialogComponent,
  GeneralInputDialogData,
} from "../dialogs/general-input-dialog/general-input-dialog.component";
import { ContactDialogComponent } from "../dialogs/contact-dialog/contact-dialog.component";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";

@Injectable({
  providedIn: "root",
})
export class DialogService {
  constructor(
    private dialog: MatDialog,
    private snackBar: MatSnackBar,
    private translate: TranslateService
  ) {
  }

  public openAlertDialog(title: string, text: string, errorMessage: string = null): Observable<void> {
    var ref = this.dialog.open(GeneralAlertDialogComponent, {
      data: { title, text, errorMessage },
    });

    return ref.afterClosed();
  }

  public openConfirmDialog(title: string, text: string, okButtonText?: string, cancelButtonText?: string): Observable<boolean> {
    var ref = this.dialog.open(GeneralConfirmDialogComponent, {
      data: { title, text, okButtonText, cancelButtonText },
    });

    return ref.afterClosed();
  }

  /**
   * Dialog for a simple string input field
   * @param {string} title Title of the dialog
   * @param {string} text Main text under the title
   * @param {string} input Default value for the input
   * @param {boolean} canBeEmpty if false, the dialog won't accept empty string
   * @returns
   */
  public openInputDialog(
    title: string,
    text: string,
    input: string = "",
    canBeEmpty: boolean = false
  ): Observable<string> {
    var ref = this.dialog.open(GeneralInputDialogComponent, {
      data: <GeneralInputDialogData>{ title, text, input, canBeEmpty },
    });

    return ref.afterClosed();
  }

  public showSnackbar(message: string, action: string = null) {
    if (action) {
      forkJoin([
        this.translate.get(message),
        this.translate.get(action),
      ]).subscribe(([translatedMessage, translatedAction]) => {
        this.snackBar.open(translatedMessage, translatedAction, {
          horizontalPosition: "center",
          verticalPosition: "bottom",
          panelClass: ["mat-toolbar", "mat-primary", "nano-snackbar"],
        });
      });
    } else {
      this.translate.get(message).subscribe((message) => {
        this.snackBar.open(message, undefined, {
          horizontalPosition: "center",
          verticalPosition: "bottom",
          duration: 3 * 1000,
          panelClass: ["mat-toolbar", "mat-primary", "nano-snackbar"],
        });
      });
    }
  }
}
