import {lineData} from "@/components/lineChart/lineChart";
import {Text} from "@tarojs/components";

/**
 * 秒转分
 * s：秒
 * 20:20:20
 * 10:10
 */
export function formatMinute(s: number | string): string {
  const time = Number(s)
  if (isNaN(time)) {
    return ''
  }
  const h = parseInt(String(time / 3600))
  const min = parseInt(String(time / 60 % 60))
  const sec = Math.ceil(time % 60)
  const hours = h < 10 ? '0' + h : h
  const formatSecond = sec > 59 ? 59 : sec
  return `${hours > 0 ? `${hours}:` : ''}${min < 10 ? '0' + min : min}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`
}

/**
 * 小于60秒显示秒
 * 大于60s - 1h 显示分
 * 大于1h 显示小时
 */
export function formatTime(time: number, padding = 10): JSX.Element {
  if (time < 60) {
    return (<>
      {time}
      <Text style={{color: '#000', paddingLeft: `${padding}px`}}>秒</Text>
    </>)
  } else if (time >= 60 && time < 3600) {
    return (<>
      {(time / 60).toFixed(1)}
      <Text style={{color: '#000', paddingLeft: `${padding}px`}}>分钟</Text>
    </>)
  } else if (time >= 3600) {
    return (<>
      {(time / 3600).toFixed(1)}
      <Text style={{color: '#000', paddingLeft: `${padding}px`}}>小时</Text>
    </>)
  }
  return <></>
}

export function formatDateTime(date, format) {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? '上午' : '下午', // 上午/下午
    A: date.getHours() < 12 ? 'AM' : 'PM', // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      );
    }
  }
  return format;
}

export function formatDate(date, format) {
  const year = date.getFullYear(),
    month = date.getMonth() + 1,//月份是从0开始的
    day = date.getDate(),
    hour = date.getHours(),
    min = date.getMinutes(),
    sec = date.getSeconds();
  const preArr = Array.apply(null, Array(10)).map(function (_elem, index) {
    return '0' + index;
  });

  return format.replace(/YY/g, year)
    .replace(/MM/g, preArr[month] || month)
    .replace(/dd/g, preArr[day] || day)
    .replace(/hh/g, preArr[hour] || hour)
    .replace(/mm/g, preArr[min] || min)
    .replace(/ss/g, preArr[sec] || sec);
}


export function getMonday(): number {
  const now = new Date();
  const nowTime = now.setHours(0, 0, 0, 0);
  const day = now.getDay() || 7;  //为周日的时候 day 修改为7  否则当天周天会有问题
  const oneDayTime = 24 * 60 * 60 * 1000;
  return nowTime - (day - 1) * oneDayTime;//显示周一
}


export function getSunday(): number {
  const now = new Date();
  const nowTime = now.setHours(0, 0, 0, 0);
  const day = now.getDay() || 7;  //为周日的时候 day 修改为7  否则当天周天会有问题
  const oneDayTime = 24 * 60 * 60 * 1000;
  return nowTime + (7 - day) * oneDayTime
}

export function monthFirst(): number {
  const data = new Date();
  data.setDate(1);
  return data.setHours(0, 0, 0, 0)
}

export function monthEnd(): number {
  const data = new Date();
  if (data.getMonth() === 11) {
    data.setMonth(0);
  } else {
    data.setMonth(data.getMonth() + 1);
  }
  data.setDate(1);
  data.setHours(0);
  data.setSeconds(0);
  data.setMinutes(0);
  return (parseInt(String(data.getTime() / 1000)) - 1) * 1000;
}

export function everyDay(start_time: number, end_time: number, data: Record<number, number>): lineData[] {
  const time = 86400000
  try {
    const days = Math.floor((end_time - start_time) / time)
    if (isNaN(days)) return [];

    return new Array(days === 1 ? 1 : days + 1).fill(0).map((_, index) => {
      const day = start_time + index * time
      return {
        time: formatDateTime(new Date(day), 'MM/dd'),
        value: data?.[String(day)] || 0
      }
    })

  } catch (e) {
    return []
  }
}
