export const formatDate = (date: Date | string): any => {
    if (date === undefined || date === null) {
        return '-';
    }

    let dateDetail: Date;

    if (date instanceof Date) {
        dateDetail = date;
    } else {
        dateDetail = new Date(date as string);
        if (isNaN(dateDetail.getTime())) {
            return '-';
        }
    }
    const year = dateDetail.getFullYear();
    const month = String(dateDetail.getMonth() + 1).padStart(2, '0');
    const day = String(dateDetail.getDate()).padStart(2, '0');

    const hours = String(dateDetail.getHours()).padStart(2, '0');
    const minutes = String(dateDetail.getMinutes()).padStart(2, '0');
    const seconds = String(dateDetail.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
export const formatListTime = (date: any) => {
    let dateDetail: Date;
    if (date instanceof Date) {
        dateDetail = date;
    } else if (typeof date === 'string') {
        dateDetail = new Date(date);
        if (isNaN(dateDetail.getTime())) {
            return '-';
        }
    } else {
        return '-';
    }
    const year = dateDetail.getFullYear();
    const month = String(dateDetail.getMonth() + 1).padStart(2, '0');
    const day = String(dateDetail.getDate()).padStart(2, '0');
    const hours = dateDetail.getHours();
    const minutes = String(dateDetail.getMinutes()).padStart(2, '0');
    const seconds = String(dateDetail.getSeconds()).padStart(2, '0');// 将24小时制转换为12小时制
    //如果不是今年 返回年
    if (dateDetail.getFullYear() !== new Date().getFullYear()) {
        return `${year}`;
    }
    //如果是今天,返回时和分,如果不是今天,返回月和日

    if (dateDetail.toDateString() === new Date().toDateString()) {
        return `${hours}:${minutes}`;
    } else {
        return `${month}-${day}`;
    }
}


export const formatDateArray = (dates: Date[]): string[] => {
    return dates.map(date => formatDate(date));
};

export const getToday = () => {
    const today = new Date();
    return {
        start: formatDate(new Date(today.setHours(0, 0, 0, 0))),
        end: formatDate(new Date(today.setHours(23, 59, 59, 999)))
    };
};

export const getYesterday = () => {
    const yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    return {
        start: formatDate(new Date(yesterday.setHours(0, 0, 0, 0))),
        end: formatDate(new Date(yesterday.setHours(23, 59, 59, 999)))
    };
};

export const getTomorrow = () => {
    const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    return {
        start: formatDate(new Date(tomorrow.setHours(0, 0, 0, 0))),
        end: formatDate(new Date(tomorrow.setHours(23, 59, 59, 999)))
    };
};

export const getThisWeek = () => {
    const today = new Date();
    const startOfWeek = new Date(today.setDate(today.getDate() - today.getDay()));
    const endOfWeek = new Date(today.setDate(today.getDate() - today.getDay() + 6));
    return {
        start: formatDate(new Date(startOfWeek.setHours(0, 0, 0, 0))),
        end: formatDate(new Date(endOfWeek.setHours(23, 59, 59, 999)))
    };
};

export const getThisMonth = () => {
    const today = new Date();
    const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
    const endOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
    return {
        start: formatDate(new Date(startOfMonth.setHours(0, 0, 0, 0))),
        end: formatDate(new Date(endOfMonth.setHours(23, 59, 59, 999)))
    };
};

export const getDaysBetweenDates = (startDateStr: string, endDateStr: string): string[] => {
    const startDate = new Date(startDateStr);
    const endDate = new Date(endDateStr);
    const days: string[] = [];
    let currentDate = new Date(startDate);

    while (currentDate <= endDate) {
        days.push(formatDate(currentDate).split(' ')[0]);
        currentDate.setDate(currentDate.getDate() + 1);
    }

    return days;
};

export const getEndOfDay = (date: Date | string): string => {
    let dateDetail: Date;

    if (date instanceof Date) {
        dateDetail = new Date(date);
    } else {
        dateDetail = new Date(date);
        if (isNaN(dateDetail.getTime())) {
            return '-';
        }
    }

    dateDetail.setHours(23, 59, 59, 0); // 设置为当天 23:59:00
    return formatDate(dateDetail).slice(0, 19); //
};
export const getCurrentDate = () => {
    const date = new Date();
    const year = date.getFullYear();
    // 月份从0开始，所以要加1
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1并补0
    const day = String(date.getDate()).padStart(2, '0'); // 补0操作
    return `${year}-${month}-${day}`;
}
//帮我写个方法获取前30天日期并返回
export const getPrevious30Days = () => {
    const dates = [];
    const currentDate = new Date();
    for (let i = 0; i < 31; i++) {
        const date = new Date(currentDate);
        date.setDate(currentDate.getDate() - i);
        dates.push(date.toISOString().slice(0, 10));
    }
    return dates[dates.length - 1];
}

//获取当前时间的时分
export const getCurrentTime = (date:any) => {
    const today = new Date();
    const selectedDate =date ? new Date(date) : today;

    // 判断是否是今天
    const isToday =
        selectedDate.getFullYear() === today.getFullYear() &&
        selectedDate.getMonth() === today.getMonth() &&
        selectedDate.getDate() === today.getDate();

    if (isToday) {
        // 如果是今天，返回当前时间如 "14:30"
        return `${String(today.getHours()).padStart(2, '0')}:${String(today.getMinutes()).padStart(2, '0')}`;
    } else {
        // 如果不是今天，返回 null 或 undefined，表示不设置 min-time
        return undefined;
    }
};