import { Component, PropsWithChildren, useEffect } from 'react'
import { View, Text, Button, Input } from '@tarojs/components'
import './index/index.scss'
import Taro, { useDidShow, useReady, useRouter } from '@tarojs/taro'
// import ComponentA from './component'
import Rings from '@components/Rings';
import SingleSelect from '@components/SingleSelect';
import { useDispatch, useSelector } from 'react-redux';
import { getInfoSuccess } from '@/store/user';
import { wxPubFollow } from '@/services/permission';
import { staticResources } from '@/services/common';
import { use } from 'i18next';
import Clocks from '@/features/trackTimeDuration/components/Clock';
import Console from '@/features/trackTimeDuration/components/Console';
import More from '@/features/trackTimeDuration/components/More';
import { getChecks } from '@/services/trackTimeDuration';
import { setScenario } from '@/store/scenario';

import { setSpecifiedStatus, setSpecifiedState,machine } from '@/store/trackTimeMachine';
import trackTimeService from '@/store/trackTimeMachine';
// import TabBar from '../../components/Tabbar';

export default function IndexPage() {
  const dispatch = useDispatch();
  const array: any[] = []
  const user = useSelector((state: any) => state.user);
  const permission = useSelector((state: any) => state.permission);
  const common = useSelector((state: any) => state.common);
  array.push('ffff');
  array.push(<Text>ffff</Text>)
  // array.push(<Rings radius={50}/>)




  useReady(async () => {
    const userData = await getStorage('userData');
    if (userData) {
      dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
      // return JSON.parse(userData as string) as UserState;
      setTimeout(() => {
        checkWXPubFollow()
        getCheckData()
      }, 200)
    }
  })

  useDidShow(() => {
    if (user.isLogin)
      getCheckData();
  })

  function getCheckData() {
    getChecks().then(res => {
      if ((res as any).scenario) {
        dispatch(setScenario((res as any).scenario));
        global.scenario = (res as any).scenario;
        // console.log((res as any).scenario.name + ' ' + (res as any).status);
        // trackTimeService.send({type:(res as any).scenario.name});
        // setSpecifiedStatus('FAST_SLEEP', 'ON_GOING');

        // 初始化状态机
        // const initialState = machine.initialState;

        // 设置指定状态为 C->3
        // const nextState = machine.transition(initialState, 'SET_SPECIFIED_STATE');
        // const specifiedState = setSpecifiedState(nextState, 'FAST_SLEEP');
        // setSpecifiedState();
        // console.log('name:'+(res as any).scenario.name)
        // machine.reset();
        trackTimeService.send({type:'RESET'});
        
        trackTimeService.send({type:(res as any).scenario.name});

        const currentState = trackTimeService.getSnapshot();
        let json = {};
        var key = (res as any).scenario.name
        json[key] = (res as any).status
        currentState.value = json;
        // debugger
        machine.context.currentStatus =  `${key}.${(res as any).status}`;//'mixed.ON_GOING2'
        // debugger
        // trackTimeService.send('APPLE');
        // machine.transition(`${key}.${(res as any).status}`,'' as any);
        
        // trackTimeService.send({type:'START_FAST'});
        // trackTimeService.send({type:'START_SLEEP'});
        // trackTimeService.send({type:'END_SLEEP'});
        // trackTimeService.send({type:'END_SLEEP'});
        // trackTimeService.send({type:'START_FAST'});
        // trackTimeService.send({type:'END_FAST'});
        // trackTimeService.send({type:'START_SLEEP'});
        
        // console.log(currentState.value);
        // debugger
        // trackTimeService.send('setCurrentStatus', {status:(res as any).status});
        // setCurrentStatus((res as any).status);
        // trackTimeService.send({type:'START_FAST'});
      }
    })
  }

  function checkWXPubFollow() {
    wxPubFollow().then(res => {
      console.log(res);
    })
  }

  function followWxPub() {
    const resource = common.resources.filter((item: any) => {
      return item.code == 'follow_wx_pub'
    })

    Taro.showModal({
      title: '提示',
      content: '关注公众号后可接收提醒\n点击确定，前往关注',
      showCancel: true,
      success(result) {
        if (result.confirm) {
          Taro.navigateTo({
            url: '/pages/H5?title=fast16cc 关注服务号&url=' + resource[0].url,
          })
        }
      },
    })
  }


  async function getStorage(key: string) {
    try {
      const res = await Taro.getStorage({ key });
      return res.data;
    } catch {
      return '';
    }
  }

  function goDetail() {
    Taro.navigateTo({
      // url: '/pages/index/detail'
      //src/moduleA/pages/third.tsx
      url: '../../moduleA/pages/third'
    })
  }

  function login() {
    if (user.isLogin) {
      Taro.navigateTo({
        url: '/pages/ChooseScenario'
      })
    }
    else {
      Taro.navigateTo({
        url: '/pages/ChooseAuth'
      })
    }

  }
  return (
    <View style={{ backgroundColor: '#fff', flex: 1, flexDirection: 'column', display: 'flex' }}>
      {
        permission.wxPubFollow == false ? <Text onClick={() => followWxPub()}>去关注公众号</Text>:<Text>已关注</Text>
      }
      {/* <ComponentA value="ABBBB" /> */}
      <Button onClick={() => goDetail()}>go detail</Button>
      {/* <SingleSelect items={array}></SingleSelect> */}

      {/* <Input placeholder="请输入用户名1" style={{ textAlign: 'left', backgroundColor: 'pink' }} /> */}
      <Text className='login' onClick={() => login()}>sss</Text>
      {/* <TabBar /> */}
      <Clocks />
      <Console />
      <More />

    </View>
  )
}