import moment from "moment-timezone";

export function lastDayOfMonth(date ) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份从0开始计算，需要加1
  const lastDay = new Date(year, month, 0).getDate();
  return lastDay;
}

/**
 * 两个日期的相差天数
 * */
export function DateDiff(sDate1, sDate2) {
  const dateParts1 = sDate1.split("-");
  const oDate1 = new Date(Date.UTC(dateParts1[0], dateParts1[1] - 1, dateParts1[2]));

  const dateParts2 = sDate2.split("-");
  const oDate2 = new Date(Date.UTC(dateParts2[0], dateParts2[1] - 1, dateParts2[2]));

  const timeDiff = Math.abs(oDate2 - oDate1);
  const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

  return (daysDiff+1);
}




/**
 * 不晚于结束
 *
 * */
export function pickerOptionsStart(endTime){
  return {
    disabledDate: time => {
      let endDateVal = endTime;
      if (endDateVal) {
        return time.getTime() > new Date(endDateVal).getTime();
      }
    }
  }
}

/**
 * 不早于开始
 *
 * */
export function pickerOptionsEnd(startTime){
  return {
    disabledDate: time => {
      let endDateVal = startTime;
      if (endDateVal) {
        return time.getTime() < new Date(endDateVal).getTime();
      }
    }
  }
}

/**
 * 比较两个时间的先后，控制精度
 * @param date1
 * @param date2
 * @param accuracy y年  m月  d日
 * @return -1:date1<date2,0:date1=date2,1:date1>date2
 */
export function compareDateWithAccuracy(time1 ,time2,accuracy = 'd'){
  let date1 = isDate(time1) ? time1 : new Date(time1)
  let date2 = isDate(time2) ? time2 : new Date(time2)
  let year1 = date1.getFullYear();
  let year2 = date2.getFullYear();

  let month1 = date1.getMonth() + 1;
  let month2 = date2.getMonth() + 1;

  let day1 =  date1.getDate();
  let day2 = date2.getDate();

  if (accuracy === 'y') {
    return year1 > year2 ? 1 : year1 === year1 ? 0 : -1
  } else if (accuracy === 'm'){
    if(year1 > year2){
      return 1
    }else if(year1 < year2){
      return -1
    }else{
      return month1 > month2 ? 1 : month1 === month2 ? 0 : -1
    }
  } else if ( accuracy === 'd') {
    if(year1 > year2){
      return 1
    }else if(year1 < year2){
      return -1
    }else{
      if(month1 > month2){
        return 1
      }else if(month1 < month2){
        return -1
      }else{
        return day1 > day2 ? 1 : day1 === day2 ? 0 : -1
      }
    }
  }
}


/**
 * 比较两个时间的先后，控制精度, 获取较小的时间（默认）
 * @param date1
 * @param date2
 * @param accuracy y年  m月  d日
 * @return -1:date1<date2,0:date1=date2,1:date1>date2
 */
export function getCompareDateWithAccuracy(time1 ,time2,accuracy = 'd',flag = true){
  if((time1 == undefined || time1 == null) && (time2 == undefined || time2 == null)) { return   null}
  if(time1 == undefined || time1 == null ){ return time2 }
  if(time2 == undefined || time2 ==null) { return time1 }
  let date1 = isDate(time1) ? time1 : new Date(time1)
  let date2 = isDate(time2) ? time2 : new Date(time2)
  let year1 = date1.getFullYear();
  let year2 = date2.getFullYear();

  let month1 = date1.getMonth() + 1;
  let month2 = date2.getMonth() + 1;

  let day1 =  date1.getDate();
  let day2 = date2.getDate();

  if (accuracy === 'y') {
    if(flag){
      return year1 >= year2 ? time2 : time1
    }else{
      return year1 >= year2 ? time1 : time2
    }

  } else if (accuracy === 'm'){
    if(flag){
      if(year1 > year2){
        return time2
      }else if(year1 < year2){
        return time1
      }else{
        return month1 > month2 ? time2 : (month1 === month2 ? time2 : time1)
      }
    }else{
      if(year1 > year2){
        return time1
      }else if(year1 < year2){
        return time2
      }else{
        return month1 > month2 ? time1 : (month1 === month2 ? time1 : time2)
      }
    }

  } else if ( accuracy === 'd') {
    if(flag){
      if(year1 > year2){
        return time2
      }else if(year1 < year2){
        return time1
      }else{
        if(month1 > month2){
          return time2
        }else if(month1 < month2){
          return time1
        }else{
          return day1 > day2 ? time2 : day1 === day2 ? time2 : time1
        }
      }
    }else{
      if(year1 > year2){
        return time1
      }else if(year1 < year2){
        return time2
      }else{
        if(month1 > month2){
          return time1
        }else if(month1 < month2){
          return time2
        }else{
          return day1 > day2 ? time1 : day1 === day2 ? time1 : time2
        }
      }
    }
  }

}

