/**
 * Created by baoyinghai on 10/18/16.
 */
import React from 'react';
import { showLoading, hideLoading } from '../modules/loading-bar';

import { dispatch } from '../service/DispatchService';
// import { forceLogout } from '../actions/global';
import { Modal } from '../../components/mxa';

export const ContentRender = props => {
  return (
    <div>
      <div style={{ wordWrap: 'break-word' }}>{props.message}</div>
      <div style={{ wordWrap: 'break-word' }}>{props.uuid}</div>
    </div>
  );
};

/* eslint-disable */
export const longRunExec = (bizHandler, needLoding = true) => {

  const self = this;
  if (needLoding) {
    dispatch(showLoading());
  }
  return bizHandler()
    .then((response) => {

    })
    .catch((errorData) => {
      let content = errorData.message ? errorData.message : errorData.msgContent + '(' + errorData.ex + ')';
      // TODO upload error;
      if ('Network request failed' === errorData.message) {
        content = '网络请求异常!';
      }
      console.log('** error ** ', errorData);
      console.log('uudi', errorData && errorData.uuid);
      Modal.error({
        title: '异常',
        content: (<ContentRender message={content} uuid={errorData.uuid} />),
        onOk: () => {
          if (errorData.msgCode === 'TOKEN_INVALIDATE' || errorData.ex === 2114 || errorData.ex === 2115) {
            location.reload();
          }
        }
      });
      // if (errorData.msgCode === 'SYSTEM_AUTH_HEADER_INVALID' || errorData.msgCode === 'SYSTEM_AUTH_HEADER_NOTMATCHED') {
      //   dispatch(forceLogout());
      // }
    }).catch((e) => {
      // TODO upload error; merge
    console.log(e);
    Modal.error({
      title: '系统异常',
      content: JSON.stringify(e)
    });
  }).then(() => {
    console.log('need loading: ', needLoding);
    if (needLoding) {
      dispatch(hideLoading());
    }
  });
};

