import { TimeFormatter } from "@/utils/time_format";
import { useSelector } from "react-redux";

// const common = useSelector((state: any) => state.common);

export const pickerDurations = () => {
    const list: string[] = [];
    for (let i = 0; i <= 22 * 12; i++) {
        var count = i * 5 + 60;
        var hour = Math.floor(count / 60);
        var minute = count % 60;
        list.push(`${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`);
    }
    return list;
}


//获取picker的取值范围，返回二维数组
export const durationDatas = (common: any) => {
    var min: number = 1
    var max: number = 23
    var step: number = 5
    
    if (common.duration) {
        min = common.duration.min
        max = common.duration.max
        step = common.duration.step
    }
    var minutes: string[] = []
    for (let i = 0; i < 60; i += step) {
        minutes.push(i+'分钟')
    }
    var hours: string[] = []
    for (let i = min; i <= max; i++) {
        hours.push(i+'小时')
    }
    return [hours, minutes]
}

//通过开始时间和结束时间计算picker的index值,返回格式为[hourIndex,minuteIndex]
export const durationIndex = (start: string, end: string,common: any) => {
    if ((start+'').indexOf(':') == -1) {
        start = TimeFormatter.formatTime(new Date(parseInt(start+'')));
    }
    if ((end+'').indexOf(':') == -1) {
        end = TimeFormatter.formatTime(new Date(parseInt(end+'')));
    }

    var min: number = 1
    var max: number = 23
    var step: number = 5
    // const common = useSelector((state: any) => state.common);
    if (common.duration) {
        min = common.duration.min
        max = common.duration.max
        step = common.duration.step
    }


    var startHour = parseInt(start.split(':')[0])
    var startMinute = parseInt(start.split(':')[1])
    var endHour = parseInt(end.split(':')[0])
    var endMinute = parseInt(end.split(':')[1])
    var startCount = startHour * 60 + startMinute
    var endCount = endHour * 60 + endMinute
    if (endCount < startCount) {
        endCount += 24 * 60
    }
    var leftCount = endCount - startCount
    var leftHour = Math.floor(leftCount / 60)
    var leftMinute = leftCount % 60
    leftHour = leftHour < min ? min : leftHour
    leftHour = leftHour > max ? max : leftHour
    return [leftHour - min, leftMinute / step]
}

//通过开始时间和结束时间计算duration,返回格式为[hour,minute]
export const durationTime = (start: string, end: string) => {
    var startHour = parseInt(start.split(':')[0])
    var startMinute = parseInt(start.split(':')[1])
    var endHour = parseInt(end.split(':')[0])
    var endMinute = parseInt(end.split(':')[1])
    var startCount = startHour * 60 + startMinute
    var endCount = endHour * 60 + endMinute
    if (endCount < startCount) {
        endCount += 24 * 60
    }
    var leftCount = endCount - startCount
    var leftHour = Math.floor(leftCount / 60)
    var leftMinute = leftCount % 60
    // leftHour = leftHour < 1 ? 1 : leftHour
    // leftHour = leftHour > 23 ? 23 : leftHour
    return [leftHour, leftMinute]
}

export const getTitle = (data)=>{
    if (data.scenario == 'FAST'){
        if (data.status == 'WAIT_FOR_START'){
            return '开始断食'
        } 
        return '结束断食'
    }
    else if (data.scenario == 'SLEEP'){
        if (data.status == 'WAIT_FOR_START'){
            return '开始睡眠'
        } 
        return '结束睡眠'
    }
    else {
        if (data.status == 'WAIT_FOR_START'){
            return '开始断食'
        } 
        else if (data.status == 'ONGOING1'){
            return '开始睡眠'
        }
        else if (data.state == 'ONGOING2'){
            return '结束睡眠'
        }
        return '结束睡眠'
    }
}