import { recordCheck } from "@/services/trackTimeDuration";
import trackTimeService, { machine } from "@/store/trackTimeMachine"

export const startFast = (start_time: number, duration: number) => {
    return new Promise((resolve) => {
        duration = duration < 3600 * 1000 ? 3600 * 1000 : duration;
        // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
        const extra = {
            set_time: global.set_time,
            confirm_time: new Date().getTime(),
        }
        recordCheck({
            action: 'FAST_START',
            real_check_time: start_time,
            target_duration: duration,
            extra: extra
        }).then(res => {
            resolve(res)
            trackTimeService.send({ type: 'START_FAST' });
        });
    });
}

export const endFast = (start_time: number) => {
    return new Promise((resolve) => {
        const extra = {
            set_time: global.set_time,
            confirm_time: new Date().getTime(),
        }
        recordCheck({
            action: 'FAST_END',
            real_check_time: start_time,
            extra: extra
        }).then(res => {
            trackTimeService.send({ type: 'END_FAST' });
            trackTimeService.send({ type: 'RESET' });
            trackTimeService.send({ type: global.scenario });
            resolve(res)
        });
    });
}


export const startSleep = (start_time: number, duration: number) => {
    duration = duration < 3600 * 1000 ? 3600 * 1000 : duration;
    // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
    const extra = {
        set_time: global.set_time,
        confirm_time: new Date().getTime(),
    }
    return new Promise((resolve) => {
        recordCheck({
            action: 'SLEEP_START',
            real_check_time: start_time,
            target_duration: duration,
            extra: extra
        }).then(res => {
            trackTimeService.send({ type: 'START_SLEEP' });
            resolve(res)
        });
    });
}
export const endSleep = (start_time: number) => {
    // const duration = 8 * 3600 * 1000;
    const extra = {
        set_time: global.set_time,
        confirm_time: new Date().getTime(),
    }
    return new Promise((resolve) => {
        recordCheck({
            action: 'SLEEP_END',
            real_check_time: start_time,
            extra: extra
        }).then(res => {
            trackTimeService.send({ type: 'END_SLEEP' });
            resolve(res)
        });
    });
}

