<template>
  <div class="video-learning">
    <SubPageHeader>
      <template #center>
        <span class="header-title">{{ bookTitle }}</span>
      </template>
    </SubPageHeader>

    <main class="video-body">
      <section class="learning-panel">
        <aside class="catalog-panel">
          <div class="catalog-header">
            <span class="catalog-title">目录</span>
            <button class="catalog-action" aria-label="目录设置">
              <span></span>
              <span></span>
              <span></span>
            </button>
          </div>

          <div class="catalog-list">
            <div v-for="unit in units" :key="unit.id" class="unit-block">
              <button class="unit-row" @click="toggleUnit(unit)">
                <span>{{ unit.name }}</span>
                <svg
                  class="unit-arrow"
                  :class="{ expanded: unit.expanded }"
                  viewBox="0 0 24 24"
                  fill="none"
                  xmlns="http://www.w3.org/2000/svg"
                >
                  <path d="M7 10l5 5 5-5" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" />
                </svg>
              </button>

              <div v-if="unit.expanded" class="lesson-list">
                <button
                  v-for="lesson in unit.lessons"
                  :key="lesson.id"
                  class="lesson-row"
                  :class="{ active: activeLessonId === lesson.id }"
                  @click="selectLesson(lesson)"
                >
                  {{ lesson.name }}
                </button>
              </div>
            </div>
          </div>
        </aside>

        <section class="content-panel">
          <div class="content-header">
            <h1>章节视频</h1>
            <div class="segment-tabs">
              <button
                v-for="tab in videoTabs"
                :key="tab.key"
                class="segment-tab"
                :class="{ active: activeVideoTab === tab.key }"
                @click="activeVideoTab = tab.key"
              >
                {{ tab.label }}
              </button>
            </div>
          </div>

          <p class="video-title">{{ selectedVideo.title }}</p>

          <div class="hero-video">
            <div class="board-pattern"></div>
            <div class="teacher-card">
              <div class="teacher-avatar"></div>
            </div>
            <button class="play-btn" aria-label="播放视频">
              <span></span>
            </button>
          </div>

          <div class="episode-section">
            <h2>选集</h2>
            <div class="episode-list">
              <button
                v-for="episode in selectedVideo.episodes"
                :key="episode.id"
                class="episode-card"
                :class="{ active: activeEpisodeId === episode.id }"
                @click="activeEpisodeId = episode.id"
              >
                <div class="episode-thumb" :class="`episode-thumb--${episode.tone}`">
                  <span v-if="episode.playing" class="mini-pause"></span>
                </div>
                <span class="episode-name">{{ episode.title }}</span>
              </button>
            </div>
          </div>
        </section>
      </section>
    </main>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import SubPageHeader from '@/components/layout/SubPageHeader.vue'

interface Lesson {
  id: number
  name: string
}

interface Unit {
  id: number
  name: string
  expanded: boolean
  lessons: Lesson[]
}

interface Episode {
  id: number
  title: string
  tone: 'dark' | 'green' | 'blue'
  playing?: boolean
}

interface VideoLesson {
  id: number
  title: string
  episodes: Episode[]
}

const route = useRoute()

const bookName = computed(() => String(route.query.name || ''))
const bookSubtitle = computed(() => String(route.query.subtitle || ''))
const bookTitle = computed(() =>
  bookName.value && bookSubtitle.value
    ? `${bookName.value}${bookSubtitle.value}`
    : bookName.value || '视频学习',
)

const units = ref<Unit[]>([
  {
    id: 1,
    name: 'Unit 1 New school, new beginning!',
    expanded: true,
    lessons: [
      { id: 101, name: 'Lesson 1 Hello!' },
      { id: 102, name: 'Lesson 2 Let me show you around!' },
      { id: 103, name: 'Lesson 3 Look at the photo!' },
    ],
  },
  {
    id: 2,
    name: 'Unit 2 Learning English is fun!',
    expanded: false, 
    lessons: [
      { id: 201, name: 'Lesson 4 I love English!' },
      { id: 202, name: 'Lesson 5 My English book' },
      { id: 203, name: 'Lesson 6 Lets sing a song!' },
    ],
  },
  {
    id: 3,
    name: 'Unit 3 All about me',
    expanded: false,
    lessons: [
      { id: 301, name: 'Lesson 7 My family' },
      { id: 302, name: 'Lesson 8 I have a pet' },
      { id: 303, name: 'Lesson 9 Review' },
    ],
  },
])

