import {BsModalRef, BsModalService, ModalOptions} from "ngx-bootstrap/modal";
import {AjaxService} from "./ajax.service";
import {FrameUpload} from "../frame/upload/frame-upload/frame-upload";
import {FrameThreadRunningComponent} from "../../routes/frame/thread/frame-thread-running/frame-thread-running.component";

export class ModalService {

  /*************************
   * 显示线程窗口
   * 返回弹出窗口句柄
   */
  public static showUploadWindow(modalService: BsModalService, url: string, uploadParam?: any, modalParam?: any, options?: ModalOptions): BsModalRef {
    if (!modalParam)
      modalParam = {};
    let bsModalRef;
    const modal = Object.assign({}, {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-5p'
    }, options);
    modalParam['uploadParams'] = uploadParam || {};
    modal.initialState = modalParam;
    modal.initialState['uploadUrl'] = url;
    modal.initialState['modalService'] = modalService;
    // 如果需要去服务器请求数据
    if (modalParam['entityUrl']) {
      AjaxService.requestEntity(modalParam['entityUrl'], modalParam['entityParam'], (entity) => {
        modal.initialState['ResponseEntity'] = entity;
        bsModalRef = modalService.show(FrameUpload, modal);
      });
    } else if (modalParam['mapUrl']) {
      AjaxService.requestMap(modalParam['mapUrl'], modalParam['mapParam'], (map) => {
        modal.initialState['ResponseMap'] = map;
        bsModalRef = modalService.show(FrameUpload, modal);
      }, {RequestWithLoading: true});
    } else if (modalParam['arrayUrl']) {
      AjaxService.requestArray(modalParam['arrayUrl'], modalParam['arrayParam'], (array) => {
        modal.initialState['ResponseArray'] = array;
        bsModalRef = modalService.show(FrameUpload, modal);
      }, {RequestWithLoading: true});
    } else {
      bsModalRef = modalService.show(FrameUpload, modal);
    }
    return bsModalRef;
  }


  /*************************
   * 显示线程窗口
   * 返回弹出窗口句柄
   */
  public static showThreadWindow(modalService: BsModalService, frameThread: any, threadParam?: any, options?: ModalOptions): BsModalRef {
    const modal = Object.assign({}, {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState:  {},
      class: 'modal-5p'
    }, options);
    modal.initialState['ResponseEntity'] = frameThread;
    modal.initialState['threadParam'] = threadParam;
    return modalService.show(FrameThreadRunningComponent, modal);
  }

}
