<template>
	<view class="item" @appear="appear" @disappear="disappear" @click.stop :style="{height:height+'px'}" :key="index">

		<!-- :controls="showControls" -->
		<video class=" video" :show-fullscreen-btn="false" @controlstoggle="controlstoggles" v-if="showVideo"
			play-btn-position="bottom" :show-center-play-btn="true" :show-play-btn="true" @click.stop="videoClick()"
			@loadedmetadata="loadedmetadata" @timeupdate="timeupdate" @waiting="waiting()" object-fit="cover"
			@loadeddata="waiting()" @pause="onpause"
			@play="videoPlay('myVideo'+item.courseDetailsId,item.courseDetailsId)" :play-strategy="2"
			:show-loading="true" codec="software" :muted="false" :loop="loop" :enable-progress-gesture="false"
			:poster="item.titleImg" :ref="'myVideo'+item.courseDetailsId" :autoplay="autoplay" @ended="ended"
			:id="'myVideo'+item.courseDetailsId" :src="item.videoUrl"></video>


		<image class="poster" v-else @click.stop="posterClick" :src="item.titleImg" mode="aspectFill">
		</image>
		<!-- #ifdef APP -->
		<view class="control-play-cover" v-if="showControls&&isIos" @click="playCoverClick()">
		</view>
		<!-- #endif -->

		<!-- <view class="progress" v-if="showVideo&&!isPlying" @click.stop>
			<progress-vue @touchstart="progressScroll" @touchend="progressScrollEnd"></progress-vue>
		</view> -->
		
		<!-- <view class="play-icon" v-if="showVideo&&!isPlying" @click.stop="playIconClick()" :style="playStyle">
			<image class="icon" src="@/static/images/play.png" mode=""></image>
		</view> -->

		<view class="info" v-if="!isCommand" :style="infoStyle">
			<text class="color-fff" v-if="item.courseDetailsName">{{item.courseDetailsName}}</text>
			<view v-if="item.content" v-html="item.content"></view>
			<view class="u-m-t-20 color-fff" @click.stop="popupShow('show')">
				<text class="color-fff">
					{{item.courseDetailsName}}（共{{total}}集）选集 >
				</text>
			</view>

		</view>
		<view class="info" v-if="isCommand" :style="infoStyle">
			<text class="color-fff" v-if="item.courseDetailsName">{{item.courseDetailsName}}</text>
			<view v-if="item.content" v-html="item.content"></view>
			<view class="u-m-t-20 color-fff" @click.stop="toDetail">
				<text class="color-fff">
					查看更多续集 >
				</text>
			</view>

		</view>
		<view class="right" :style="rightStyle">
			<view class="love u-flex u-flex-xy-center u-flex-col u-m-b-40   u-text-center" @click="dianzanClick">
				<up-icon name="heart-fill" v-if="item.isGood==1" color="red" size="30"></up-icon>
				<up-icon name="heart-fill" v-else color="#ffffff" size="30"></up-icon>
				<text class="text color-fff u-font-24">{{item.goodNum<0?0:item.goodNum}}</text>
			</view>
			<view class="share u-m-b-40 u-flex u-flex-xy-center u-flex-col  u-text-center" @click="share">
				<image class="icon" src="@/static/images/share.png" mode=""></image>
				<text class="text color-fff u-font-24">分享</text>
			</view>
			<view class="zhuiju u-m-b-40 u-flex u-flex-xy-center u-flex-col  u-text-center" @click="zhuijuClick">
				<image class="icon" v-if="isCollect" src="@/static/images/shuqian_s.png" mode=""></image>
				<image class="icon" v-else src="@/static/images/shuqian.png" mode=""></image>
				<text class="text color-fff u-font-24">{{isCollect?'已追':'追剧'}}</text>
			</view>
		</view>

	</view>
</template>

