<template>
  <!-- 画中画：默认左上角；仅在有直播或轮询等待时占位，最小化后显示小按钮 -->
  <div v-show="surfaceVisible" class="live-pip-root" :class="{ 'live-pip-root--mini': minimized }">
    <div v-if="!minimized" class="live-pip">
      <div class="live-pip-bar">
        <span class="live-pip-badge">LIVE</span>
        <span class="live-pip-hint">{{ hint }}</span>
        <div class="live-pip-bar-actions">
          <button type="button" class="live-pip-iconbtn" title="小窗收起" @click="minimized = true">─</button>
          <router-link to="/live" class="live-pip-link" title="全屏页">⛶</router-link>
          <button type="button" class="live-pip-iconbtn" title="隐藏（本场直播仍可在直播页观看）" @click="hideForVisit">×</button>
        </div>
      </div>
      <video
        ref="videoRef"
        class="live-pip-video"
        playsinline
        autoplay
        muted
      ></video>
    </div>
    <button
      v-else
      type="button"
      class="live-pip-fab"
      title="展开直播小窗"
      @click="minimized = false"
    >
      <span class="live-pip-fab-dot"></span>
      直播
    </button>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue'
import { useRoute } from 'vue-router'
import { startViewing } from '../utils/liveWebRTC'

const route = useRoute()
const videoRef = ref(null)
const hint = ref('')
const minimized = ref(false)
/** 用户点 × 隐藏画中画，直至本场结束或离开首页 */
const userHidden = ref(false)
let session = null

const onLivePage = computed(() => route.path === '/live' || route.path.startsWith('/live/'))

const surfaceVisible = computed(() => {
  if (onLivePage.value) return false
  if (userHidden.value) return false
  return showSurface.value
})

const showSurface = ref(false)

onMounted(async () => {
  await nextTick()
  session = startViewing(videoRef.value, {
    muted: true,
    onStatus: (s) => {
      hint.value = s
    },
    onLive: () => {
      showSurface.value = true
      minimized.value = false
    },
    onEnded: () => {
      userHidden.value = false
      minimized.value = false
      showSurface.value = false
    }
  })
})

watch(
  () => route.path,
  () => {
    if (!onLivePage.value) return
    userHidden.value = false
  }
)

function hideForVisit() {
  userHidden.value = true
  minimized.value = false
}

onUnmounted(() => {
  session?.stop()
})
</script>

<style scoped>
.live-pip-root {
  position: fixed;
  top: 72px;
  left: 12px;
  z-index: 95;
  max-width: min(240px, 42vw);
  pointer-events: auto;
}
.live-pip-root--mini {
  max-width: none;
}
.live-pip {
  border-radius: 12px;
  overflow: hidden;
  background: rgba(6, 8, 18, 0.92);
  border: 1px solid rgba(0, 212, 255, 0.35);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(10px);
}
.live-pip-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: linear-gradient(90deg, rgba(0, 212, 255, 0.12), rgba(255, 45, 149, 0.08));
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  font-size: 11px;
}
.live-pip-badge {
  flex-shrink: 0;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 800;
  letter-spacing: 0.06em;
  background: linear-gradient(135deg, #ff2d95, #00d4ff);
  color: #0a0a12;
}
.live-pip-hint {
  flex: 1;
  min-width: 0;
  color: rgba(255, 255, 255, 0.55);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.live-pip-bar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
.live-pip-iconbtn,
.live-pip-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.35);
  color: rgba(255, 255, 255, 0.75);
  cursor: pointer;
  text-decoration: none;
  font-size: 13px;
  line-height: 1;
}
.live-pip-iconbtn:hover,
.live-pip-link:hover {
  color: #00d4ff;
  background: rgba(0, 212, 255, 0.12);
}
.live-pip-video {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  vertical-align: top;
  background: #000;
}
.live-pip-fab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(0, 212, 255, 0.4);
  background: rgba(10, 14, 28, 0.95);
  color: #00d4ff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.live-pip-fab-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff2d95;
  animation: live-pip-pulse 1.2s ease-in-out infinite;
}
@keyframes live-pip-pulse {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
    transform: scale(0.85);
  }
}
@media (max-width: 600px) {
  .live-pip-root {
    top: 64px;
    left: 8px;
    max-width: min(200px, 48vw);
  }
}
</style>
