import { CurrentDot, RealRing, RingCommon, TargetRing } from "@/components/view/Rings"
import { ColorType } from "@/context/themes/color";
import { TimeFormatter } from "@/utils/time_format";







const startArc = (time: number) => {
    var date = new Date(time);
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
}

const durationArc = (start_time: number, end_time: number) => {
    var duration = (end_time - start_time) / 1000;
    return duration / (24 * 3600) * 2 * Math.PI;
}


export const ringWidth = 12
export const smallRingRadius = 28
export const bigRingRadius = 42
export const thirdRingRadius = 56


export const haveSmallRing = (data: any) => {
    if (data.scenario == 'FAST_SLEEP')
        return true
    return false
}

export const getCommon = (data: any, isBigRing: boolean) => {
    const commonBig: RingCommon = {
        useCase: 'Record',
        radius: 50,
        lineWidth: 8,
        isFast: true,
        status: 'WAIT_FOR_START'
    }

    const commonSmall: RingCommon = {
        useCase: 'Record',
        radius: 40,
        lineWidth: 8,
        isFast: true,
        status: 'WAIT_FOR_START'
    }

    if (isBigRing) {
        return commonBig
    }
    return commonSmall
}

export const getBgRing = () => {
    return {
        color: ColorType.ring
    }
}

export const getDot = (data: any, isBigRing: boolean) => {
    const currentDotBig: CurrentDot = {
        color: global.fastColor ? global.fastColor : ColorType.fast,
        lineWidth: 8,
        borderColor: 'black',
        offset:0
    }

    const currentDotSmall: CurrentDot = {
        color: global.sleepColor ? global.sleepColor : ColorType.sleep,
        lineWidth: 8,
        borderColor: 'black',
        offset:0
    }

    if (isBigRing) {
        if (data && data.scenario == 'SLEEP') {
            currentDotBig.color = global.sleepColor ? global.sleepColor : ColorType.sleep
        }
        return currentDotBig
    }
    else {
        return currentDotSmall
    }
}

export const dotIsOuterRange = (isTargetRange: boolean, scheduleData?: any, dayLightData?: any) => {
    // var date = new Date()
    // var minutes = date.getHours() * 60 + date.getMinutes()
    // if (isTargetRange) {

    //     var scheduleStart = new Date(scheduleData!.target_start_time)
    //     var scheduleEnd = new Date(scheduleData!.target_end_time)
    //     var startMinute = scheduleStart.getHours() * 60 + scheduleStart.getMinutes()
    //     var endMinute = scheduleEnd.getHours() * 60 + scheduleEnd.getMinutes()
    //     if (minutes < startMinute) {
    //         minutes += 1440
    //     }
    //     if (endMinute < startMinute) {
    //         endMinute += 1440
    //     }
    //     if (startMinute <= minutes && endMinute > minutes) {
    //         return false;
    //     }
    //     return true;
    // }

    // var startMinute = parseInt(dayLightData.sunrise.split(':')[0]) * 60 + parseInt(dayLightData.sunrise.split(':')[1])
    // var endMinute = parseInt(dayLightData.sunset.split(':')[0]) * 60 + parseInt(dayLightData.sunset.split(':')[1])
    // if (startMinute <= minutes && endMinute > minutes) {
    //     return true;
    // }
    return false;
}

export const getTarget = (data: any, isBigRing: boolean) => {
    const targetRingBig: TargetRing = {
        color: '#FF0000',
        startArc: 0,
        durationArc: 0
    }

    const targetRingSmall: TargetRing = {
        color: '#FF0000',
        startArc: 0,
        durationArc: 0
    }

    if (isBigRing) {
        if (data.scenario == 'SLEEP') {
            targetRingBig.color = global.sleepColor ? global.sleepColor : ColorType.sleep
            targetRingBig.startArc = startArc(data.sleep.target_start_time)
            targetRingBig.durationArc = durationArc(data.sleep.target_start_time, data.sleep.target_end_time)
        }
        else {
            var timestamp = data.fast.target_start_time
            targetRingBig.color = (global.fastColor ? global.fastColor : ColorType.fast) + '66'
            targetRingBig.startArc = startArc(timestamp)
            targetRingBig.durationArc = durationArc(data.fast.target_start_time, data.fast.target_end_time)
        }
        return targetRingBig
    }
    else {
        var timestamp = data.sleep.target_start_time
        targetRingSmall.startArc = startArc(timestamp)
        targetRingSmall.durationArc = durationArc(data.sleep.target_start_time, data.sleep.target_end_time)
        return targetRingSmall
    }
}

