

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) {  
  var o = {
      "M+": this.getMonth() + 1, //月份 
      "d+": this.getDate(), //日 
      "h+": this.getHours(), //小时 
      "m+": this.getMinutes(), //分 
      "s+": this.getSeconds(), //秒 
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
      "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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;
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatChatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[month, day].map(formatNumber).join('-')} ${[hour, minute].map(formatNumber).join(':')}`
}

const getDateArr = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day, hour, minute, second]
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const getTimeAgo = stringTime =>{
  var minute = 1000 * 60;
  var hour = minute *60;
  var day = hour *24;
  var week = day * 7;
  var month = day * 30;
  var time1 = new Date().getTime();//当前的时间戳
  var time2 = stringTime*1000;//指定时间的时间戳
  var time = time1 - time2;
  var result = null;
  if(time < 0){
    return '刚刚';
  }else if(time/month >= 1){
    result = (new Date(time2)).Format("yyyy-MM-dd hh:mm:ss");
  }else if(time/week >= 1){
    result = (new Date(time2)).Format("MM-dd hh:mm:ss");
  }else if(time/day >= 1){
    result = (new Date(time2)).Format("MM-dd hh:mm");
  }else if(time/hour >= 1){
    result = parseInt(time/hour) + "小时前";
  }else if(time/minute >= 1){
    result = parseInt(time/minute) + "分钟前";
  }else {
    result = "刚刚";
  }
  return result;
}

const getTimeAgoChat = stringTime =>{
  console.log("stringTime: ", stringTime)
  var minute = 1000 * 60;
  var hour = minute *60;
  var day = hour *24;
  var week = day * 7;
  var month = day * 30;
  var time1 = new Date().getTime();//当前的时间戳
  var time2 = stringTime;//指定时间的时间戳
  var time = time1 - time2;
  var result = null;
  if(time < 0){
    return '刚刚';
  }else if(time/month >= 1){
    result = (new Date(time2)).Format("yyyy-MM-dd hh:mm:ss");
  }else if(time/week >= 1){
    result = (new Date(time2)).Format("MM-dd hh:mm:ss");
  }else if(time/day >= 1){
    result = (new Date(time2)).Format("MM-dd hh:mm");
  }else if(time/hour >= 1){
    result = parseInt(time/hour) + "小时前";
  }else if(time/minute >= 1){
    result = parseInt(time/minute) + "分钟前";
  }else {
    result = "刚刚";
  }
  return result;
}

//仿微信时间显示
const likeWeChatTimeAgoChat = stringTime =>{
  console.log("stringTime: ", stringTime)
  var minute = 1000 * 60;
  var hour = minute *60;
  var day = hour *24;
  var week = day * 7;
  var month = day * 30;
  var time1 = new Date().getTime();//当前的时间戳
  var time2 = stringTime;//指定时间的时间戳
  var time = time1 - time2;
  var result = null;
  if(time < 0){
    return '刚刚';
  }else if(time/month >= 1){
    result = (new Date(time2)).Format("yyyy-MM-dd hh:mm:ss");
  }else if(time/week >= 1){
    result = (new Date(time2)).Format("MM-dd hh:mm:ss");
  }else if(time/day >= 1){
    result = (new Date(time2)).Format("MM-dd hh:mm");
  }else if(time/hour >= 1){
    result = parseInt(time/hour) + "小时前";
  }else if(time/minute >= 1){
    result = parseInt(time/minute) + "分钟前";
  }else {
    result = "刚刚";
  }
  return result;
}

// px 转换为 rpx ，传参类型是数字（Number）
const pxTorpx = px => {
  let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  let rpx = (750 / deviceWidth) * Number(px)
  return Math.floor(rpx);
}
// px 转换为 rpx ，传参类型是数字（Number）
const rpxTopx = rpx => {
  let px = rpx / 750 * wx.getSystemInfoSync().windowWidth;
  return Math.floor(px);
}

module.exports = {
  formatTime,
  getDateArr,
  formatNumber,
  getTimeAgo,
  rpxTopx,
  pxTorpx,
  getTimeAgoChat,
  likeWeChatTimeAgoChat
}
