export function MessageTimeFormat(d) {
  var unix_stamp = ''
  if (!d) return
  if (typeof (d) === 'object') {
    unix_stamp = d.getTime()
  } else {
    unix_stamp = (new Date(d)).getTime()
  }
  var _today_obj = new Date()
  var _today_date = {
    y: _today_obj.getFullYear(),
    m: (_today_obj.getMonth() + 1 < 10 ? '0' + (_today_obj.getMonth() - -1) : (_today_obj.getMonth() - -1)),
    d: (_today_obj.getDate() < 10 ? '0' + _today_obj.getDate() : _today_obj.getDate())
  }

  var _today_stamp = Date.parse(_today_date.y + '/' + _today_date.m + '/' + _today_date.d + ' 00:00:00')

  var stamp = []
  stamp[0] = _today_stamp + 86400000
  stamp[1] = _today_stamp
  stamp[2] = _today_stamp - 86400000
  stamp[3] = _today_stamp - 172800000

  stamp[4] = _today_stamp - 518400000 // 7天

  stamp[5] = _today_stamp - 31536000000 // 365天

  var _compare_obj = new Date()
  _compare_obj.setTime(unix_stamp)

  var return_str

  if (unix_stamp >= stamp[1] && unix_stamp < stamp[0]) {
    return_str = _compare_obj.getHours() + ':' + (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj
      .getMinutes() : _compare_obj.getMinutes())
  } else if (unix_stamp >= stamp[2] && unix_stamp < stamp[1]) {
    var yesterdayText = '昨天'
    return_str = yesterdayText + ' ' + _compare_obj.getHours() + ':' +
            (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unix_stamp >= stamp[3] && unix_stamp < stamp[2]) {
    var theDayBeforeYesterdayText = '前天'
    return_str = theDayBeforeYesterdayText + ' ' + _compare_obj.getHours() + ':' +
            (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unix_stamp >= stamp[4] && unix_stamp < stamp[3]) { // 7天内
    var daynames = ['天', '一', '二', '三', '四', '五', '六']
    var dathStr = '星期' + daynames[_compare_obj.getDay()]

    var SundayText = '星期天'
    var MondayText = '星期一'
    var TuesdayText = '星期二'
    var WednesdayText = '星期三'
    var ThursdayText = '星期四'
    var FridayText = '星期五'
    var SaturdayText = '星期六'

    var dathStr2

    switch (dathStr) {
      case '星期天':
        dathStr2 = SundayText
        break
      case '星期一':
        dathStr2 = MondayText
        break
      case '星期二':
        dathStr2 = TuesdayText
        break
      case '星期三':
        dathStr2 = WednesdayText
        break
      case '星期四':
        dathStr2 = ThursdayText
        break
      case '星期五':
        dathStr2 = FridayText
        break
      case '星期六':
        dathStr2 = SaturdayText
        break
      default:
        dathStr2 = dathStr
        break
    }

    return_str = dathStr2 + ' ' + _compare_obj.getHours() + ':' +
            (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unix_stamp >= stamp[5] && unix_stamp < stamp[4]) { // 365天内
    var monthText = '月'
    var dayText = '日'
    return_str = (_compare_obj.getMonth() - (-1)) + monthText + _compare_obj.getDate() + dayText + ' ' +
            _compare_obj.getHours() + ':' + (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes()
      : _compare_obj.getMinutes())
  } else {
    var yearText = '年'
    const monthText = '月'
    const dayText = '日'
    return_str = _compare_obj.getFullYear() + yearText + (_compare_obj.getMonth() - (-1)) +
            monthText + _compare_obj.getDate() + dayText + ' ' + _compare_obj.getHours() + ':' +
            (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  }
  return return_str
}

// 页面初始时获取当前是本年第几周
export function getYearWeek(a, b, c) {
  // a为年 b为月 c为日
  /*
      date1是当前日期
      date2是当年第一天
      d是当前日期是今年第多少天
      用d + 当前年的第一天的周差距的和在除以7就是本年第几周
  */
  const date1 = new Date(a, parseInt(b) - 1, c)
  const date2 = new Date(a, 0, 1)
  const d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000)
  return Math.ceil((d + (date2.getDay() + 1 - 1)) / 7)
}

// 日期加减法  date参数为计算开始的日期，days为需要加的天数
// 格式:addDate('2017-1-11',20)
function addDate(date, days) {
  var d = new Date(date)
  d.setDate(d.getDate() + days)
  var m = d.getMonth() + 1

  return (
    d.getFullYear() +
    '-' +
    (m > 9 ? m : '0' + m) +
    '-' +
    (d.getDate() > 9 ? d.getDate() : '0' + d.getDate())
  )
}

// 第n年的第n周，获取开始和结束时间 (星期一为一周的开始)
export function week_date(weekstr) {
  const year = weekstr.split('-')[0]
  const weekNo = weekstr.split('-')[1]
  // 此年1号是星期几
  // console.log('调用时传的',year,weekNo);
  let oneday = new Date(year + '-01-01').getDay() // 0-6
  // console.log(oneday);
  // 方便计算，当为星期天时为7
  if (oneday === 0) {
    oneday = 7
  }

  let one_fistday
  let one_lastday
  // 如果1号刚好是星期一
  if (oneday === 1) {
    one_fistday = year + '-01-01'
    one_lastday = year + '-01-07'
  } else {
    const jj = 8 - oneday
    one_fistday =
      year -
      1 +
      '-12-' +
      (31 - oneday + 2 > 9 ? 31 - oneday + 2 : '0' + (31 - oneday + 2))
    one_lastday = year + '-01-' + (jj > 9 ? jj : '0' + jj)
  }

  let fistday
  let lastday
  // 如果刚好是第一周
  if (weekNo === 1) { // 在原文档进行了修改
    fistday = addDate(one_fistday, 7)
    lastday = addDate(one_lastday, 7)
  } else {
    fistday = addDate(one_lastday, (weekNo - 1) * 7 + 1)
    lastday = addDate(one_lastday, weekNo * 7)
  }
  console.log(fistday, lastday)
  return [fistday, lastday]
}

