

import { API_FAST_PLANS, API_FAST_CHECKS, API_FAST_CLOCKS, API_CLOCK_RECORDS, API_CLOCK_HOME, API_CLOCK_STATS, API_CLOCK_SUMMARY_RECORDS, API_CLOCK_RECORD_UPDATE, API_CLOCK_STREAKS, API_EAT_WAKES, API_FAST_WINDOW, API_SLEEP_WINDOW, API_SET_SCHEDULE } from './http/api'
import { request } from './http/request';
import { getLocalPush } from '@/features/trackTimeDuration/actions/TrackTimeActions';

export const getPlans = () => {
    return new Promise((resolve, reject) => {
        request({
            url: API_FAST_PLANS, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        }).catch(e => {
            reject(e)
        })
    })
}

export const setPlan = (params: Record<string, any> | undefined) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_FAST_PLANS, method: 'POST', data: { ...params }
        }).then(res => {
            resolve(res);
            // getLocalPush();
        }).catch(e => {
            reject(e)
        })
    })
}

export const getChecks = () => {
    return new Promise((resolve) => {
        request({
            url: API_FAST_CHECKS + `?scenario=${global.scenario}`, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        })
    })
}

export const clockHome = () => {
    return new Promise((resolve, reject) => {
        request({
            url: API_CLOCK_HOME, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        }).catch(e => {
            reject(e)
        })
    })
}

export const clockSummaryStats = (params: any) => {
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_STATS, method: 'GET', data: { ...params }
        }).then(res => {
            resolve(res);
        })
    })
}

export const eatWakes = (params: any) => {
    return new Promise((resolve) => {
        request({
            url: API_EAT_WAKES, method: 'GET', data: { ...params }
        }).then(res => {
            resolve(res);
        })
    })
}

export const getClocks = () => {
    return new Promise((resolve, reject) => {
        request({
            url: API_FAST_CLOCKS, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        }).catch(e => {
            reject(e)
        })
    })
}

export const getClockRecords = (params: any) => {
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_RECORDS, method: 'GET', data: { ...params }
        }).then(res => {
            resolve(res);
        })
    })

}

export const recordCheck = (params: Record<string, any>) => {
    if (params.real_check_time) {
        var date = new Date((params as any).real_check_time)
        var month = date.getMonth() + 1
        var day = date.getDate()

        params.real_check_date = date.getFullYear() + (month < 10 ? '0' + month : month + '') + (day < 10 ? '0' + day : day + '')
    }
    //如果是系统错误，不用toast而用alert
    params.showAlert = true
    return new Promise((resolve, reject) => {
        request({
            url: API_FAST_CLOCKS + `?scenario=${global.scenario}`, method: 'POST', data: { ...params }
        }).then(res => {
            resolve(res);
        }).catch(e => {
            if (global.postBtnUpdateStatus)
                global.postBtnUpdateStatus('idle')
            reject(e)
        })
    })
}

export const updateRecord = (params: any, id: any) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_CLOCK_RECORD_UPDATE + '/' + id, method: 'POST', data: { ...params }
        }).then(res => {
            resolve(res);
        }).catch(e => {
            reject(e)
        })
    })
}

export const delRecord = (id: string,params?:any) => {
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_RECORDS + '/' + id, method: 'DELETE', data: {...params}
        }).then(res => {
            resolve(res);
        })
    })
}

export const clearTimeRecords = () => {
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_RECORDS, method: 'DELETE', data: {}
        }).then(res => {
            resolve(res);
        })
    })


}

export const getStreaks = (params: any) => {
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_STREAKS, method: 'GET', data: { ...params }
        }).then(res => {
            resolve(res);
        })
    })
}

export const fastWindow = () => {
    return new Promise((resolve) => {
        request({
            url: API_FAST_WINDOW, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        })
    })
}

export const sleepWindow = () => {
    return new Promise((resolve) => {
        request({
            url: API_SLEEP_WINDOW, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        })
    })
}

export const setSchedule = (params: any) => {
    return new Promise((resolve) => {
        request({
            url: API_SET_SCHEDULE, method: 'POST', data: { ...params }
        }).then(res => {
            resolve(res);
        })
    })
}