/**
 * Created by baoyinghai on 12/23/16.
 */
import React, { PropTypes } from 'react';
import { PFetch } from '../../../framework/system/fetch';
import { longRunExec } from '../../../framework/system/longRunOpt';
import routerChange from './routerChange';
import { Modal } from '../../mxa';
import { convertTemplate, trimStart } from '../../../framework/utils/StringUtils';

const createObject = (obj = {}) => {
  if (typeof obj === 'object') {
    return obj;
  }
  return { $MXKEY: obj };
};

const processAction = (data, props, next, stopLoading, mappings) => {
  if (props.beforeRequest) {
    data.params = props.beforeRequest(data.params);
  }
  longRunExec(() => PFetch(trimStart(convertTemplate(data.url, data.params, mappings), '/'), data.params)
    .then(response => {
      if (response.needConfirm) {
        Modal.confirm({
          title: '提示',
          content: (<div>{response.msgContent}</div>),
          onOk: () => {
            longRunExec(() => PFetch(trimStart(convertTemplate(data.url, data.params, mappings), '/'), { ...data.params, needConfirm: true })
              .then(res => {
                let r = res;
                if (props.afterRequest) {
                  r = props.afterRequest(res);
                }
                Modal.info({
                  title: '提示',
                  content: (<div>{props.requestSuccessDesc || (props.buttonDescription + '成功！')}</div>),
                  onOk: () => next && next({ ...data.params, ...createObject(r) })
                });
              }));
          },
          onCancel: () => stopLoading && stopLoading(false)
        });
      } else {
        let newResponse = response;
        // 主要有nextLink的情况
        if (props.afterRequest) {
          newResponse = props.afterRequest(response);
        }
        if (props.hiddenResultPrompt) {
          next && next({ ...data.params, ...createObject(newResponse) });
        } else {
          Modal.info({
            title: '提示',
            content: (<div>{props.requestSuccessDesc || (props.buttonDescription + '成功！')}</div>),
            onOk: () => next && next({ ...data.params, ...createObject(newResponse) })
          });
        }
      }
    })
    .catch(errorData => {
      // console.log(errorData);
      Modal.error({
        title: '提示',
        content: errorData && errorData.msgContent ? (<div>{errorData.msgContent}</div>) : (<div>{props.buttonDescription}失败！</div>),
        onOk: () => stopLoading && stopLoading(false),
      });
    }));
};

const routerChangeDecorator = (url, mappings, switchLoading) => {
  return {
    next: (data, props, next) => {
      routerChange.next(data, url, props, next, switchLoading, mappings);
    }
  };
};

export default {
  next: processAction,
  ctrl: (data, props, processCtrl, switchLoading) => {
    if (props.nextActionLink) {
      processCtrl.push(routerChangeDecorator(props.nextActionLink, props.nextActionLinkMappings, switchLoading));
    } else {
      processCtrl.push(routerChangeDecorator(props.actionLink || props.domainLink, props.actionLinkMappings, switchLoading));
    }
  }
};
