import { ColorType } from "@/context/themes/color";
import { TimeFormatter } from "@/utils/time_format";
import { useTranslation } from "react-i18next";
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, t) => {
    if (data.scenario == 'FAST') {
        if (data.status == 'WAIT_FOR_START') {
            return t('feature.track_time_duration.console.fast_start')
        }
        return t('feature.track_time_duration.console.fast_end')
    }
    else if (data.scenario == 'SLEEP') {
        if (data.status == 'WAIT_FOR_START') {
            return t('feature.track_time_duration.console.sleep_start')
        }
        return t('feature.track_time_duration.console.sleep_end')
    }
    else {
        if (data.status == 'WAIT_FOR_START') {
            return t('feature.track_time_duration.console.fast_start')
        }
        else if (data.status == 'ONGOING1') {
            return t('feature.track_time_duration.console.sleep_start')
        }
        else if (data.state == 'ONGOING2') {
            return t('feature.track_time_duration.console.sleep_end')
        }
        return t('feature.track_time_duration.console.sleep_end')
    }
}

export const getDurationTitle = (data, t) => {
    if (data.scenario == 'FAST') {
        return t('feature.track_time_duration.console.fast_duration')
    }
    else if (data.scenario == 'SLEEP') {
        return t('feature.track_time_duration.console.sleep_duration')
    }
    else {
        if (data.status == 'WAIT_FOR_START') {
            return t('feature.track_time_duration.console.fast_duration')
        }
        return t('feature.track_time_duration.console.sleep_duration')
    }
}

export const getTimePickerTitle = (data, t) => {
    if (data.scenario == 'FAST') {
        if (data.status == 'WAIT_FOR_START')
            return t('feature.track_time_duration.console.real_fast_start_dt')
        return t('feature.track_time_duration.console.real_fast_end_dt')
    }
    else if (data.scenario == 'SLEEP') {
        if (data.status == 'WAIT_FOR_START')
            return t('feature.track_time_duration.console.real_sleep_start_dt')
        return t('feature.track_time_duration.console.real_sleep_end_dt')
    }
    else {
        if (data.status == 'WAIT_FOR_START') {
            return t('feature.track_time_duration.console.real_fast_start_dt')
        }
        else if (data.status == 'ONGOING1') {
            return t('feature.track_time_duration.console.real_sleep_start_dt')
        }
        else if (data.status == 'ONGOING2') {
            return t('feature.track_time_duration.console.real_sleep_end_dt')
        }

        return t('feature.track_time_duration.console.real_fast_end_dt')
    }
}

//按钮颜色
export const getColor = (data) => {
    if (data.scenario == 'FAST') {
        return global.fastColor?global.fastColor:ColorType.fast
    }
    else if (data.scenario == 'SLEEP') {
        return global.sleepColor?global.sleepColor:ColorType.sleep
    }
    else {
        if (data.status == 'WAIT_FOR_START' || data.status == 'ONGOING3') {
            return global.fastColor?global.fastColor:ColorType.fast
        }
    }
    return global.sleepColor?global.sleepColor:ColorType.sleep
}

//透明度
export const textAlpha = (time) => {
    if (time.status == 'WAIT_FOR_START' || time.status == 'DONE') {
        return 1;
    }

    if (time.status == 'ONGOING' || time.status == 'ONGOING3') {
        if (time.scenario == 'SLEEP') {
            return time.sleep.target_end_time > new Date().getTime() ? 0.4 : 1
        }
        return time.fast.target_end_time > new Date().getTime() ? 0.4 : 1
    }
    if (time.status == 'ONGOING1')
        return 1//current.sleep.target_start_time > new Date().getTime()?0.4:1
    if (time.status == 'ONGOING2')
        return time.sleep.target_end_time > new Date().getTime() ? 0.4 : 1
    return 1
}

export const textNextStepAlpha = (time) => {
    return time.fast.target_end_time > new Date().getTime() ? 0.4 : 1
}