import $n from '../util/nothing'
import $store from '../store'
import Enums from '../util/Enums'

/**
 * vuex Store 的 action 调用
 * @param {*} action 调用的action名
 * @param {*} params 参数
 */
export function _dispatch (action, params) {
  return $store.dispatch(action, params)
}
/**
 * vuex Store 的 getters 引用
 * @param {*} getters Store中引用的getters属性名
 * @param {*} defaultValue 默认值（结果为空时）
 */
export  function  _getters (getters, defaultValue = null) {
  return $n._ifNull($store.getters[getters], defaultValue)
}
/**
 * 基于Promise调用API
 * @param {*}
 */
export function _callAPI ({callFun = Function, callParams = [], callBack, resultType = Enums.DataType.JSON} = {}) {
  return new Promise((resolve, reject) => {
    callFun(...callParams).then(({ code, count, data, per_page, msg}) => {
      if (code === 0) {
        data = $n._caseValue(
          resultType,
          Enums.DataType.JSON, $n._ifNull(data, {}),
          Enums.DataType.ARRAY, $n._ifNull(data, []),
          Enums.DataType.DATE, $n._isNotNull(data) ? new Date(data) : null,
          Enums.DataType.STRING, $n._ifNull(data, null),
          Enums.DataType.NUMBER, Number($n._ifNull(data, 0)),
          data
        )
        if (callBack) callBack(data)
        resolve(data)
      } else throw {code:code,data:data,msg:msg} || '执行 [' + callFun.name + '] 发生未知错误.'
    }).catch(err => reject(err))
  })
}

/**
 * 基于Promise调用API
 * @param {*}
 */
// export function _callAPIone ({callFun = Function, callParams = [], callBack, resultType = Enums.DataType.JSON} = {}) {
//   return new Promise((resolve, reject) => {
//     callFun(...callParams).then(({ code, count, data, per_page, msg,total}) => {
//       if (code === 0) {
//         data = $n._caseValue(
//           resultType,
//           Enums.DataType.JSON, $n._ifNull(data, {}),
//           Enums.DataType.ARRAY, $n._ifNull(data, []),
//           Enums.DataType.DATE, $n._isNotNull(data) ? new Date(data) : null,
//           Enums.DataType.STRING, $n._ifNull(data, null),
//           Enums.DataType.NUMBER, Number($n._ifNull(data, 0)),
// 		  code,
//           data,
// 		  count,
// 		  per_page,
// 		  msg,
// 		  total
//         )
//         if (callBack) callBack({code:code,count:count,data:data,per_page:per_page,msg:msg,total:total})
//         resolve({code:code,count:count,data:data,per_page:per_page,msg:msg,total:total})
//       } else throw {code:code,data:data,msg:msg} || '执行 [' + callFun.name + '] 发生未知错误.'
//     }).catch(err => reject(err))
//   })
// }

/**
 * 查询
 * @param {*}
 */
export function _query ({api, params, callBack, resultType = Enums.DataType.JSON} = {}) {
  return _callAPI({callFun: api, callParams: [params], callBack, resultType})
}



/**
 * 修改
 * @param {*}
 */
export function _update ({api, params, callBack, resultType = Enums.DataType.JSON} = {}) {
  return _callAPI({callFun: api, callParams: [params], callBack, resultType})
}
/**
 * 保存
 * @param {*}
 */
export function _save ({api, params, callBack, resultType = Enums.DataType.JSON} = {}) {
  return _callAPI({callFun: api, callParams: [params], callBack, resultType})
}
/**
 * 删除
 * @param {*}
 */
export function _delete ({api, params, callBack, resultType = Enums.DataType.JSON} = {}) {
  return _callAPI({callFun: api, callParams: [params], callBack, resultType})
}
/**
 * 调用动作
 * @param {*}
 */
export function _callAction ({api, params, callBack, resultType = Enums.DataType.JSON, action = null} = {}) {
  console.warn('############# Action: ', action)
  return _callAPI({callFun: api, callParams: [params], callBack, resultType})
}
/**
 * 列表查询
 * @param {*}
 */
export function _queryList ({api, params, callBack, resultType = Enums.DataType.ARRAY} = {}) {
  return _callAPI({callFun: api, callParams: [params], callBack, resultType})
}
/**
 * 分页查询
 * @param {*}
 */
export function _queryPage ({api, params, page, callBack, resultType = Enums.DataType.JSON} = {}) {
  return _callAPI({callFun: api, callParams: [params, page], callBack, resultType})
}
/**
 * 流式分页查询
 * @param {*}
 */
export function _queryFlowPage ({api, params, page, callBack, resultType = Enums.DataType.JSON} = {}) {
  return _callAPI({callFun: api, callParams: [params, page], callBack, resultType})
}
