import { Injectable } from '@angular/core';
import { PopupBaseComponent } from '../components/popup-base/popup-base.component';
import { MatDialog, MatDialogConfig, MatDialogRef } from '../components/angular-material-index';

@Injectable({
  providedIn: 'root'
})
export class MatDialogService {

  constructor(private dialog: MatDialog) { }

  public openDialog(dialogComponent: any, dataObject: Object, isModal: boolean = false, onCloseDialog?: (_) => void, autofocus: boolean = true):
    MatDialogRef<unknown, any> {
    const dialogConfig = new MatDialogConfig();
    if (dialogComponent instanceof PopupBaseComponent) isModal = true; // we open derived from PopupBaseComponent popup dialogs
                                                                       // only as modal to shure executing of cancel function, thats reset BaseEntityWrapper
    dialogConfig.disableClose = isModal;
    dialogConfig.autoFocus = autofocus;
    dialogConfig.data = dataObject;
    const dialog = this.dialog.open(dialogComponent, dialogConfig);
    dialog.afterClosed().subscribe(result => {
      if (onCloseDialog) onCloseDialog(result);
    });
    return dialog;
  }

}