<script setup>
	import * as Api from '@/api/video/index.js'
	// #ifdef APP
	const domModule = uni.requireNativePlugin('dom')
	// #endif
	import progressVue from './progress.vue'
	import {
		computed,
		nextTick,
		onMounted,
		ref,
		watch
	} from 'vue'


	let loop = ref(false)
	// #ifdef APP
	loop.value = true
	// #endif

	const props = defineProps({
		isIos:{
			type: Boolean,
			default: false
		},
		isAndriod: {
			type: Boolean,
			default: false
		},
		height: {
			type: Number,
			default: 0
		},
		item: {
			type: Object,
			defaulr: () => {
				return {
					videoUrl: ''
				}
			}
		},
		rightStyle: {
			type: Object,
			default: () => {
				return {

				}
			}
		},
		infoStyle: {
			type: Object,
			default: () => {
				return {

				}
			}
		},
		isTabbar: {
			type: Boolean,
			default: false
		},
		isCommand: {
			type: Boolean,
			default: false
		},
		instance: {
			type: Object,
			defaulr: () => {
				return {
					proxy: {}
				}
			}
		},
		current: {
			//h5判断是否是当前项目
			type: Number,
			default: 0
		},
		index: {
			type: Number,
			default: 0
		},
		nowIndex: {
			//app判断是否是当前项目
			type: Number,
			default: 0
		},
		total: {
			type: Number,
			default: 0
		},
		isCollect: {
			type: [Number, Boolean],
			default: 0
		},
		playSpeeds: {
			type: Number,
			default: 1
		},
		showAndriod: {
			type: Boolean,
			default: false
		}
	})

	let isWaiting = ref(true)

	function waiting() {
		console.log('waiting');
		isWaiting.value = true
	}

	let showControls = ref(props.showAndriod?false:true)

	let playPercent = ref(0)


	let autoplay = ref(props.item.videoUrl ? true : false)

	const emits = defineEmits(['controlstoggles', 'disappear', 'appear', 'waiting', 'videoPlay', 'ended', 'dianzanClick',
		'share', 'zhuijuClick', 'popupShow', 'itemMounted', 'toDetail', 'playStatusChange', 'progressScroll',
		'progressScrollEnd'
	])

	function progressScroll(e) {
		emits('progressScroll')
	}

	function progressScrollEnd(e) {
		emits('progressScrollEnd')
	}

	function posterClick() {
		if (!props.item.videoUrl) {
			popupShow('pay', props.item)
		}
	}

	function controlstoggles(e) {
		showControls.value = e.detail.show
		emits('controlstoggles', e)
	}

	function toDetail() {
		if (video) {
			video.pause()
		}
		emits('toDetail')
	}


	let videoIsLoadedmetadata = ref(false)

	function loadedmetadata() {
		console.log('loadedmetadata');
		videoIsLoadedmetadata.value = true
	}


	let isPlying = ref(false)


	//是否是第一次加载时的播放，不是暂停再播放
	let isFirstPlay = true
	let isPlayFinish = false //是否播放完成	
	/**
	 * @param {type}  = [start,end] 
	 */
	function sendPlayStatus(type = 'start') {
		emits('playStatusChange', {
			courseId: props.item.courseId,
			courseDetailsId: props.item.courseDetailsId,
			type
		})
		// Api.playStatus({
		// 	courseId:props.item.courseId,
		// 	courseDetailsId:props.item.courseDetailsId,
		// 	type
		// })
	}

	function timeupdate(e) {
		playPercent.value = e.detail.currentTime * 100 / e.detail.duration
		//隐藏loding
		uni.hideLoading()
		if (isPlayFinish) {
			return
		}
		if (e.detail.currentTime > e.detail.duration * 0.9) {
			if (!isFirstPlay) {
				sendPlayStatus('end')
				isPlayFinish = true
			}
		}
	}


	let videoBar = null


	let watingTimer = null

	function videoPlay() {
		if (isFirstPlay && !isPlayFinish) {
			sendPlayStatus('start')
		}

		isFirstPlay = false
		isPlying.value = true
		clearTimeout(watingTimer)
		watingTimer = setTimeout(() => {
			isWaiting.value = false
		}, 1000)
		// showControls.value = false
		// #ifdef H5
		videoBar = videoBar ? videoBar : document.querySelector('.uni-video-bar');
		videoBar.style.cssText = 'display:none;';
		// #endif

		// #ifdef APP
		if (!props.showAndriod) {}
		// #endif


	}

	function onpause() {
		isPlying.value = false
		// showControls.value = true
		// #ifdef H5
		videoBar = videoBar ? videoBar : document.querySelector('.uni-video-bar');
		videoBar.style.cssText = '';
		// #endif


	}



	function ended() {
		sendPlayStatus('end')
	}

	function dianzanClick() {
		emits('dianzanClick')
	}

	function share() {
		emits('share')
	}

	function zhuijuClick() {
		emits('zhuijuClick')
	}

	function popupShow(key) {
		emits('popupShow', key)
	}

	function playCoverClick() {
		console.log('playCoverClick');
		if (video) {
			if (isPlying.value) {
				video.pause()
			} else {
				video.play()
			}
		}
	}
	
	function playIconClick(){
		if (video) {
			if (isPlying.value) {
				video.pause()
			} else {
				video.play()
			}
		}
	}
	
	function videoClick() {
		console.log('videoClick');
		// #ifdef APP
		// 兼容ios控制条和信息展示不同步
		if (isPlying.value&&showControls.value) {
			return
		}
		// #endif

		if (video) {
			if (isPlying.value) {
				video.pause()
			} else {
				video.play()
			}
		}
	}

	let first = true

	function appear() {
		if (video) {
			video.playbackRate(props.playSpeeds)
			video.play()
		}
		emits('appear', first)
		if (first) {
			first = false
		}
	}

	let video = null

	function disappear(e) {
		emits('disappear', e)
		if (video) {
			video.pause()
		}
	}
	const showVideo = computed(() => {
		if (props.isAndriod) {
			return (props.nowIndex === props.index && props.item.videoUrl) ? true : false
		} else {
			return props.current === props.index && props.item.videoUrl
		}
	})
	onMounted(() => {
		init()
		emits('itemMounted', props.index)
	})

	function init() {
		try {
			if (props.item.videoUrl && showVideo.value) {
				video = uni.createVideoContext('myVideo' + props.item.courseDetailsId)
				video.playbackRate(props.playSpeeds)
				video.play()
			}
		} catch (error) {
			console.error('------')
			//TODO handle the exception
		}
	}
	watch(() => props.playSpeeds, (newval) => {
		console.log('speed' + newval);
		if (video) {
			video.playbackRate(newval)
		}
	})
	watch(() => showVideo.value, (newval) => {
		if (newval) {
			isFirstPlay = true
			isPlayFinish = false
			nextTick(() => {
				init()
			})
		} else {
			video = null
		}
	})

	const customStyle = computed(() => {
		// #ifdef H5
		return {
			bottom: props.isTabbar ? '50px' : '0'
		}
		// #endif
		// #ifndef H5
		return {}
		// #endif
	})
	const playStyle = computed(() => {
		if (isPlying.value) {
			return {
				bottom: '0'
			}
		}
		return {
			bottom: props.isTabbar ? (50 + 44 + 'px') : '44px'
		}
	})
	const infoStyle = computed(() => {
		// #ifdef H5
		return {
			transform: `translateX(${(!isPlying.value||!props.item.videoUrl)?0:'-750rpx'})`
		}
		// #endif
		return {
			transform: `translateX(${(showControls.value||!props.item.videoUrl)?0:'-750rpx'})`
		}
		// return {
		// 	transform: `translateX(${(!isPlying.value||!props.item.videoUrl)?0:'-750rpx'})`
		// }
	})

	const rightStyle = computed(() => {
		// #ifdef H5
		return {
			transform: `translateX(${(!isPlying.value||!props.item.videoUrl)?'0':60}px)`
		}
		// #endif
		return {
			transform: `translateX(${(showControls.value||!props.item.videoUrl)?'0':60}px)`
		}

	})