export const getSchedule = (data: any, isFast: boolean, isBigRing: boolean, isHourMinutesFormat?: boolean) => {

    var detail = data;
    if (isHourMinutesFormat) {
        var obj = data.schedule
        if (isFast) {
            var startHour = parseInt(obj.fast.start_time.split(':')[0])
            var startM = parseInt(obj.fast.start_time.split(':')[1])
            var endHour = parseInt(obj.fast.end_time.split(':')[0])
            var endM = parseInt(obj.fast.end_time.split(':')[1])
            var dt1 = new Date()
            dt1.setHours(startHour)
            dt1.setMinutes(startM)
            var startTimestamp = dt1.getTime()
            var dt2 = new Date()
            dt2.setHours(endHour)
            dt2.setMinutes(endM)
            var endTimestamp = dt2.getTime()
            if (startTimestamp > endTimestamp) {
                endTimestamp += 24 * 3600 * 1000
            }
            detail.fast = {
                target_start_time: startTimestamp,
                target_end_time: endTimestamp
            }
        }
        else {
            var startHour = parseInt(obj.sleep.start_time.split(':')[0])
            var startM = parseInt(obj.sleep.start_time.split(':')[1])
            var endHour = parseInt(obj.sleep.end_time.split(':')[0])
            var endM = parseInt(obj.sleep.end_time.split(':')[1])
            var dt1 = new Date()
            dt1.setHours(startHour)
            dt1.setMinutes(startM)
            var startTimestamp = dt1.getTime()
            var dt2 = new Date()
            dt2.setHours(endHour)
            dt2.setMinutes(endM)
            var endTimestamp = dt2.getTime()
            if (startTimestamp > endTimestamp) {
                endTimestamp += 24 * 3600 * 1000
            }
            detail.sleep = {
                target_start_time: startTimestamp,
                target_end_time: endTimestamp
            }
        }
    }



    const realRingBig: RealRing = {
        color: global.fastColor ? global.fastColor : ColorType.fast,
        startArc: 0,
        durationArc: 0
    }

    const realRingSmall: RealRing = {
        color: global.sleepColor ? global.sleepColor : ColorType.sleep,
        startArc: 0,
        durationArc: 0
    }

    if (isBigRing) {
        if (isFast) {
            realRingBig.startArc = startArc(detail.fast.target_start_time)
            realRingBig.durationArc = durationArc(detail.fast.target_start_time, detail.fast.target_end_time)
        }
        else {
            realRingBig.color = global.sleepColor ? global.sleepColor : ColorType.sleep
            realRingBig.startArc = startArc(detail.sleep.target_start_time)
            realRingBig.durationArc = durationArc(detail.sleep.target_start_time, detail.sleep.target_end_time)
        }
        return realRingBig
    }
    else {
        realRingSmall.startArc = startArc(detail.sleep.target_start_time)
        realRingSmall.durationArc = durationArc(detail.sleep.target_start_time, detail.sleep.target_end_time)
        return realRingSmall
    }
}


