<template>
	<view class="player-wrap" :style="frameStyle">
		<!--
			APP-PLUS 直播播放器（当前：原生 <video> 单档）

			⚠️ wrs-uts-videoplayer（IjkPlayer 内核）尚未购买授权，已临时停用。
			   购买后请按文件底部"放开 wrs-uts-videoplayer 步骤"重启第三方播放器分档。

			当前行为：
			  · RTMP / FLV → 原生 <video>（HBuilderX 5.08+ ExoPlayer/AVPlayer，
			    不支持 RTMP，可能会卡或失败——服务端尽量提供 m3u8）
			  · m3u8 / mp4 → 原生 <video>（正常）
		-->
		<!-- ===== 放开 wrs-uts-videoplayer 后取消下面 6 行注释 ===== -->
		<!-- <stream-player-external
			v-if="strategy === 'external' && hasSource"
			:source="source"
			:frame-width="frameWidth"
			:frame-height="frameHeight"
			:phase="phase"
			@play="onExtPlay"
			@buffering="onExtBuffering"
			@error="onExtError"
			@ended="onExtEnded"
		/> -->
		<video
			v-if="hasSource"
			id="livePlayer"
			ref="livePlayer"
			class="player-canvas"
			:style="frameStyle"
			:src="source"
			:controls="false"
			object-fit="contain"
			:autoplay="true"
			:loop="!isLivePhase"
			:show-center-play-btn="false"
			:show-fullscreen-btn="false"
			:show-play-btn="false"
			:show-mute-btn="false"
			:show-progress="false"
			:show-loading="false"
			:enable-progress-gesture="false"
			:enable-play-gesture="false"
			:vslide-gesture="false"
			:auto-pause-if-navigate="false"
			:auto-pause-if-open-native="false"
			@play="onVideoPlay"
			@waiting="onVideoWaiting"
			@error="onVideoError"
			@ended="onVideoEnded"
		/>
	</view>
</template>

<script>
// ===== 放开 wrs-uts-videoplayer 后取消下面 2 行注释 =====
// import StreamPlayerExternal from './stream-player-external.nvue'
// import { hasExternalLivePlayer } from '@/features/livestream/external-player-detect'

export default {
	name: 'StreamPlayerNative',
	// ===== 放开 wrs-uts-videoplayer 后取消下一行注释 =====
	// components: { StreamPlayerExternal },
	props: {
		source: { type: String, default: '' },
		frameWidth: { type: Number, default: 0 },
		frameHeight: { type: Number, default: 0 },
		phase: { type: String, default: 'onair' }
	},
	computed: {
		frameStyle() {
			const w = this.frameWidth ? `${this.frameWidth}px` : '100%'
			const h = this.frameHeight ? `${this.frameHeight}px` : '100%'
			return { width: w, height: h }
		},
		hasSource() {
			return !!this.source
		},
		isLivePhase() {
			return this.phase === 'onair' || this.phase === 'preparing'
		},
		isStreamingProtocol() {
			const src = String(this.source || '').toLowerCase()
			if (!src) return false
			if (src.startsWith('rtmp://') || src.startsWith('rtmps://')) return true
			if (src.includes('.flv')) return true
			return false
		},
		strategy() {
			// ===== 放开 wrs-uts-videoplayer 后改回下面这段 =====
			// if (this.isStreamingProtocol && hasExternalLivePlayer) return 'external'
			// return 'video'
			return 'video'
		}
	},
	mounted() {
		console.log('=== [STREAM-PLAYER:NVUE] mounted ===', {
			source: this.source,
			phase: this.phase,
			strategy: this.strategy
		})
	},
	beforeDestroy() {
		console.log('=== [STREAM-PLAYER:NVUE] beforeDestroy ===')
	},
	watch: {
		source(next, prev) {
			console.log('[STREAM-PLAYER:NVUE] source changed', { prev, next, strategy: this.strategy })
		}
	},
	methods: {
		// ===== 放开 wrs-uts-videoplayer 后取消下面 4 个方法的注释 =====
		// onExtPlay() {
		// 	console.log('[STREAM-PLAYER:APP] external play')
		// },
		// onExtBuffering() {
		// 	console.log('[STREAM-PLAYER:APP] external buffering')
		// },
		// onExtError(ev) {
		// 	console.error('[STREAM-PLAYER:APP] external error', ev && (ev.detail || ev))
		// },
		// onExtEnded() {
		// 	console.warn('[STREAM-PLAYER:APP] external ended')
		// },
		onVideoPlay() {
			console.log('[STREAM-PLAYER:APP] <video> play')
		},
		onVideoWaiting() {
			console.warn('[STREAM-PLAYER:APP] <video> WAITING (buffering)')
		},
		onVideoError(e) {
			console.error('[STREAM-PLAYER:APP] <video> ERROR', e && (e.detail || e))
		},
		onVideoEnded() {
			console.warn('[STREAM-PLAYER:APP] <video> ENDED')
		}
	}
}

/**
 * ============================================================
 * 放开 wrs-uts-videoplayer 步骤（购买授权后执行）
 * ============================================================
 *
 * 1. 确认 uni_modules/wrs-uts-videoplayer 已正确安装
 *    （manifest.json 里 app-plus.modules 里包含或自动识别）
 *
 * 2. 在本文件取消以下注释块：
 *    - <template> 顶部 <stream-player-external> 标签
 *    - <script> 顶部两条 import
 *    - components: { StreamPlayerExternal }
 *    - strategy() 计算属性内的原始 if 判断
 *    - methods 里 onExtPlay / onExtBuffering / onExtError / onExtEnded
 *
 * 3. 把 <video> 的 v-if="hasSource" 改回 v-else-if="hasSource"
 *
 * 4. 真机测试（推荐先用 pages/dev/ijk-test 测试页验证）。
 * ============================================================
 */
</script>

<style>
.player-wrap {
	position: absolute;
	top: 0;
	left: 0;
	overflow: hidden;
	background-color: #000000;
}
.player-canvas {
	position: absolute;
	top: 0;
	left: 0;
}
</style>
