/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return
  if (typeof content !== 'string') {
    content = JSON.stringify(content)
  }
  window.localStorage.setItem(name, content)
}

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return
  return window.localStorage.getItem(name)
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return
  window.localStorage.removeItem(name)
}

/**
 * 获取style样式
 */
export const getStyle = (element, attr, NumberMode = 'int') => {
  let target
  // scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
  if (attr === 'scrollTop') {
    target = element.scrollTop
  } else if (element.currentStyle) {
    target = element.currentStyle[attr]
  } else {
    target = document.defaultView.getComputedStyle(element, null)[attr]
  }
  // 在获取 opactiy 时需要获取小数 parseFloat
  return NumberMode === 'float' ? parseFloat(target) : parseInt(target)
}

/**
 * 数组分组
 * 如[1,1,1,1,1,1,1,1] => [[1,1,1],[1,1,1],[1,1]]
 */
export const group = (array, subGroupLength) => {
  let index = 0
  let newArray = []
  while (index < array.length) {
    newArray.push(array.slice(index, index += subGroupLength))
  }
  return newArray
}

/**
 * 在元素后插入
 */
export const insertAfter = (newElement, targetElement) => {
  let parent = targetElement.parentNode
  if (parent.lastChild === targetElement) {
    parent.appendChild(newElement)
  } else {
    parent.insertBefore(newElement, targetElement.nextSibling)
  }
}

/**
 * 格式化时间
 * @param {格式化格式} fmt 如 yyyy-MM-dd hh:mm:ss
 * @param {时间对象} date
 */
export const Format = (fmt, date) => {
  let o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
  }
  return fmt
}

/**
 * 只执行一次
 * @param {*} fn 要执行的函数
 */
export const once = fn => {
  let flag = false
  return function () {
    if (flag) return
    fn.apply(this, arguments)
    flag = true
  }
}

/*
* 判断数据类型函数的生产函数，传入的参数为数据类型，返回一个判断传入的参数是否为该类型的函数
*/
const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target)
export const isString = isType('String')
export const isObject = isType('Object')
export const isArray = isType('Array')

export const dealBloToURL = (blob, name) => {
  // const url = window.URL.createObjectURL(blob)
  // window.open(url)
  const href = window.URL.createObjectURL(blob)
  // window.open(url)
  // downloadElement.download = 'wudi'
  let downloadElement = document.createElement('a')
  downloadElement.href = href
  downloadElement.download = name
  document.body.appendChild(downloadElement)
  downloadElement.click()
  // document.body.removeChild(downloadElement)
  // window.URL.revokeObjectURL(href)
}

// 判断今天是否在某个时间段内 Sdate:开始时间，结束时间
export const isDuringDate = (Sdate, Edate) => {
  var today = new Date()
  var beginDate = new Date(Sdate)
  var endDate = new Date(Edate)
  if (today >= beginDate && today <= endDate) {
    return true
  }
  return false
}
