import React, {useContext, useEffect, useState} from "react";
import intl from "react-intl-universal";
import MainP from "../MainP/MainP";
import MainE from "../MainE/MainE";
import MainQ from "../MainQ/MainQ";
import TabPanel from "../Component/TabPanel";
import SwitchOnOff from "../Component/SwitchOnOff";
import {useAppSelector} from "../../store/hooks";
import {selectSwitchState} from "../../store/ChangeSwitchState";

function SwitchLine() {
  const [PageIndex, setPageIndex] = useState(1)
  const state = useAppSelector(selectSwitchState)
  useEffect(() => {
      let timerId: NodeJS.Timer;

      function StartSwitch() {
        timerId = setInterval(() => {
          if (PageIndex < 3) {
            setPageIndex(PageIndex + 1)
          } else {
            setPageIndex(1)
          }
        }, 30000)
      }

      function StopSwitch() {
        clearInterval(timerId)
      }

      if (state) {
        StartSwitch()
      } else {
        StopSwitch()
      }

      window.dispatchEvent(new Event('resize'))
      return () => {
        clearInterval(timerId)
      }
    }, [PageIndex, state]
  )
  return (
    <div>
      <SwitchOnOff/>
      <TabPanel index={1} value={PageIndex}><MainP/></TabPanel>
      <TabPanel index={2} value={PageIndex}><MainQ/></TabPanel>
      <TabPanel index={3} value={PageIndex}><MainE/></TabPanel>
    </div>
  )
}

export default SwitchLine;