/**
 * Created by baoyinghai on 5/26/17.
 */
import React from 'react';
import { showModal } from '../../../framework/pageContainer/ModalWrapper';
import { PFetch } from '../../../framework/system/fetch';
import { longRunExec } from '../../../framework/system/longRunOpt';
import { clickSpaceWrapper } from '../../clickSpaceCallback';
import { Modal } from '../../mxa';

export const popModal = (modalTitle, body) => {
  showModal({modalTitle, body});
};

export const fetch = (url, params, success, fail) => {
  longRunExec(() => PFetch(url, params).then(success).catch(fail));
};

export const closeWhileClickSpace = clickSpaceWrapper;

const getDescOfResponse = msg => {
  if (typeof msg === 'string') {
    return msg;
  }
  if (typeof msg === 'object') {
    return (msg && msg.msgContent) || JSON.stringify(msg);
  }
  return msg;
};


export const popAlert = (msg, ok, cancel) => {
  const config = {
    title: '提示',
    content: (<div>{getDescOfResponse(msg)}</div>),
  };
  ok ? config.onOk = ok : null;
  cancel ? config.onCancel = cancel : null;
  Modal.info(config);
};

export const popConfirm = (msg, ok, cancel) => {
  const config = {
    title: '提示',
    content: (<div>{getDescOfResponse(msg)}</div>),
  };
  ok ? config.onOk = ok : null;
  cancel ? config.onCancel = cancel : null;
  Modal.confirm(config);
};