import {
  endOfMonth,
  endOfToday,
  endOfWeek,
  endOfYesterday,
  format,
  startOfMonth,
  startOfToday,
  startOfWeek,
  startOfYesterday,
  subMonths,
} from 'date-fns';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
const DATE_FORMAT = 'YYYY-MM-DD ';

export function formatToDateTime(date: Date, formatStr = DATE_TIME_FORMAT): string {
  const date2 = new Date(date);
  return format(date2, formatStr);
}

export function formatToDate(date: Date, formatStr = DATE_FORMAT): string {
  return format(date, formatStr);
}

export function timestampToTime(timestamp) {
  const date = new Date(timestamp * 1000);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = (date.getDate() + 1 <= 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  const h = (date.getHours() + 1 <= 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const m = (date.getMinutes() + 1 <= 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  const s = date.getSeconds() + 1 <= 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}

export function formatNanoSeconds(timestamp) {
  // 将纳秒转换为毫秒
  const timestampInMilliseconds = Math.floor(timestamp / 1000000);
  // 创建日期对象
  const date = new Date(timestampInMilliseconds);
  // 提取年、月、日、时、分、秒
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  const displayFormat = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  return displayFormat;
}

export function timestampToTimeNF(timestamp) {
  const date = new Date(timestamp);
  const Y = date.getFullYear();
  const M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  const D = date.getDate() + 1 <= 10 ? '0' + date.getDate() : date.getDate();
  const h = date.getHours() + 1 <= 10 ? '0' + date.getHours() : date.getHours();
  const m = date.getMinutes() + 1 <= 10 ? '0' + date.getMinutes() : date.getMinutes();
  const s = date.getSeconds() + 1 <= 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y.toString() + M.toString() + D.toString() + h.toString() + m.toString() + s.toString();
}

export function timestampToDate(timestamp) {
  const date = new Date(timestamp);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = date.getDate() + 1 <= 10 ? '0' + date.getDate() : date.getDate();
  return Y + M + D;
}

export function getTime() {
  const myDate = new Date();

  const hour = myDate.getHours().toString().padStart(2, '0');

  const minutes = myDate.getMinutes().toString().padStart(2, '0');

  const seconed = myDate.getSeconds().toString().padStart(2, '0');

  return hour + ':' + minutes + ':' + seconed;
}

export function getDate() {
  const myDate = new Date();

  const month = (myDate.getMonth() + 1).toString().padStart(2, '0');

  const day = myDate.getDate().toString().padStart(2, '0');

  return myDate.getFullYear() + '-' + month + '-' + day;
}

export function defaultStatisticsTimeOptions() {
  return new Date().getTime() - 86400 * 1000;
}

export function defaultTimeOptions(num = 7) {
  const dateStamp = new Date(new Date().toLocaleDateString()).getTime();
  console.log(dateStamp); // 1656345600000

  const date = new Date(dateStamp);
  // @ts-ignore
  return [date - 86400000 * num, date * 1];
}

export function defRangeShortcuts() {
  const nowDate = new Date();
  const dayBase = 86400 * 1000;
  return {
    今天: [startOfToday().getTime(), endOfToday().getTime()] as const,
    昨天: () => {
      return [startOfYesterday().getTime(), endOfYesterday().getTime()] as const;
    },
    近7天: [startOfToday().getTime() - dayBase * 6, endOfToday().getTime()] as const,
    近30天: [startOfToday().getTime() - dayBase * 29, endOfToday().getTime()] as const,
    近90天: [startOfToday().getTime() - dayBase * 89, endOfToday().getTime()] as const,
    本周: () => {
      return [
        startOfWeek(nowDate, { weekStartsOn: 1 }).getTime(),
        endOfWeek(nowDate, { weekStartsOn: 1 }).getTime(),
      ] as const;
    },
    本月: () => {
      return [startOfMonth(nowDate).getTime(), endOfMonth(nowDate).getTime()] as const;
    },
    上月: () => {
      return [
        startOfMonth(subMonths(nowDate, 1)).getTime(),
        endOfMonth(subMonths(nowDate, 1)).getTime(),
      ] as const;
    },
  };
}
