import { ref,onBeforeUnmount } from "vue"
/**
 * param:  "day" 表示 年-月-日 | 
 *      (默认) "time" 表示  年-月-日 时:分:秒 
 */
export function unioformDateTransform(date, param = "time"){
    if(date){
        let strDate = new Date(date).toLocaleString('zh-CN')
        let days = strDate.split(" ")[0]
        let time = strDate.split(" ")[1]
        let year = days.split("/")[0]
        let month = days.split("/")[1]
        let day = days.split("/")[2]

        if(Number(month)<10){
            month = '0'+ month
        }
        if(Number(day)<10){
            day = '0'+day
        }
        days =  year+'-'+month+'-'+day

        switch(param){
            case "day":
                return days
            case "time":
                return days + " " + time
        }
    }else{
        return ''
    }
}


// 时间戳 转换成 天/时/分/秒
export function TimestampToDate(timestamp){
    let remainStamp = timestamp
    let day = 0
    let hours = 0
    let minutes  = 0
    let seconds = 0

    day = Math.floor(remainStamp / 1000 / 60 /60 / 24 )
    remainStamp = remainStamp - day * 24 * 60 * 60 *1000

    hours = Math.floor(remainStamp / 1000 / 60 / 60) 
    remainStamp = remainStamp - hours * 60 * 60 * 1000

    minutes = Math.floor(remainStamp / 1000 / 60)
    remainStamp = remainStamp - minutes * 1000 * 60

    seconds = Math.floor(remainStamp / 1000)

    return {
        day,
        hours,
        minutes,
        seconds
    }
}

// 时:分:秒  ——>>  秒
export function TimeToSecond(time){
    let splitArr = time.split(':')
    let hour = splitArr[0]
    let minute = splitArr[1]
    let seconds = splitArr[2]
    return seconds + minute * 60 + hour * 60 * 60
}
interface CountDownItm {
    day?:string |number
    hours?:string | number
    minutes?:string | number
    abort:boolean
}

// 倒计时
export const useTimeCountDown = ()=>{
    const intervalFlag = ref()
    const deadlineCountDown = ref<CountDownItm>()
    // 开始倒计时
    // 截止时间 deadlineTime
    //  timeInterval  倒计时时间间隔
    const StartCounDown  =(deadlineTime,timeInterval=1000)=>{
        intervalFlag.value =  setInterval(()=>{
            // 截止日的
            let timestamp = new Date(deadlineTime).getTime()
            // 当前的
            let currentTimestamp = new Date().getTime()
            let sectionTime = timestamp - currentTimestamp
            if(sectionTime > 0){
                console.log('计时器----')
                deadlineCountDown.value =  {
                    ...TimestampToDate(sectionTime),
                    abort:false
                }
            }else{
                deadlineCountDown.value = {abort:true}
                clearInterval(intervalFlag.value)
                intervalFlag.value = null
            }
        },timeInterval)
    }

    // 销毁倒计时
    const DestoryInterval = ()=>{
        console.log('销毁倒计时')
        clearInterval(intervalFlag.value)
        intervalFlag.value = null
    }
    onBeforeUnmount(()=>{
        DestoryInterval()
    })

    return {
        DestoryInterval,
        deadlineCountDown,
        StartCounDown
    }
}