/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source 源数据
 * @returns {Object} targetObj 复制后的数据
 */
export function deepClone (source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

/**
 * 判断val是否是空对象
 * @param {*} val js变量
 * @returns {Boolean} 返回boolean值
 */
export const isEmpty = function (val) {
  // null or undefined
  if (val === null) return true

  if (typeof val === 'boolean') return false

  if (typeof val === 'number') return !val

  if (val instanceof Error) return val.message === ''

  switch (Object.prototype.toString.call(val)) {
    // String or Array
    case '[object String]':
    case '[object Array]':
      return !val.length

    // Map or Set or File
    case '[object File]':
    case '[object Map]':
    case '[object Set]': {
      return !val.size
    }
    // Plain Object
    case '[object Object]': {
      return !Object.keys(val).length
    }
  }

  return false
}

/**
 * 验证手机号
 * @param {*} phone 手机号
 * @returns {Boolean} boolean值
 */
export function validatePhone (phone) {
  const reg = /^[1]([3-9])[0-9]{9}$/
  return reg.test(phone)
}

/**
 * 获取URL中的参数
 * @param {String} url
 * @returns {String} 要获取的参数name
 */
export function getQueryString (url, name) {
  const reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
  const r = url.substr(1).match(reg)
  if (r !== null) {
    return r[2]
  }
  return null
}

/**
 * 根据对象的某个key排序
 * @param {*} 对象
 */
export function sortObject (property) {
  return function (obj1, obj2) {
    const value1 = obj1[property]
    const value2 = obj2[property]
    return value1 - value2
  }
}
/**
 * 根据对象的某个key倒序排序
 * @param {*} 对象
 */
export function sortDescObject (property) {
  return function (obj1, obj2) {
    const value1 = obj1[property]
    const value2 = obj2[property]
    return value2 - value1
  }
}

/**
 * 判断商标是否是无效的
 * @param {String} 商标状态
 * @returns {Boolean} boolean值
 */
export function tmIsIneffective (status) {
  return status === '商标无效'
}

/**
 *将json对象转化为url要拼接的字符串
 */
export function encodeParam (link, params) {
  let url = ''
  if (params && Object.keys(params).length > 0) {
    for (const k in params) {
      const value = params[k] !== undefined ? params[k] : ''
      url += '&' + k + '=' + encodeURIComponent(value)
    }
    url = url ? url.substring(1) : ''
    if (params) {
      link += (link.indexOf('?') < 0 && params ? '?' : '&') + url
    }
  }
  return link
}
/**
 * 计算时间
 * @param {*} time
 * @param {*} cFormat
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return timeStr
}
// 获取任意时间
export function getDate(date, AddDayCount = 0) {
  if (!date) {
    date = new Date()
  }
  if (typeof date !== 'object') {
    date = date.replace(/-/g, '/')
  }
  const dd = new Date(date)

  dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

  const y = dd.getFullYear()
  const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
  const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
  return {
    fullDate: y + '-' + m + '-' + d,
    year: y,
    month: m,
    date: d,
    day: dd.getDay()
  }
}
