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

		<video class="u-flex-1 video" :show-fullscreen-btn="false" @controlstoggle="controlstoggles" v-if="showVideo"
			play-btn-position="center"
			:show-center-play-btn="false"
			:show-play-btn="false"
			:controls="true"
			@click.stop="videoClick()"
			@timeupdate="timeupdate" @waiting="waiting()" object-fit="cover" @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>
		
		<view class="play-icon" v-if="showVideo&&!isPlying" @click.stop="videoClick()" :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'
	import {
		computed,
		nextTick,
		onMounted,
		ref,
		watch
	} from 'vue'


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

	const props = defineProps({
		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
		},
		showControls: {
			type: Boolean,
			default: true
		}
	})
	let autoplay = ref(props.item.videoUrl ? true : false)

	const emits = defineEmits(['controlstoggles', 'disappear', 'appear', 'waiting', 'videoPlay', 'ended', 'dianzanClick',
		'share', 'zhuijuClick', 'popupShow', 'itemMounted', 'toDetail', 'showInfo', 'playStatusChange'
	])
	function posterClick(){
		if(!props.item.videoUrl){
			popupShow('pay',props.item)
		}
	}
	function controlstoggles(e) {
		emits('controlstoggles', e)
	}

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

	function waiting() {

	}


	let isPlying = ref(false)
	watch(() => isPlying.value, (newval) => {
		console.log(newval);
		emits('showInfo', newval)
	})


	//是否是第一次加载时的播放，不是暂停再播放
	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) {
		//隐藏loding
		// #ifdef H5
		uni.hideLoading()
		// #endif
		if (isPlayFinish) {
			return
		}
		if (e.detail.currentTime > e.detail.duration * 0.9) {
			if (!isFirstPlay) {
				sendPlayStatus('end')
				isPlayFinish = true
			}
		}
	}

	function videoPlay() {
		if (isFirstPlay && !isPlayFinish) {
			sendPlayStatus('start')
		}
		isFirstPlay = false
		isPlying.value = true
		// #ifdef H5
		emits('controlstoggles', {
			detail: {
				show: false
			}
		})
		// #endif

	}

	function onpause() {
		isPlying.value = false
		// #ifdef H5
		emits('controlstoggles', {
			detail: {
				show: true
			}
		})
		// #endif
	}



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

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

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

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

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

	function videoClick() {
		console.log('videoClick');
		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()
				console.log('init 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) => {
		console.log('showVideo change:' + 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(() => {
		return {
			transform: `translateX(${(!isPlying.value||!props.item.videoUrl)?0:'-750rpx'})`
		}
	})

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

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

	.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;
		}
		
	}
	.ji-list {
		.ji-item {
			width: 210rpx;
			margin-bottom: 20rpx;
			margin-right: 30rpx;
			height: 88rpx;
			font-size: 28rpx;
			color: #333;
			border-radius: 12rpx;
			background-color: #F5F7FF;
			position: relative;

			&.active {
				background-color: $my-main-color;
				color: #fff;
			}

			.lock {
				position: absolute;
				border-radius: 0px 6px 0px 6px;
				background-color: #ccc;
				padding: 2rpx 4rpx;
				top: 0;
				right: 0;
			}

			.playing {
				position: absolute;
				width: 32rpx;
				height: 24rpx;
				bottom: 5px;
				right: 7px;
			}
		}
	}
	/* #ifdef H5 */
	.ji-item:nth-of-type(3n) {
		margin-right: 0;
	}
	
	/* #endif */

	.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;
		}
	}

	.zhifubao {
		width: 56rpx;
		height: 56rpx;
	}
</style>