//把hh:mm转换成时间戳
export const timeTotimestamp = (data: any) => {
    var detail: any = {}
    var obj = data.schedule
    {
        var startHour = parseInt(obj.fast.start_time.split(':')[0])
        var startM = parseInt(obj.fast.start_time.split(':')[1])
        var endHour = parseInt(obj.fast.end_time.split(':')[0])
        var endM = parseInt(obj.fast.end_time.split(':')[1])
        var dt1 = new Date()
        dt1.setHours(startHour)
        dt1.setMinutes(startM)
        var startTimestamp = dt1.getTime()
        var dt2 = new Date()
        dt2.setHours(endHour)
        dt2.setMinutes(endM)
        var endTimestamp = dt2.getTime()
        if (startTimestamp > endTimestamp) {
            endTimestamp += 24 * 3600 * 1000
        }
        detail.fast = {
            target_start_time: startTimestamp,
            target_end_time: endTimestamp
        }
    }
    {
        var startHour = parseInt(obj.sleep.start_time.split(':')[0])
        var startM = parseInt(obj.sleep.start_time.split(':')[1])
        var endHour = parseInt(obj.sleep.end_time.split(':')[0])
        var endM = parseInt(obj.sleep.end_time.split(':')[1])
        var dt1 = new Date()
        dt1.setHours(startHour)
        dt1.setMinutes(startM)
        var startTimestamp = dt1.getTime()
        var dt2 = new Date()
        dt2.setHours(endHour)
        dt2.setMinutes(endM)
        var endTimestamp = dt2.getTime()
        if (startTimestamp > endTimestamp) {
            endTimestamp += 24 * 3600 * 1000
        }
        if (startTimestamp < detail.fast.target_start_time) {
            startTimestamp += 24 * 3600 * 1000
            endTimestamp += 24 * 3600 * 1000
        }
        detail.sleep = {
            target_start_time: startTimestamp,
            target_end_time: endTimestamp
        }
    }

    return detail
}

export const getReal = (data: any, isBigRing: boolean, isRecord: boolean) => {
    // console.log(data)
    const realRingBig: RealRing = {
        color: global.fastColor ? global.fastColor : ColorType.fast,
        startArc: 0,
        durationArc: 0
    }

    const realRingSmall: RealRing = {
        color: global.sleepColor ? global.sleepColor : ColorType.sleep,
        startArc: 0,
        durationArc: 0
    }

    if (isBigRing) {
        if (data.scenario == 'SLEEP') {
            realRingBig.color = global.sleepColor ? global.sleepColor : ColorType.sleep
            if (data.sleep.real_end_timezone && data.sleep.real_end_timezone.gmt && data.status == 'COMPLETED') {
                var timestamp = TimeFormatter.transferTimestamp(data.sleep.real_start_time, data.sleep.real_end_timezone.gmt)
                realRingBig.startArc = startArc(timestamp)
            }
            else {
                realRingBig.startArc = startArc(data.sleep.real_start_time)
            }

            realRingBig.durationArc = durationArc(data.sleep.real_start_time, isRecord ? (data.sleep.real_end_time ? data.sleep.real_end_time : new Date().getTime()) : new Date().getTime())
        }
        else {
            if (data.fast.real_end_timezone && data.fast.real_end_timezone.gmt && data.status == 'COMPLETED') {
                var timestamp = TimeFormatter.transferTimestamp(data.fast.real_start_time, data.fast.real_end_timezone.gmt)

                realRingBig.startArc = startArc(timestamp)
            }
            else {
                realRingBig.startArc = startArc(data.fast.real_start_time)
            }
            // realRingBig.startArc = startArc(data.fast.real_start_time)
            realRingBig.durationArc = durationArc(data.fast.real_start_time, isRecord ? (data.fast.real_end_time ? data.fast.real_end_time : new Date().getTime()) : new Date().getTime())
        }
        return realRingBig
    }
    else {
        if (!data.sleep) {
            return realRingSmall
        }
        if (data.sleep.real_end_timezone && data.sleep.real_end_timezone.gmt && data.status == 'COMPLETED') {
            var timestamp = TimeFormatter.transferTimestamp(data.sleep.real_start_time, data.fast.real_end_timezone.gmt ? data.fast.real_end_timezone.gmt : data.sleep.real_end_timezone.gmt)
            realRingSmall.startArc = startArc(timestamp)
        }
        else {
            realRingSmall.startArc = startArc(data.sleep.real_start_time)
        }
        // realRingSmall.startArc = startArc(data.sleep.real_start_time)
        realRingSmall.durationArc = durationArc(data.sleep.real_start_time, isRecord ? (data.sleep.real_end_time ? data.sleep.real_end_time : new Date().getTime()) : new Date().getTime())
        // if (isRecord && !isBigRing){
        //     debugger
        // }
        return realRingSmall
    }


}
