import {
  Button,
  closeToast,
  Icon,
  Loading,
  Popup,
  showToast,
  Slider,
  Swipe,
  SwipeInstance,
  SwipeItem,
  Tab,
  Tabs
} from 'vant'
import {
  defineComponent,
  onMounted,
  reactive,
  nextTick,
  onUnmounted,
  ref,
  watch,
  Transition,
  computed
} from 'vue'
import iconBack from './image/back.svg'
import styles from './index.module.less'
import 'plyr/dist/plyr.css'
import request from '@/helpers/request'
import { state } from '@/state'
import { useRoute } from 'vue-router'
import { postMessage, promisefiyPostMessage } from '@/helpers/native-message'
import MusicScore from './component/musicScore'
import iconMenu from './image/icon-menu.svg'
import iconDian from './image/icon-dian.svg'
import iconPoint from './image/icon-point.svg'
import iconLoop from './image/icon-loop.svg'
import iconLoopActive from './image/icon-loop-active.svg'
import iconplay from './image/icon-play.svg'
import iconpause from './image/icon-pause.svg'
import iconUp from './image/icon-up.svg'
import iconDown from './image/icon-down.svg'
import Points from './component/points'
import { browser, getSecondRPM } from '@/helpers/utils'
import { Vue3Lottie } from 'vue3-lottie'
import playLoadData from './datas/data.json'

