export default {
    isEmpty(v) {
        if (typeof v == "undefined" || v == null) {
            return true
        }
        if (v == 0 || v == "") {
            return true
        }
        return /^\s+$/g.test(v)
    },

    toTimeStamp(v, ymd) {
        if (this.isEmpty(v)) {
            return null
        }
        if (ymd) {
            ymd = ymd.replace(/-/g, "/")
            v = ymd + " " + v
        } else {
            v = v.replace(/-/g, "/")
        }
        return new Date(v).getTime() / 1000
    },

    setActivityCountdownTime(now, activity) {
        let beginTime = activity.beginTime  // 活动开始时间
        let endTime = activity.endTime  // 活动结束时间
        let dayBeginTime = activity.dayBeginTime // 每天开始时间
        let dayEndTime = activity.dayEndTime // 每天结束时间
        let nowY = now.getFullYear()
        let nowM = now.getMonth() + 1
        let nowD = now.getDate()
        const Ymd = nowY + "/" + nowM + "/" + nowD;
        dayBeginTime = this.toTimeStamp(dayBeginTime, Ymd)
        dayEndTime = this.toTimeStamp(dayEndTime, Ymd)
        beginTime = this.toTimeStamp(beginTime)
        endTime = this.toTimeStamp(endTime)
        const nowTime = now.getTime() / 1000
        let diffSec = 0;
        if (activity.timeType == 2) {
            if (beginTime == null || endTime == null) {
                return
            }
            // 固定时间段内活动
            beginTime = this.fixBeginTime(beginTime, activity.dayBeginTime)
            endTime = this.fixEndTime(endTime, activity.dayEndTime)
            if (nowTime < beginTime) {
                diffSec = beginTime - nowTime
                activity.status = 4
                activity.countDownTime = this.formatTimeBySec(diffSec)
                activity.statusText = "即将开始"
                return
            }
            if (nowTime > endTime) {
                activity.status = 3
                activity.statusText = "已结束"
                return
            }
        }
        // 长期时间，只要计算，距离当天时间即可
        if (dayBeginTime == null || dayEndTime == null) {
            return
        }
        if (nowTime < dayBeginTime) {
            diffSec = dayBeginTime - nowTime
            activity.status = 4
            activity.countDownTime = this.formatTimeBySec(diffSec)
            activity.statusText = "即将开始"
            return
        } 
        
        if (nowTime > dayEndTime) {
            diffSec = dayBeginTime + 86400 - nowTime
            activity.status = 5
            activity.countDownTime = this.formatTimeBySec(diffSec)
            activity.statusText = "即将开始"
            return
        } 
        activity.status = 2
        activity.statusText = "进行中"
    },
    
    fixBeginTime(time, hourTime) {
        if (this.isEmpty(hourTime)) {
            return time
        }
        const date = new Date(time * 1000)
        const ymd = date.getFullYear(date) + "/" + (date.getMonth() + 1) + "/" + date.getDate()
        const fullH = new Date(ymd + " " + hourTime).getTime() / 1000;
        return fullH > time ? fullH : time

    },
    fixEndTime(time, hourTime) {
        if (this.isEmpty(hourTime)) {
            return time
        }
        const date = new Date(time * 1000)
        const ymd = date.getFullYear(date) + "/" + (date.getMonth() + 1) + "/" + date.getDate()
        const fullH = new Date(ymd + " " + hourTime).getTime() / 1000;
        return fullH < time ? fullH : time
    },
    formatTimeBySec(sec) {
        const day = Math.floor(sec / 86400)
        const hour = Math.floor((sec % 86400) / 3600)
        const min = Math.floor(((sec % 86400) % 3600) / 60)
        const s = Math.floor((sec % 86400) % 3600 % 60)
        let rt = ''
        if (day > 0) {
            rt = day + '天'
        }
        rt += this.formatHMS(hour || 0) + ":"
        rt += this.formatHMS(min || 0) + ":"
        rt += this.formatHMS(s || 0)
        return rt
    },
    formatHMS(h) {
        if (h < 10) {
            return 0 + "" + h
        }
        return h
    }
}