const videoTabs = [
  { key: 'chapter' as const, label: '章节' },
  { key: 'knowledge' as const, label: '知识点' },
]

const activeLessonId = ref(101)
const activeVideoTab = ref<'chapter' | 'knowledge'>('chapter')
const activeEpisodeId = ref(1)

const videoLessons: VideoLesson[] = [
  {
    id: 101,
    title: '视频标题xxxx',
    episodes: [
      { id: 1, title: '视频标题xxx...', tone: 'dark', playing: true },
      { id: 2, title: '视频标题xxx...', tone: 'green' },
      { id: 3, title: '视频标题xxx...', tone: 'green' },
      { id: 4, title: '视频标题xxx...', tone: 'blue' },
    ],
  },
  {
    id: 102,
    title: '校园介绍重点句型',
    episodes: [
      { id: 5, title: 'Let me show...', tone: 'green', playing: true },
      { id: 6, title: 'Around school...', tone: 'blue' },
      { id: 7, title: 'Practice time...', tone: 'green' },
    ],
  },
  {
    id: 103,
    title: '看图表达与单词复习',
    episodes: [
      { id: 8, title: 'Look at photo...', tone: 'blue', playing: true },
      { id: 9, title: 'Words review...', tone: 'dark' },
    ],
  },
]

const selectedVideo = computed(() =>
  videoLessons.find(video => video.id === activeLessonId.value) ?? videoLessons[0],
)

function toggleUnit(unit: Unit) {
  unit.expanded = !unit.expanded
}

function selectLesson(lesson: Lesson) {
  activeLessonId.value = lesson.id
  activeEpisodeId.value = selectedVideo.value.episodes[0]?.id ?? 0
}
</script>

<style scoped lang="scss">
.video-learning {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: #292929;
  color: #f5f5f5;
}

.header-title {
  font-size: 30px;
  color: #f5f5f5;
}

:deep(.back-icon path) {
  stroke: #fff;
}

.video-body {
  flex: 1;
  min-height: 0;
  padding: 0 64px 32px;
  overflow: hidden;
}

.learning-panel {
  width: 100%;
  max-width: 1792px;
  height: 100%;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 690px minmax(0, 1fr);
  overflow: hidden;
  border-radius: 22px;
  background: #2e2e2f;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.22);
}

.catalog-panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #454545;
}

.catalog-header {
  height: 86px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.catalog-title {
  font-size: 28px;
  color: rgba(255, 255, 255, 0.82);
}

.catalog-action {
  width: 34px;
  height: 34px;
  display: grid;
  gap: 4px;
  place-content: center;
  border: 0;
  background: transparent;
  cursor: pointer;

  span {
    width: 18px;
    height: 2px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.55);
  }
}

.catalog-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: none;

  &::-webkit-scrollbar {
    display: none;
  }
}

.unit-row,
.lesson-row {
  width: 100%;
  border: 0;
  text-align: left;
  color: rgba(255, 255, 255, 0.86);
  cursor: pointer;
}

.unit-row {
  min-height: 78px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: transparent;
  font-size: 25px;
  font-weight: 600;
}

.unit-arrow {
  width: 22px;
  height: 22px;
  color: rgba(255, 255, 255, 0.5);
  flex-shrink: 0;
  transform: rotate(-90deg);
  transition: transform 0.2s;

  &.expanded {
    transform: rotate(0);
  }
}

.lesson-list {
  background: rgba(255, 255, 255, 0.06);
}

.lesson-row {
  position: relative;
  height: 78px;
  padding: 0 34px 0 42px;
  background: transparent;
  font-size: 24px;
  color: rgba(255, 255, 255, 0.72);
  transition: color 0.15s, background 0.15s;

  &.active {
    color: #fff;
    background: rgba(0, 0, 0, 0.16);

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      width: 6px;
      border-radius: 0 4px 4px 0;
      background: #2f80ff;
    }
  }
}

