import dayjs from 'dayjs';
import { get } from 'http';

export function getNowDateAndMonday(time: number) {
  let timestamp = time;
  const serverDate = new Date(time);
  if (serverDate.getDay() == 0) {
    timestamp -= 7 * 24 * 60 * 60 * 1000;
  }
  const mondayTime =
    timestamp - (serverDate.getDay() - 1) * 24 * 60 * 60 * 1000;

  const mondayData = new Date(mondayTime).getTime();
  //年
  // const mondayY = mondayData.getFullYear();
  // //月
  // const mondayM =
  //   mondayData.getMonth() + 1 < 10
  //     ? '0' + (mondayData.getMonth() + 1)
  //     : mondayData.getMonth() + 1;
  // //日
  // const mondayD =
  //   mondayData.getDate() < 10
  //     ? '0' + mondayData.getDate()
  //     : mondayData.getDate();

  // const str = mondayY + '-' + mondayM + '-' + mondayD;
  return mondayData;
}
export function getNowDateAndSunday(time: number) {
  const timestamp = time;
  const serverDate = new Date(time);

  let num = 7 - serverDate.getDay();
  if (num == 7) {
    num = 0;
  }
  const sundayTiem = timestamp + num * 24 * 60 * 60 * 1000;
  const SundayData = new Date(sundayTiem).getTime();
  //年
  // const tomorrowY = SundayData.getFullYear(); //月
  // const tomorrowM =
  //   SundayData.getMonth() + 1 < 10
  //     ? '0' + (SundayData.getMonth() + 1)
  //     : SundayData.getMonth() + 1;
  // //日
  // const tomorrowD =
  //   SundayData.getDate() < 10
  //     ? '0' + SundayData.getDate()
  //     : SundayData.getDate();
  // const str = tomorrowY + '-' + tomorrowM + '-' + tomorrowD;

  return SundayData;
}

export const getTimes = (
  times: any,
  keys: Array<string> = [],
  format = 'YYYY-MM-DD'
) => {
  if (times && times.length) {
    return format == 'YYYY-MM-DD'
      ? {
          [keys[0] || 'start']: dayjs(times[0]).isValid()
            ? dayjs(times[0]).format(format) + ' 00:00:00'
            : '',
          [keys[1] || 'end']: dayjs(times[1]).isValid()
            ? dayjs(times[1]).format(format) + ' 23:59:59'
            : ''
        }
      : {
          [keys[0] || 'start']: dayjs(times[0]).isValid()
            ? dayjs(times[0]).format(format)
            : '',
          [keys[1] || 'end']: dayjs(times[1]).isValid()
            ? dayjs(times[1]).format(format)
            : ''
        };
  }
  return {};
};

export function formatTime(time: number) {
  const hours = Math.floor(time / 3600);

  const minutes = Math.floor(Math.floor(time % 3600) / 60);

  const seconds = Math.floor(time % 60);

  const h = hours.toString().length === 1 ? `0${hours}` : hours;

  const m = minutes.toString().length === 1 ? `0${minutes}` : minutes;

  const s = seconds.toString().length === 1 ? `0${seconds}` : seconds;
  let str = '';
  if (hours > 0) {
    str = `${h}小时${m}分钟${s}秒`;
  } else if (minutes > 0) {
    str = `${m}分钟${s}秒`;
  } else {
    str = `${s}秒`;
  }
  console.log();
  return str;
}

export function getHours(time: number) {
  const minutes = Math.floor(time / 60 / 60);
  return minutes;
}

export function getLastMinutes(time: number) {
  const minutes = Math.floor(time / 60);
  return Math.floor(minutes % 60)
}

export function getMinutes(time: number) {
  const minutes = Math.floor(time / 60);
  return minutes;
}

export function getSecend(time: number) {
  const seconds = Math.floor(time % 60);
  return seconds;
}

export function getChatMinutes(time: number) {
  const minutes = Math.floor(time / 60);
  let s = 0
  if(time % 60) {
    s = Math.ceil(time % 60 / 60 * 100) / 100
  }
  return minutes + s;
}


// 秒转时分秒
export function formateSeconds(endTime: string, pad = 2) {
  let secondTime = parseInt(endTime) //将传入的秒的值转化为Number
  let min = 0 // 初始化分
  let h = 0 // 初始化小时
  let result = ''
  if (secondTime >= 60) {
    //如果秒数大于等于60，将秒数转换成整数
    min = parseInt(secondTime / 60 + '') //获取分钟，除以60取整数，得到整数分钟
    secondTime = parseInt((secondTime % 60) + '') //获取秒数，秒数取佘，得到整数秒数
    if (min >= 60) {
      //如果分钟大于等于60，将分钟转换成小时
      h = parseInt(min / 60 + '') //获取小时，获取分钟除以60，得到整数小时
      min = parseInt((min % 60) + '') //获取小时后取佘的分，获取分钟除以60取佘的分
    }
  }
  if (h) {
    result = `${h.toString().padStart(pad, '0')}时${min.toString().padStart(pad, '0')}分${secondTime
      .toString()
      .padStart(pad, '0')}秒`
  } else if (min) {
    result = `${min.toString().padStart(pad, '0')}分${secondTime.toString().padStart(pad, '0')}秒`
  } else {
    result = `${secondTime.toString().padStart(pad, '0')}秒`
  }
  return result
}