import { StringUtil } from "./StringUtil";

export class TimeUtil {

    //获取时间戳
    static getTimeStamp() {
        return Date.now()
    }

    //毫秒
    static getTime() {
        return Number(new Date().getTime());
    }

    //秒
    static getTimeEx(D_value) {
        return Math.floor(Number(new Date().getTime()) / 1000) + D_value;
    }

    //毫秒
    static getTimeEx2(D_value) {
        return Math.floor(Number(new Date().getTime())) + D_value;
    }

    //格式化时间(秒数到01:23:45)
    static formatTime(time: number) {
        if (time <= 0) return "";
        let h = Math.floor(time / 3600);
        let m = Math.floor((time / 60 % 60));
        let s = Math.floor((time % 60));
        let str: any = ((h < 10) ? "0{0}:" : "{0}:") + ((m < 10) ? "0{1}:" : "{1}:") + ((s < 10) ? "0{2}" : "{2}");
        return StringUtil.format(str, h, m, s);
    }

    //格式化时间(00:00)
    static formatTimeByTwo(time: number) {
        if (time <= 0) return "";
        let h = Math.floor(time / 3600);
        let m = Math.floor((time / 60 % 60));
        let s = Math.floor((time % 60));

        if (h > 0) {
            let str: any = ((h < 10) ? "0{0}:" : "{0}:") + ((m < 10) ? "0{1}" : "{1}")
            return StringUtil.format(str, h, m);
        } else {
            let str: any = ((m < 10) ? "0{0}:" : "{0}:") + ((s < 10) ? "0{1}" : "{1}");
            return StringUtil.format(str, m, s);
        }
    }
    static getDateDay(time, half?) {
        let date = new Date(time * 1000);
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ''
        if (half) {
            return Y + M + D;
        }
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + '  ' + h + m + s
    }
    //日/月 时:分 (毫秒制)
    static getDate_DDMMHHMM_ms(time) {
        let date = new Date(time);
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + ''
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '/'
        
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
        return D + M + ' ' + h + m 
    }

    static getDateDay_DDMMYYYY_hhmm(time) {
        let date = new Date(time);
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '-'
        let Y = date.getFullYear() + ''
        
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ''
        return D + M + Y + '  ' + h + m
    }
    
    //日/月 (毫秒制)
    static getDate_DDMM_ms(time) {
        let date = new Date(time);
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + ''
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '/'
        return D + M 
    }

    //两个时间间隔天数
    static getDayInterval(time1: number, time2: number) {
        let format_time = (count: number) => {
            let time = new Date(count);
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let date = time.getDate();
            return (year + "-" + month + "-" + date)
        };
        return (Date.parse(format_time(Math.max(time1, time2))) - Date.parse(format_time(Math.min(time1, time2)))) / 86400000;
    }

    /**
     * 结束时间为跨天凌晨5点
     * @param serverTime 
     * @returns 
     */
    static getTodayEndTime(serverTime: number) {
        let time = new Date(serverTime * 1000);
        if (time.getHours() >= 5) {
            time.setHours(5, 0, 0, 0);
            return Number((time.getTime() + 86400000) / 1000);
        } else {
            time.setHours(5, 0, 0, 0);
            return Number(time.getTime() / 1000);
        }
    }

    /**
     * 时间格式化（倒计时）
     * @param seconds 要转换的时间
     * @param isShowDay 是否以：天/小时/分钟 的格式显示
     * @returns 返回当前传入时间对应的格式字符串
     */
    static formatCountdown(seconds: number, isShowDay: boolean) {
        let day = Math.floor(seconds / 60 / 60 / 24);
        let strReturn = '';
        if (isShowDay) {
            let hours = Math.floor((seconds % (60 * 60 * 24)) / 60 / 60);
            let minutes = Math.floor(seconds / 60 % 60);
            if (day >= 1) {
                strReturn = strReturn + day + '天';
            }
            strReturn = strReturn + hours + '时';
            strReturn = strReturn + minutes + '分';
        } else {
            let hours = Math.floor(seconds / 60 / 60);
            seconds = seconds - hours * 60 * 60;
            let minutes = Math.floor(seconds / 60);
            seconds = seconds - minutes * 60;
            strReturn = hours + '时', minutes + '分', seconds + '秒';
        }
        return strReturn;
    }

    /** 将秒转换为时分秒的时间格式 */
    static secondsToHMS(seconds: number, isShowHour?: boolean): string {
        if (!seconds) {
            seconds = 0;
        }
        const hours = Math.floor(seconds / 3600);
        const minutes = Math.floor((seconds % 3600) / 60);
        const remainingSeconds = seconds % 60;

        let timeStr = '';
        if (isShowHour) {
            // const hoursStr = hours < 10 ? "0" + hours : hours.toString();
            const hoursStr = hours.toString();
            const minutesStr = minutes < 10 ? "0" + minutes : minutes.toString();
            const secondsStr = remainingSeconds < 10 ? "0" + remainingSeconds : remainingSeconds.toString();
            timeStr = `${hoursStr}:${minutesStr}:${secondsStr}`;
        } else {
            const minutesStr = minutes < 10 ? "0" + minutes : minutes.toString();
            const secondsStr = remainingSeconds < 10 ? "0" + remainingSeconds : remainingSeconds.toString();
            timeStr = `${minutesStr}:${secondsStr}`;
        }

        return timeStr;
    }

}