.content-panel {
  min-width: 0;
  min-height: 0;
  padding: 34px 34px 40px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #303030;
}

.content-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;

  h1 {
    font-size: 28px;
    font-weight: 600;
    color: #fff;
  }
}

.segment-tabs {
  display: flex;
  align-items: center;
  padding: 5px;
  border-radius: 99px;
  background: #616161;
}

.segment-tab {
  min-width: 78px;
  height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 99px;
  background: transparent;
  color: rgba(255, 255, 255, 0.54);
  font-size: 20px;
  cursor: pointer;

  &.active {
    background: #fff;
    color: #252525;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
  }
}

.video-title {
  margin-top: 36px;
  font-size: 24px;
  color: rgba(255, 255, 255, 0.72);
}

.hero-video {
  position: relative;
  margin-top: 28px;
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: 572px;
  min-height: 360px;
  border-radius: 6px;
  overflow: hidden;
  background:
    radial-gradient(circle at 70% 30%, rgba(255, 255, 255, 0.18), transparent 22%),
    linear-gradient(155deg, #0d5a50 0%, #176f61 48%, #0c4a43 100%);
}

.board-pattern {
  position: absolute;
  inset: 0;
  opacity: 0.58;
  background-image:
    linear-gradient(25deg, transparent 0 45%, rgba(255, 255, 255, 0.45) 46%, transparent 48%),
    linear-gradient(115deg, transparent 0 56%, rgba(255, 255, 255, 0.28) 57%, transparent 59%);
  background-size: 210px 140px, 260px 160px;
}

.teacher-card {
  position: absolute;
  right: 12%;
  bottom: 0;
  width: 230px;
  height: 78%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.teacher-avatar {
  width: 165px;
  height: 270px;
  border-radius: 90px 90px 18px 18px;
  background:
    radial-gradient(circle at 50% 48px, #ffe2c5 0 42px, transparent 43px),
    linear-gradient(#f4f1e9 0 92px, #6c6f75 93px 100%);
  box-shadow: 0 0 0 12px rgba(255, 255, 255, 0.06);
}

.play-btn {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 82px;
  height: 82px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.34);
  transform: translate(-50%, -50%);
  cursor: pointer;

  span {
    width: 0;
    height: 0;
    margin-left: 6px;
    border-top: 17px solid transparent;
    border-bottom: 17px solid transparent;
    border-left: 25px solid #fff;
  }
}

.episode-section {
  margin-top: 36px;

  h2 {
    margin-bottom: 22px;
    font-size: 24px;
    font-weight: 500;
    color: #fff;
  }
}

.episode-list {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 24px;
}

.episode-card {
  position: relative;
  min-width: 0;
  border: 0;
  border-radius: 5px;
  overflow: hidden;
  background: transparent;
  cursor: pointer;
  outline: 2px solid transparent;

  &.active {
    outline-color: rgba(255, 255, 255, 0.42);
  }
}

.episode-thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 7;
  background:
    linear-gradient(155deg, rgba(5, 48, 43, 0.85), rgba(19, 108, 94, 0.88)),
    repeating-linear-gradient(25deg, transparent 0 34px, rgba(255, 255, 255, 0.22) 35px 37px);

  &--green {
    background:
      linear-gradient(155deg, rgba(18, 93, 80, 0.72), rgba(30, 120, 100, 0.88)),
      repeating-linear-gradient(25deg, transparent 0 34px, rgba(255, 255, 255, 0.22) 35px 37px);
  }

  &--blue {
    background:
      linear-gradient(155deg, rgba(17, 75, 105, 0.72), rgba(45, 115, 145, 0.86)),
      repeating-linear-gradient(25deg, transparent 0 34px, rgba(255, 255, 255, 0.2) 35px 37px);
  }
}

.mini-pause {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28px;
  height: 28px;
  transform: translate(-50%, -50%);

  &::before,
  &::after {
    content: '';
    position: absolute;
    top: 3px;
    width: 6px;
    height: 22px;
    border-radius: 2px;
    background: #fff;
  }

  &::before {
    left: 6px;
  }

  &::after {
    right: 6px;
  }
}

.episode-name {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 22px 12px 8px;
  color: #fff;
  font-size: 17px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.72));
}
</style>
