<script setup lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'
import video_categories from './english_basic_categories.json'
import { $XXL } from '@/utils'

defineOptions({ name: 'EnglishBasic' })

// 分类视频数据
const videoCategories = ref(video_categories)

// 当前选中的视频ID
const activeVideoId = ref('1')

// 折叠面板激活的分类ID
const activeCollapseNames = ref(['category_1'])

// 当前播放的视频信息
const activePlay = reactive({
  id: '1',
  name: video_categories[0].children[0].name,
  url: video_categories[0].children[0].url,
  isPlay: false,
  isEnded: false,
})

// 视频播放器引用
const videoRef = ref<HTMLVideoElement>()

// 处理视频选择
function handleVideoSelect(item: any) {
  if (activePlay.id !== item.id) {
    if (activePlay.isPlay && videoRef.value) {
      videoRef.value.pause()
    }

    // 切换播放视频
    setTimeout(() => {
      activePlay.id = item.id
      activePlay.url = item.url
      activePlay.name = item.name
      activeVideoId.value = item.id

      nextTick(() => {
        if (videoRef.value) {
          videoRef.value.play()
          // 如果需要跳转到特定时间点，可以使用以下代码
          // const { playDuration } = item
          // if (Number(playDuration) > 0) {
          //     videoRef.value.currentTime = Number(playDuration)
          // }
        }
      })
    }, 30)
  }
}

// 视频事件处理函数
function handleVideoEnded() {
  activePlay.isPlay = false
  activePlay.isEnded = true

  // 自动播放下一个视频
  // 在二级结构中查找当前视频和下一个视频
  let foundCurrentVideo = false
  let nextVideo = null

  // 遍历所有分类查找当前视频和下一个视频
  for (let i = 0; i < videoCategories.value.length; i++) {
    const category = videoCategories.value[i]
    const videos = category.children

    // 在当前分类中查找当前视频的索引
    const currentIndex = videos.findIndex(item => item.id === activePlay.id)

    if (currentIndex !== -1) {
      foundCurrentVideo = true

      // 如果当前分类中还有下一个视频
      if (currentIndex < videos.length - 1) {
        nextVideo = videos[currentIndex + 1]
      }
      // 如果当前分类是最后一个视频，查找下一个分类的第一个视频
      else if (i < videoCategories.value.length - 1) {
        nextVideo = videoCategories.value[i + 1].children[0]
      }
      // 如果是最后一个分类的最后一个视频，循环到第一个分类的第一个视频
      else {
        nextVideo = videoCategories.value[0].children[0]
      }

      break
    }
  }

  // 如果找到了下一个视频，播放它
  if (nextVideo) {
    handleVideoSelect(nextVideo)
  }
}

function handleVideoPause() {
  activePlay.isPlay = false
}

function handleVideoPlay() {
  activePlay.isPlay = true
  activePlay.isEnded = false
}

onMounted(() => {
  // 初始化第一个视频
  if (videoCategories.value.length > 0 && videoCategories.value[0].children.length > 0) {
    const firstVideo = videoCategories.value[0].children[0]
    activePlay.id = firstVideo.id
    activePlay.url = firstVideo.url
    activePlay.name = firstVideo.name
    activeVideoId.value = firstVideo.id
  }
  // 屏幕常亮
  $XXL.setScreenOn(1)
})
</script>

<template>
  <div class="english-basic">
    <div class="top">
      <BackBar leftText="英语零基础" />
    </div>
    <div class="main">
      <div class="content">
        <div class="course-list-container">
          <el-scrollbar>
            <div class="course-list">
              <el-collapse v-model="activeCollapseNames">
                <el-collapse-item
                  v-for="category in videoCategories"
                  :key="category.id"
                  :title="category.name"
                  :name="category.id"
                >
                  <div
                    v-for="item in category.children"
                    :key="item.id"
                    class="course-item"
                    :class="{ active: activeVideoId === item.id }"
                    @click="handleVideoSelect(item)"
                  >
                    <div class="course-title">{{ item.name }}</div>
                  </div>
                </el-collapse-item>
              </el-collapse>
            </div>
          </el-scrollbar>
        </div>
        <div class="video-container">
          <div class="video-title">{{ activePlay.name }}</div>
          <div class="video-player">
            <video
              ref="videoRef"
              :src="activePlay.url"
              class="video-element"
              controls
              preload="auto"
              @ended="handleVideoEnded"
              @pause="handleVideoPause"
              @play="handleVideoPlay"
            ></video>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.english-basic {
  // @include main_bg;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;

  .top {
    position: absolute;
    top: 1vh;
    left: 24px;
    z-index: 1;
  }

  .main {
    height: 100%;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    // padding: 120px 64px 64px;
    // box-sizing: border-box;

    .content {
      width: 100%;
      height: 100%;
      display: flex;
      background: rgba(255, 255, 255, 0.6);
      border-radius: 1.67vh;
      overflow: hidden;
      // box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);

      .course-list-container {
        padding-top: 6.67vh;
        width: 480px;
        height: 100%;
        background: linear-gradient(180deg, #fafafa 0%, #f9f5ff 100%);
        border-right: 0.08vh solid rgba(0, 0, 0, 0.05);
        overflow-y: auto;

        .course-list {
          padding: 1.67vh 0;

          :deep(.el-collapse) {
            border: none;

            .el-collapse-item__header {
              font-size: 2.67vh;
              font-weight: bold;
              color: $font-color;
              height: 8.33vh;
              line-height: 8.33vh;
              padding: 0 0 0 40px;
              background-color: #f5f7fa;
              border-bottom: 0.08vh solid #ebeef5;
            }

            .el-collapse-item__content {
              padding: 0;
            }
          }

          .course-item {
            padding: 1.33vh 20px 1.33vh 40px;
            cursor: pointer;
            transition: all 0.3s ease;
            border-left: 0.33vh solid transparent;

            &:hover {
              background-color: rgba(0, 0, 0, 0.03);
            }

            &.active {
              background-color: rgb(147, 242, 141);
              border-left: 0.33vh solid rgb(33, 204, 24);

              .course-title {
                color: #000;
              }
            }

            .course-title {
              padding-left: 15px;
              font-size: 2.33vh;
              color: $font-color;
              font-weight: 500;
            }
          }
        }
      }

      .video-container {
        flex: 1;
        display: flex;
        flex-direction: column;
        padding: 2vh;

        .video-title {
          font-size: 2.67vh;
          font-weight: bold;
          color: $font-color;
          margin-bottom: 2vh;
          text-align: center;
        }

        .video-player {
          flex: 1;
          border-radius: 1.33vh;
          overflow: hidden;
          display: flex;
          justify-content: center;
          align-items: center;
          background-color: #000;

          .video-element {
            width: 100%;
            height: 100%;
            max-height: 100%;
            object-fit: contain;
          }
        }
      }
    }
  }
}
</style>