export function isDate(date){
  if(typeof date === 'object' && date instanceof Date){
    return true
  }else{
    return false
  }
}

export function  isArray(arr){
  if(typeof arr === 'object' && arr instanceof Array){
    return true
  }else{
    return false
  }
}


/**
 * 获取时间段或时间中的最小时间
 * @param arr
 */
export function getMinDateFromArray(arr){
  let min = null
  if(arr == null || arr.length === 0){
    return null
  }
  if(arr.length === 1){
    return arr[0]  !== undefined && arr[0]  != null  ? (isArray(arr[0]) ? arr[0][0] : arr[0] ) : null
  }
  for (const arrElement of arr) {
      if(arrElement !== undefined && arrElement != null){
        if(isArray(arrElement)){
          min = getCompareDateWithAccuracy(min,arrElement[0]);
        }else{
          min = getCompareDateWithAccuracy(min,arrElement);
        }

      }

  }
  return min
}


/**
 * 获取时间段或时间中的最大时间
 * @param arr
 */
export function getMaxDateFromArray(arr){
  let max = null
  if(arr == null || arr.length == 0){
    return null
  }
  if(arr != null && arr.length == 1){
    return arr[0] != undefined && arr[0]  != null ? (isArray(arr[0]) ? arr[0][1] : arr[0] ): null
  }
  for (const arrElement of arr) {
    if(arrElement != undefined && arrElement != null){
      if(isArray(arrElement)){
        max = getCompareDateWithAccuracy(max,arrElement[1],'d',false);
      }else {
        max = getCompareDateWithAccuracy(max,arrElement,'d',false);
      }
    }
  }
  return max
}



/**
 * 兼容火狐的 new Date()
 *
 * */
export function fireFoxNewDate(sDate1){
  const dateParts1 = sDate1.split("-");
  return new Date(Date.UTC(dateParts1[0], dateParts1[1] - 1, dateParts1[2]));
}



// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = 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 time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

/**
 *  三个月前的时间
 * */
export function threeDate(includesCurrent=true) {

  let date = new Date()
  let currentY = date.getFullYear();
  // 三个月前的时间减2
  let currentM = date.getMonth() - 2;
  let lastM = new Date(currentY,currentM,0);
  // 日期小于10，前面加10
  let lastDay = lastM.getDate() < 10 ? '0' + lastM.getDate() : lastM.getDate()
  // 对特殊月份1,2,3,进行特殊判断
  let d = new Date()
  let realYear = null
  // 年份应该减1
  if (d.getMonth() == 0 || d.getMonth() == 1 || d.getMonth() == 2) {
    realYear = d.getFullYear() - 1
  } else {
    realYear = d.getFullYear()
  }
  let realMonth = null
  // 3月对应12月，2月对应11月，1月对应10月
  if (d.getMonth()-2 == 0) {
    realMonth = 12
  } else if (d.getMonth()-2 == -1) {
    realMonth = 11
  } else if (d.getMonth()-2 == -2) {
    realMonth = 10
  } else {
    realMonth = d.getMonth() - 2
  }
  // a是当前日期，lastDay是三个月前日期
  let a = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
  realMonth = realMonth < 10 ? '0' + realMonth : realMonth
  // 当前时间大于三个月前，时间取三个月前的时间，小于或者等于，取当前时间
  let realDay = a > lastDay ? lastDay : a
  // 返回三个月前日期
  if(!includesCurrent){
    return realYear + '-' + realMonth + '-' + realDay
  }
  realDay-=1
  return realYear + '-' + realMonth + '-' + realDay
}


export  function judgeIsBeforeNow(time){
 return moment(time).isBefore(new Date(),'day')
}




