import React, { useState, useEffect, useRef } from 'react'
import EventName from '../event/eventName'
import mpegts from '@cloud-app-dev/mpegts.js'
import { useLatest } from 'ahooks'


function ErrorEvent({ event, api, errorReloadTimer, flv, hls, changePlayIndex, isHistory, playIndex }) {
  const [errorTimer, setErrorTime] = useState(0)
  const errorInfo = useRef(null)
  const reloadTimer = useRef(null)
  const waitingTimer = useRef(null)

  useEffect(() => {
    const errorHandle = (...args) => {
      if (args[2] && args[2].msg && args[2].msg.includes('Unsupported audio')) {
        return
      }

      if (args[1] && args[1].details && (
        args[1].details.includes("bufferStalledError") ||
        args[1].details.includes("bufferNudgeOnStall") ||
        args[1].details.includes("bufferSeekOverHole") ||
        args[1].details.includes("bufferAddCodecError")
      )) {
        return
      }

      console.error(...args)
      errorInfo.current = args
      setErrorTime(errorTimer + 1)
    }


    const videoWaiting = (...args) => {
      // waitingTimer.current && clearTimeout(waitingTimer.current)
      // waitingTimer.current = setTimeout(() => {
      //   console.log('video,waiting时间超过3秒,播放下个片段');
      //   changePlayIndex(playIndex + 1)
      // }, 3000)
    }

    const errorInfoHandle = (...args) => {
      console.log(...args)

    }

    const reloadSuccess = (e) => {
      waitingTimer.current && clearTimeout(waitingTimer.current)
      if (errorTimer > 0) {
        console.warn('视频重连成功！')
        event.emit(EventName.RELOAD_SUCCESS)
        api.success()
        clearErrorTimer()
      }
    }
    const clearErrorTimer = () => setErrorTime(0)
    try {
      if (flv) {
        flv.on('error', errorHandle)
        // for (const key in mpegts.Events) {
        //   if (Object.hasOwnProperty.call(mpegts.Events, key)) {
        //     const element = mpegts.Events[key];
        //     console.log(element);
        //     element === 'error' ? flv.on('error', errorHandle) : flv.on(element, (e) => errorInfoHandle(element, e))

        //   }
        // }
      }
      if (hls) {
        hls.on('hlsError', errorHandle)
      }
    } catch (e) {
      //
    }

    if (isHistory) {
      //历史视频切换播放索引时清除错误次数
      event.on(EventName.CHANGE_PLAY_INDEX, clearErrorTimer)
      //历史视频主动清除错误次数
      event.on(EventName.CLEAR_ERROR_TIMER, clearErrorTimer)
    }
    event.addEventListener('error', errorHandle, false)
    event.addEventListener('waiting', videoWaiting, false)
    //获取video状态清除错误状态
    event.addEventListener('canplay', reloadSuccess, false)

    return () => {
      try {
        if (flv) {
          flv.off('error', errorHandle)
        }
        if (hls) {
          hls.off('hlsError', errorHandle)
        }
      } catch (e) {
        console.warn(e)
      }
      if (isHistory) {
        event.off(EventName.CHANGE_PLAY_INDEX, clearErrorTimer)
        event.off(EventName.CLEAR_ERROR_TIMER, clearErrorTimer)
      }
      event.removeEventListener('error', errorHandle, false)
      event.removeEventListener('canplay', reloadSuccess, false)
      event.removeEventListener('waiting', videoWaiting, false)
    }
  }, [event, flv, hls, errorTimer, isHistory, api])

  useEffect(() => {
    if (errorTimer === 0) {
      return
    }
    if (errorTimer > errorReloadTimer) {
      isHistory ? changePlayIndex(playIndex + 1) : event.emit(EventName.RELOAD_FAIL)
      api.unload()
      return
    }

    console.warn(`视频播放出错，正在进行重连${errorTimer}`)
    reloadTimer.current = setTimeout(() => {
      errorInfo.current ? event.emit(EventName.ERROR_RELOAD, errorTimer, ...errorInfo.current) : event.emit(EventName.ERROR_RELOAD, errorTimer)
      api.reload(true)
    }, 2 * 1000)

    return () => {
      clearTimeout(reloadTimer.current)
    }
  }, [errorTimer, api, event, flv, hls])

  return <></>
}

export default ErrorEvent
