import { Injectable } from '@angular/core';
import { NbDialogService } from '@nebular/theme';
import { AlertDlgComponent } from '../ui/alert-dlg/alert-dlg.component';
import { first, map, mergeMap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { MultiRecordPickerDlgComponent } from '../ui/fancy-table/multi-record-picker-dlg/multi-record-picker-dlg.component';
import { ObjectUtils } from '../utilities/object-utils';
import { MessageBoxConfig, ADIcon, ADButtons } from '../ui/alert-dlg/alert-dlg.model';

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

  constructor(
    public dlgService: NbDialogService
  ) { }


  /**
   * Shows msg box
   * @param title 
   * @param [text]
   * @param [config] Partial<MessageBoxConfig> 
   * @returns Ok,Yes:true, No,Close:false, Cancel:null
   */
  show(title: string,
    config: Partial<MessageBoxConfig> = {}): Observable<boolean> {
    let defaultConfig = { buttons: ADButtons.OK } as Partial<MessageBoxConfig>;
    Object.assign(defaultConfig, config);
    defaultConfig.title = title;

    return this.dlgService.open(AlertDlgComponent, {
      context: {
        config: new MessageBoxConfig(defaultConfig)
      },
      closeOnBackdropClick: false,
    }).onClose.pipe(first())
  }


  showMsg(config: Partial<MessageBoxConfig>): Observable<boolean> {
    return this.dlgService.open(AlertDlgComponent, {
      context: {
        config: new MessageBoxConfig(config)
      },
      closeOnBackdropClick: false,
    }).onClose.pipe(first())
  }

  showInputbox(title: string, text = '', config: Partial<MessageBoxConfig> = { inputType: 'string', icon: ADIcon.QUESTION }): Observable<any> {
    config.title = title;
    config.text = text;
    if (config.inputType === undefined) {
      config.inputType = 'string';
    }
    if (config.icon === undefined) {
      config.icon = ADIcon.QUESTION;
    }
    if (config.buttons === undefined) {
      config.buttons = ADButtons.OKCancel;
    }
    return this.dlgService.open(AlertDlgComponent, {
      context: {
        config: new MessageBoxConfig(config)
      },
      closeOnBackdropClick: false,
    }).onClose.pipe(first());
  }
  showConfirmDeleteBox(config: Partial<MessageBoxConfig> = {
    title: 'Delete This Record?',
    text: `Are you sure you want to delete it?`,
    icon: ADIcon.QUESTION,
    buttons: ADButtons.YesNo,
    confirmButtonText: 'Delete',
    cancelButtonText: "Cancel"
  }) {
    return this.show(config.title, config);
  }

}