export default defineComponent({
  name: 'CoursewarePlay',
  setup() {
    const handleInit = (type = 0) => {
      // 横屏
      postMessage({
        api: 'setRequestedOrientation',
        content: {
          orientation: type
        }
      })
      // 头,包括返回箭头
      postMessage({
        api: 'setTitleBarVisibility',
        content: {
          status: type
        }
      })
      // 安卓的状态栏
      postMessage({
        api: 'setStatusBarVisibility',
        content: {
          isVisibility: type
        }
      })
    }
    handleInit()
    onUnmounted(() => {
      handleInit(1)
      window.removeEventListener('message', iframeHandle)
    })
    /** 设置播放容器 16:9 */
    const parentContainer = reactive({
      width: '100vw'
    })
    const setContainer = () => {
      let width = screen.height * (16 / 9)
      console.log('计算的屏幕宽度', width, screen.width)
      if (width > screen.width) {
        return
      }
      parentContainer.width = width + 'px'
      console.log('设置宽度', parentContainer.width)
    }

    const route = useRoute()
    const headeRef = ref()
    const data = reactive({
      detail: null,
      knowledgePointList: [] as any,
      itemList: [] as any,
      showHead: true,
      isCourse: false
    })
    const activeData = reactive({
      nowTime: 0,
      model: true, // 遮罩
      videoBtns: true, // 视频
      currentTime: 0,
      duration: 0,
      timer: null as any,
      item: null as any
    })
    // 获取缓存路径
    const getCacheFilePath = async (material: any) => {
      const res = await promisefiyPostMessage({
        api: 'getCourseFilePath',
        content: {
          url: material.content,
          localPath: '',
          materialId: material.materialId,
          updateTime: material.updateTime,
          type: material.type // SONG VIDEO IMAGE
        }
      })
      // console.log('缓存路径返回', res)
      return res
    }
    // 获取当前课程是否签退
    const getCourseSchedule = async () => {
      if (!route.query.courseId) return
      try {
        const res = await request.get(
          `${state.platformApi}/courseSchedule/detail/${route.query.courseId}`
        )
        if (res?.data) {
          data.isCourse = res.data.status === 'ING' ? true : false
        }
      } catch (e) {
        console.log(e)
      }
    }
    const getItemList = async () => {
      const list: any = []
      const browserInfo = browser()
      for (let i = 0; i < data.knowledgePointList.length; i++) {
        const item = data.knowledgePointList[i]
        const itemLength = item.materialList.length - 1
        for (let j = 0; j < item.materialList.length; j++) {
          const material = item.materialList[j]
          //请求本地缓存
          if (browserInfo.isApp && ['VIDEO', 'IMG'].includes(material.type)) {
            const localData = await getCacheFilePath(material)
            if (localData?.content?.localPath) {
              material.url = material.content
              material.content = localData.content.localPath
              // console.log("🚀 ~ material", material)
            }
          }

          let videoItem = {}
          if (material.type === 'VIDEO') {
            videoItem = {
              currentTime: 0,
              duration: 0,
              progress: 0,
              paused: true,
              loop: false,
              videoEle: null,
              timer: null,
              playModel: false,
              isprepare: false,
              isDrage: false,
              muted: true // 是否静音
            }
          }
          list.push({
            ...material,
            ...videoItem,
            iframeRef: null,
            tabName: item.name,
            isLast: j === itemLength, // 当前知识点
            autoPlay: false //加载完成是否自动播放
          })
        }
      }

      let item: any = null
      if (route.query.kId) {
        item = list.find((n: any) => n.materialId == route.query.kId)
        const _firstIndex = list.findIndex((n: any) => n.materialId == route.query.kId)
        popupData.firstIndex = _firstIndex > -1 ? _firstIndex : 0
      }
      item = item ? item : list[0] || {}
      if (item) {
        popupData.tabName = item.tabName
        popupData.tabActive = item.knowledgePointId
        popupData.itemActive = item.id
        popupData.itemName = item.name
        popupData.activeIndex = popupData.firstIndex
        item.autoPlay = true
        item.muted = true
      }
      console.log('🚀 ~ list', list)
      data.itemList = list
    }
    const getDetail = async () => {
      try {
        const res: any = await request.get(
          state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}`
        )
        if (Array.isArray(res?.data)) {
          data.detail = res.data
        }
        if (Array.isArray(res?.data?.knowledgePointList)) {
          let index = 0
          data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
            if (Array.isArray(n.materialList)) {
              n.materialList = n.materialList.map((item: any) => {
                index++
                return {
                  ...item,
                  materialId: item.id,
                  id: index + ''
                }
              })
            }
            return n
          })
          getItemList()
        }
      } catch (error) {}
    }
    // ifram事件处理
    const iframeHandle = (ev: MessageEvent) => {
      // console.log(ev.data)
      if (ev.data?.api === 'headerTogge') {
        activeData.model = !activeData.model
      }
    }
    onMounted(() => {
      getDetail()
      getCourseSchedule()
      window.addEventListener('message', iframeHandle)
      window.addEventListener('resize', setContainer)
      setContainer()
    })
    // 返回
    const goback = () => {
      postMessage({ api: 'goBack' })
    }

    const swipeRef = ref<SwipeInstance>()
    const popupData = reactive({
      firstIndex: 0,
      open: false,
      activeIndex: 0,
      tabActive: '',
      tabName: '',
      itemActive: '',
      itemName: ''
    })
    // 设置当前的激活状态
    const setActiveData = (val: any, oldVal: any) => {
      handleStopVideo()
      handleStopMusicScore()
    }
    watch(() => popupData.activeIndex, setActiveData)

    // 停止所有的播放
    const handleStopVideo = () => {
      data.itemList.forEach((m: any) => {
        const item = data.itemList[popupData.activeIndex]
        if (item?.id != m.id) {
          m.autoPlay = false
          m.videoEle?.pause()
        }
      })
    }
    // 停止曲谱的播放
    const handleStopMusicScore = () => {
      data.itemList.forEach((m: any) => {
        m.iframeRef?.contentWindow?.postMessage({ api: 'setPlayState' }, '*')
      })
    }
    // 切换素材
    const toggleMaterial = () => {
      const index = data.itemList.findIndex((n: any) => n.id == popupData.itemActive)
      if (index > -1) {
        popupData.activeIndex = index
        swipeRef.value?.swipeTo(index, {
          immediate: true
        })
      }
    }
    /** 延迟收起模态框 */
    const setModelOpen = () => {
      clearTimeout(activeData.timer)
      closeToast()
      activeData.timer = setTimeout(() => {
        activeData.model = false
      }, 4000)
    }
    // 轮播切换
    const handleSwipeChange = (val: any) => {
      console.log('轮播切换')
      popupData.activeIndex = val
      const item = data.itemList[val]
      if (item) {
        popupData.tabActive = item.knowledgePointId
        popupData.itemActive = item.id
        popupData.itemName = item.name
        popupData.tabName = item.tabName
        if (item.type == 'SONG') {
          activeData.model = true
        }
        if (item.type === 'VIDEO') {
          // console.log("🚀 ~ item", item)
          // 自动播放下一个视频
          clearTimeout(activeData.timer)
          closeToast()
          item.currentTime = 0
          item.videoEle && (item.videoEle.currentTime = 0)
          nextTick(() => {
            item.autoPlay = true
            item.videoEle?.play()
          })
        }
      }
    }
    // 上一个知识点, 下一个知识点
    const handlePreAndNext = (type: string) => {
      if (type === 'up') {
        swipeRef.value?.prev()
      } else {
        swipeRef.value?.next()
      }
    }

    // 去点名,签退
    const gotoRollCall = (pageTag: string) => {
      postMessage({
        api: 'open_app_page',
        content: {
          action: 'app',
          pageTag: pageTag,
          url: '',
          params: JSON.stringify({ courseId: route.query.courseId })
        }
      })
    }

    // 双击
    const handleDbClick = (item: any) => {
      // console.log(item)
      if (item && item.type === 'VIDEO') {
        const videoEle: HTMLVideoElement = item.videoEle
        if (videoEle) {
          if (videoEle.paused) {
            closeToast()
            videoEle.play()
          } else {
            showToast('已暂停')
            videoEle.pause()
          }
        }
      }
    }

    // 暂停播放
    const handlePaused = (e: Event, m: any) => {
      e.stopPropagation()
      m.videoEle?.pause()
      m.paused = true
    }
    // 开始播放
    const handlePlay = (e: Event, m: any) => {
      e.stopPropagation()
      closeToast()
      m.videoEle?.play()
    }

    // 调整播放进度
    const handleChangeSlider = (m: any) => {
      if (m?.videoEle) {
        // console.log('进度条', m.progress)
        m.currentTime = m.duration * (m.progress / 100)
        m.videoEle.currentTime = m.currentTime
      }
    }

    //当前视频播放完
    const handleEnded = (m: any) => {
      // console.log(m)
      if (popupData.activeIndex != data.itemList.length - 1) {
        swipeRef.value?.next()
      }
    }

    return () => (
      <div class={styles.playContent}>
        <div class={styles.coursewarePlay} style={{ width: parentContainer.width }}>
          <Swipe
            style={{ height: '100%' }}
            ref={swipeRef}
            showIndicators={false}
            loop={false}
            vertical
            lazyRender={true}
            touchable={false}
            initialSwipe={popupData.firstIndex}
            onChange={handleSwipeChange}
          >
            {data.itemList.map((m: any, mIndex: number) => {
              return (
                <SwipeItem class={styles.swipeItem}>
                  <>
                    <div
                      class={styles.itemDiv}
                      onClick={() => {
                        clearTimeout(activeData.timer)
                        if (Date.now() - activeData.nowTime < 300) {
                          handleDbClick(m)
                          return
                        }
                        activeData.nowTime = Date.now()
                        activeData.timer = setTimeout(() => {
                          activeData.model = !activeData.model
                          setModelOpen()
                        }, 300)
                      }}
                    >
                      {m.type === 'VIDEO' ? (
                        <>
                          <video
                            playsinline="false"
                            muted={m.muted}
                            preload="auto"
                            class="player"
                            data-vid={m.id}
                            src={m.content}
                            loop={m.loop}
                            autoplay={m.autoPlay}
                            onLoadedmetadata={(e: Event) => {
                              const videoEle = e.target as unknown as HTMLVideoElement
                              m.currentTime = videoEle.currentTime
                              m.duration = videoEle.duration
                              m.videoEle = videoEle
                              m.isprepare = true
                            }}
                            onTimeupdate={(e: Event) => {
                              if (!m.isprepare) return
                              const videoEle = e.target as unknown as HTMLVideoElement
                              m.currentTime = videoEle.currentTime
                              m.progress = Number(
                                ((videoEle.currentTime / m.duration) * 100).toFixed(1)
                              )
                            }}
                            onPlay={() => {
                              // 播放
                              m.paused = false
                              console.log('播放')
                              setModelOpen()
                              m.muted = false
                            }}
                            onPause={() => {
                              //暂停
                              clearTimeout(activeData.timer)
                              m.paused = true
                            }}
                            onEnded={() => handleEnded(m)}
                          >
                            <source src={m.content} type="video/mp4" />
                          </video>
                          {m.muted && (
                            <div class={styles.loadWrap}>
                              <Vue3Lottie animationData={playLoadData}></Vue3Lottie>
                            </div>
                          )}
                          <Transition name="bottom">
                            {activeData.model && (
                              <div class={[styles.bottomFixedContainer]}>
                                <div class={styles.time}>
                                  <span>{getSecondRPM(m.currentTime)}</span>
                                  <span>{getSecondRPM(m.duration)}</span>
                                </div>
                                <div class={styles.slider}>
                                  <Slider
                                    onClick={() => {
                                      setModelOpen()
                                    }}
                                    style={{ display: m.isprepare ? 'block' : 'none' }}
                                    buttonSize={16}
                                    step={0.1}
                                    modelValue={m.progress}
                                    onUpdate:modelValue={(val: any) => {
                                      m.progress = val
                                      handleChangeSlider(m)
                                    }}
                                    onDragStart={(e: Event) => {
                                      // 开始拖动,暂停播放
                                      console.log('开始拖动')
                                      // 如果拖动之前,视频是播放状态,拖动完毕后继续播放
                                      if (!m.paused) {
                                        m.isDrage = true
                                      }
                                      handlePaused(e, m)
                                    }}
                                    onDragEnd={(e: Event) => {
                                      console.log('结束拖动')
                                      if (m.isDrage) {
                                        m.isDrage = false
                                        handlePlay(e, m)
                                      }
                                    }}
                                    min={0}
                                    max={100}
                                  />
                                </div>

                                <div class={styles.actions}>
                                  <div class={styles.actionBtn}>
                                    {m.isprepare ? (
                                      <>
                                        {m.paused ? (
                                          <img
                                            src={iconplay}
                                            onClick={(e: Event) => handlePlay(e, m)}
                                          />
                                        ) : (
                                          <img
                                            src={iconpause}
                                            onClick={(e: Event) => handlePaused(e, m)}
                                          />
                                        )}
                                      </>
                                    ) : (
                                      <Loading color="#fff" />
                                    )}

                                    {m.loop ? (
                                      <img
                                        src={iconLoopActive}
                                        onClick={(e: Event) => {
                                          e.stopPropagation()
                                          m.loop = false
                                        }}
                                      />
                                    ) : (
                                      <img
                                        src={iconLoop}
                                        onClick={(e: Event) => {
                                          e.stopPropagation()
                                          m.loop = true
                                        }}
                                      />
                                    )}
                                  </div>
                                  <div>{m.name}</div>
                                </div>
                              </div>
                            )}
                          </Transition>
                        </>
                      ) : m.type === 'IMG' ? (
                        <img src={m.content} />
                      ) : (
                        <MusicScore
                          data-vid={m.id}
                          music={m}
                          onSetIframe={(el: any) => {
                            m.iframeRef = el
                          }}
                        />
                      )}
                    </div>
                  </>
                </SwipeItem>
              )
            })}
          </Swipe>

          <Transition name="top">
            {activeData.model && (
              <div class={styles.headerContainer} ref={headeRef}>
                <div class={styles.backBtn} onClick={() => goback()}>
                  <Icon name={iconBack} />
                  返回
                </div>
                <div class={styles.menu}>{popupData.tabName}</div>
              </div>
            )}
          </Transition>

          <Transition name="right">
            {activeData.model && (
              <div class={styles.rightFixedBtns}>
                <div
                  class={styles.fullBtn}
                  onClick={() => {
                    clearTimeout(activeData.timer)
                    popupData.open = true
                  }}
                >
                  <img src={iconMenu} />
                  <span>知识点</span>
                </div>
                {data.isCourse && (
                  <>
                    <div
                      class={[styles.fullBtn, styles.point]}
                      onClick={() => gotoRollCall('student_roll_call')}
                    >
                      <img src={iconDian} />
                      <span>点名</span>
                    </div>
                    <div class={styles.fullBtn} onClick={() => gotoRollCall('sign_out')}>
                      <img src={iconPoint} />
                      <span>签退</span>
                    </div>
                  </>
                )}
              </div>
            )}
          </Transition>

          <Transition name="left">
            {activeData.model && (
              <div class={styles.leftFixedBtns}>
                {popupData.activeIndex != 0 && (
                  <div
                    class={[styles.fullBtn, styles.prePoint]}
                    onClick={() => handlePreAndNext('up')}
                  >
                    <img src={iconUp} />
                    <span style={{ textAlign: 'center' }}>上一个</span>
                  </div>
                )}
                {popupData.activeIndex != data.itemList.length - 1 && (
                  <div class={styles.fullBtn} onClick={() => handlePreAndNext('down')}>
                    <span style={{ textAlign: 'center' }}>下一个</span>
                    <img src={iconDown} />
                  </div>
                )}
              </div>
            )}
          </Transition>

          <Popup
            class={styles.popup}
            overlayClass={styles.overlayClass}
            position="right"
            round
            v-model:show={popupData.open}
            onClose={() => {
              const item = data.itemList[popupData.activeIndex]
              if (item?.type == 'VIDEO') {
                setModelOpen()
              }
            }}
          >
            <Points
              data={data.knowledgePointList}
              tabActive={popupData.tabActive}
              itemActive={popupData.itemActive}
              onHandleSelect={(res: any) => {
                // console.log(res)
                popupData.tabActive = res.tabActive
                popupData.itemActive = res.itemActive
                popupData.tabName = res.tabName
                popupData.open = false
                toggleMaterial()
              }}
            />
          </Popup>
        </div>
      </div>
    )
  }
})
