/**
 * 收集数据
 */
import React from 'react';
import { Modal } from '../../mxa';
import {
  LIST_SELECTTYPE,
  BUTTON_INTERACTIVETYPE,
  BUTTON_MESSAGEPROMPTTYPE,
  BUTTON_RELATEDDATA,
  BUTTON_BINDPARAMETERTYPE,
  BUTTON_ACTIONTYPE
} from '../../../framework/constant/dictCodes';

import routerChange from './routerChange';
import triggerRequest from './triggerRequest';
import { filterEmptyValue } from '../../../framework/utils/EmptyUtils';

const confirm = Modal.confirm;

const genCustomParams = props => {
  const { record, buttonDescription, inject } = props;
  let customParams = {};
  if (inject.buttons && inject.buttons.length > 0) {
    const injectAction = inject.buttons.filter(button => button.key === buttonDescription);
    if (injectAction && injectAction.length > 0 && injectAction[0].action) {
      customParams = injectAction[0].action({ props, record });
    }
  }
  return customParams;
};

const createBindParameters = (record, props) => {
  let params = {};
  if (props.bindParameterType === BUTTON_BINDPARAMETERTYPE.SEVERAL) {
    props.bindParameters && props.bindParameters.forEach(item => {
      params[item.name] = filterEmptyValue(item.value, record[item.name]);
    });
  } else if (props.bindParameterType === BUTTON_BINDPARAMETERTYPE.ALL) {
    params = Object.assign(params, record);
    // add bind Parameters
    if (props.bindParameters && props.bindParameters.filter) {
      params = props.bindParameters.filter(item => item.value !== null && item.value !== undefined)
        .reduce((res, p) => {
          res[p.name] = p.value;
          return res;
        }, params);
    }
  } else if (props.bindParameterType === BUTTON_BINDPARAMETERTYPE.CUSTOMIZE) {
    params = Object.assign(params, genCustomParams(props));
  } else if (props.bindParameterType === BUTTON_BINDPARAMETERTYPE.KEYS) {
    params = record[props.mainEntityKey];
  } else if (props.bindParameterType === BUTTON_BINDPARAMETERTYPE.KEY) {
    params = {
      [props.mainEntityKey]: record[props.mainEntityKey]
    };
  } else if (props.bindParameterType === BUTTON_BINDPARAMETERTYPE.NONE) {
    params = {};
  }
  return params;
};

const createTransmitParameters = props => {
  const params = {};
  if (props.transmitParameters && props.transmitParameters.length > 0 && props.query) {
    props.transmitParameters.forEach(item => {
      params[item.name] = props.query[item.name];
    });
  }
  if (props.isTransmitAllParameters && props.query) {
    return Object.assign(params, props.query);
  }
  return params;
};

const genParams = (record, props) => Object.assign({}, createTransmitParameters(props), createBindParameters(record, props));

const triggerActionWithoutRows = (data, props, next) => {
  const params = genParams({}, props);
  if (props.messagePromptType === BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: (<div>确认{props.buttonDescription}吗？</div>),
      onOk: () => next(params),
      onCancel() {},
    });
  } else {
    next(params);
  }
};

const triggerActionWithoutForm = (data, props, next) => {
  const params = genParams({}, props);
  const cb = () => {
    if (props.submitFuc) {
      props.submitFuc(values => {
        next({ ...params, ...values });
      });
    } else {
      next(params);
    }
  };
  if (props.messagePromptType === BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: (<div>确认{props.buttonDescription}吗？</div>),
      onOk: () => cb(),
      onCancel() {},
    });
  } else {
    cb();
  }
};

const triggerActionSingle = (activeData, props, next) => {
  const params = genParams(activeData, props);
  if (props.messagePromptType === BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: (<div>确认{props.buttonDescription}吗？</div>),
      onOk: () => next(params),
      onCancel() {},
    });
  } else {
    next(params);
  }
};

const triggerActionMultiple = (activeData, props, next) => {
  const paramPartA = activeData.map(record => createBindParameters(record, props));
  const paramPartB = createTransmitParameters(props);
  let params = {};
  if (props.transmitParameters && props.transmitParameters.length > 0) {
    params = { ...paramPartB, keyArray: paramPartA };
  } else {
    params = { keyArray: paramPartA };
  }
  if (props.messagePromptType === BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: (<div>确认{props.buttonDescription}所选数据吗？</div>),
      onOk: () => next(params),
      onCancel() {},
    });
  } else {
    next(params);
  }
};

const triggerActionAll = (activeData, props, next) => {
  const paramPartB = createTransmitParameters(props);
  let params = {};
  if (props.transmitParameters && props.transmitParameters.length > 0) {
    params = { ...paramPartB, dataArray: activeData };
  } else {
    params = { dataArray: activeData.map(item => {
      item.key && delete item.key;
      return item;
    }) };
  }
  if (props.messagePromptType === BUTTON_MESSAGEPROMPTTYPE.CONFIRM) {
    confirm({
      title: '提示',
      content: (<div>确认{props.buttonDescription}的数据吗？</div>),
      onOk: () => next(params),
      onCancel() {},
    });
  } else {
    next(params);
  }
};

const collectData = (p, props, next) => {
  const { record } = props;
  if (props.relatedData === BUTTON_RELATEDDATA.NONE) {
    triggerActionWithoutRows({}, props, next);
  } else if (props.relatedData === BUTTON_RELATEDDATA.FORM) {
    triggerActionWithoutForm({}, props, next);
  } else if (props.inline) {
    triggerActionSingle(record, props, next);
  } else if (props.relatedData === 'ALL') {
    triggerActionAll(record, props, next);
    console.log('aaaaa');
  } else if (props.selectedType !== LIST_SELECTTYPE.CHECKBOX
    || props.relatedData === BUTTON_RELATEDDATA.SINGLE) {
    triggerActionSingle(record[0], props, next);
  } else {
    triggerActionMultiple(record, props, next);
  }
};

const createTriggerRequest = switchLoading => {
  return {
    next: (d, p, next) => {
      triggerRequest.next({ params: d, url: p.actionLink || p.domainLink }, p, next, switchLoading);
    },
    ctrl: triggerRequest.ctrl
  };
};

const createRouterChange = switchLoading => {
  return {
    next: (d, p, next) => {
      routerChange.next(d, p.actionLink || p.domainLink, p, next, switchLoading);
    }
  };
};

export default {
  next: collectData,
  ctrl: (data, props, processCtrl, switchLoading) => {
    if (props.actionType === BUTTON_ACTIONTYPE.REQUEST) {
      switchLoading && switchLoading(true);
      processCtrl.push(createTriggerRequest(switchLoading));
    } else if (props.actionType === BUTTON_ACTIONTYPE.ROUTER) {
      processCtrl.push(createRouterChange(switchLoading));
    } else {
      // 默认 ROUTER
      processCtrl.push(createRouterChange(switchLoading));
    }
  }
};