</script>

<style lang="scss" scoped>
	.item {
		// flex: 1;
		// height: 100%;
		position: relative;
	}

	/* 假设控制条的类名为 uni-video-controls */
	.uni-video-controls {
		transition: none !important;
		-webkit-transition: none !important;
	}

	.box {
		/* #ifdef H5 */
		flex: 1;
		/* #endif */
	}

	.u-flex-1 {
		flex: 1;
		width: 100%;
		height: 100%;
	}

	.u-popup {
		position: fixed !important;
	}

	::v-deep .u-popup {
		position: fixed !important;
	}

	.video {
		width: 750rpx;
		flex: 1;
	}

	.poster {
		/* #ifdef H5 */
		position: absolute;
		width: 100%;
		height: 90%;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		/* #endif */
		/* #ifdef APP */
		width: 750rpx;
		flex: 1;
		/* #endif */
	}

	.u-text-center {
		text-align: center;
	}

	.u-flex-row {
		flex-direction: row !important;
	}

	.info {
		/* #ifdef H5 */
		width: 80%;
		/* #endif */
		height: auto;
		position: absolute !important;
		bottom: 50px;
		left: 10px;
		color: #ffffff;
		font-size: 15px;
		z-index: 9999;
		transition: transform .3s ease-in-out;
	}

	.u-flex-y-center {
		align-items: center !important;
	}

	.swipers-items {
		width: 750rpx;
		flex: 1;
		position: relative;
		background-color: #000;
	}

	.right {
		transition: transform .3s ease-in-out;
		position: absolute !important;
		right: 20rpx;
		/* #ifdef H5 */
		top: 50%;
		transform: translateY(-50%);
		/* #endif */
		/* #ifdef APP */
		bottom: 100px;
		/* #endif */
		z-index: 999;
		color: #fff;
		font-size: 24rpx;

		.icon {
			width: 60rpx;
			height: 60rpx;
		}

		.share {
			.text {
				white-space: nowrap;
			}
		}

		.zhuiju {}
	}

	.poster-popup {
		position: fixed !important;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: 9999;
		justify-content: center;
		align-items: center;
	}

	.play-icon {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		justify-content: center;
		align-items: center;
		$icon-size: 80rpx;

		.icon {
			width: $icon-size;
			height: $icon-size;
		}

	}

	.hot {
		width: 40rpx;
		height: 40rpx;
	}

	.pay-list {
		.pay-list-item {
			flex-direction: row;
			justify-content: center;
			background-color: #F2F2F2;
			padding: 24rpx;
			border-radius: 20rpx;
			margin-bottom: 40rpx;
		}
	}

	.progress {
		padding: 20rpx;
		position: absolute;
		bottom: 20rpx;
		left: 0;
		right: 0;
		z-index: 1;
	}

	.zhifubao {
		width: 56rpx;
		height: 56rpx;
	}

	.control-play-cover {
		position: absolute;
		left: 10px;
		bottom: 0;
		width: 38px;
		height: 44px;
		// background-color: red;
		background-color: transparent;
		z-index: 1;
	}
</style>