/* eslint-disable no-undef */
/* eslint-disable arrow-parens */
const strategy = {
  NEW: {
    text: '待付款',
    color: 'red',
  },
  WAIT_PAY: {
    text: '待付款',
    color: 'red',
  },
  CLOSE: {
    text: '交易关闭',
    color: 'black',
  },
  REFUND: {
    text: ' 已退款',
    color: 'black',
  },
  UNDELIVERED: {
    text: '待提货',
    color: 'black',
  },
  DELIVERED: {
    text: '已提货',
    color: 'black',
  },
  UNSEND: {
    text: '待发货',
    color: 'black',
  },
};
// 兜底中间态
const getTempStatus = type => strategy[type] || {};

// 返回状态颜色
const getColor = (viewStatus = '') =>
  getTempStatus(viewStatus).color || viewStatus;

// 返回状态文字
const getText = (viewStatus = '', deliverType = '') => {
  if (deliverType.indexOf('DELIVERY') > -1) {
    const tmpObj = {
      ...strategy,
      UNDELIVERED: {
        text: '待收货',
        color: 'black',
      },
      DELIVERED: {
        text: '已收货',
        color: 'black',
      },
    };
    return (tmpObj[viewStatus] && tmpObj[viewStatus].text) || '收货';
  } else {
    return getTempStatus(viewStatus).text || viewStatus;
  }
};

// 返回总商品数量
const getTotalNum = (list = []) =>
  list.reduce((pre, item) => pre + +item.num, 0);

// 计算支付倒计时间
const getDeadLineTime = deadTime => {
  const nowTime = getDate();
  const deadLine = getDate(iosNewDate(deadTime));
  return Math.floor(Math.abs(deadLine - nowTime) / 1000);
};

const iosNewDate = date => {
  date = date.replace(getRegExp('-', 'g'), '/');
  return getDate(date);
};

// 获取 是否已经支付 return false true
const hadPayed = status => {
  if (status === 'NEW' || status === 'WAIT_PAY') {
    return false;
  }
  return true;
};

const judgePositionY = (detailData = {}) => {
  const { status, deliverType, deliverStatus, refundStatus } = detailData;
  if (status === 'WAIT_PAY' || status === 'NEW') {
    return '-20';
  }
  if (status === 'CLOSE') {
    if (refundStatus === 'FULL') {
      return '0';
    }
    return '-50';
  }
  if (deliverType.indexOf('MANUAL') > -1 && deliverStatus === 'UNSEND') {
    return '-70';
  }
  if (deliverType.indexOf('DELIVERY') > -1) {
    return '-50';
  }
  return '0';
};

export default {
  getColor,
  judgePositionY,
  getText,
  getDeadLineTime,
  getTotalNum,
  hadPayed,
};
