import { View, Text } from "@tarojs/components";
import Taro from "@tarojs/taro";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
import trackTimeService, { machine } from "@/store/trackTimeMachine"
import { recordCheck } from "@/services/trackTimeDuration";

export default function Component() {
    const { t } = useTranslation()
    const scenario = useSelector((state: any) => state.scenario);

    useEffect(() => {
        var value = trackTimeService.getSnapshot().value
    }, [machine.context.currentStatus])

    useEffect(() => {
        trackTimeService.onTransition(state => {
            var value = trackTimeService.getSnapshot().value
        });
    }, []);

    function waitActionSheet() {
        Taro.showActionSheet({
            itemList: [t('feature.track_time_duration.action_sheet.change_schedule'), t('feature.track_time_duration.action_sheet.switch_scenario')]
        })
            .then(res => {
                console.log(res.tapIndex)
                switch (res.tapIndex) {
                    case 0:
                        Taro.navigateTo({
                            url: '/pages/SetSchedule'
                        })
                        break;
                    case 1:
                        Taro.navigateTo({
                            url: '/pages/ChooseScenario'
                        })
                        break;
                }
            })
            .catch(err => {
                console.log(err.errMsg)
            })
    }

    function endFastActionSheet() {
        Taro.showActionSheet({
            itemList: [t('feature.track_time_duration.action_sheet.end_fast')]
        })
            .then(res => {
                console.log(res.tapIndex)
                switch (res.tapIndex) {
                    case 0:
                        {

                            const start_time = new Date().getTime();
                            // const duration = 8 * 3600 * 1000;
                            const extra = {
                                set_time: start_time - 20 * 1000,
                                confirm_time: start_time + 50 * 1000,
                            }
                            recordCheck({
                                action: 'FAST_END',
                                real_check_time: start_time,
                                extra: extra
                            }).then(res => {
                                trackTimeService.send({ type: 'END_FAST' });
                                console.log(res);
                                trackTimeService.send({ type: 'RESET' });

                                trackTimeService.send({ type: global.scenario.name });
                            });
                        }
                        break;
                }
            })
            .catch(err => {
                console.log(err.errMsg)
            })
    }


    function ongoingActionSheet() {
        Taro.showActionSheet({
            itemList: [t('feature.track_time_duration.action_sheet.give_up'), t('feature.track_time_duration.action_sheet.switch_scenario')]
        })
            .then(res => {
                console.log(res.tapIndex)
            })
            .catch(err => {
                console.log(err.errMsg)
            })
    }

    function checkActionSheetData() {
        var state = trackTimeService.getSnapshot().value
        debugger
        if ((state as any).FAST_SLEEP === 'WAIT_FOR_START' ||
            (state as any).FAST === 'WAIT_FOR_START' ||
            (state as any).SLEEP === 'WAIT_FOR_START') {
            waitActionSheet()
        }
        else if ((state as any).FAST_SLEEP === 'ONGOING1' ||
            (state as any).FAST_SLEEP === 'ONGOING2') {
            endFastActionSheet()
        }
        else if ((state as any).FAST_SLEEP === 'ONGOING3') {
            Taro.showToast({
                title: '暂无更多操作',
                icon: 'none',
                duration: 2000
            })
        }
    }

    return (
        <View>
            <Text onClick={checkActionSheetData}>More Component</Text>
        </View>
    )
}