<template>
	<view></view>
</template>

<script setup>
	import {
		useAudio
	} from './useAudio';
	import { onUnmounted } from "vue"
	
	let historyData = null;
	const {
		handlePlay,
		handleStop,
		innerAudioContext
	} = useAudio();
	let num = 0;
	let alreadyplay = false; // 是否是首次播放
	function doPlay(data) {
		// console.log('dddd')
		if (historyData) {
			if (historyData.stId == data.stId && historyData.index == data.index) {
				// 播放中切换其他音频时发出事件 同题切换音频
				historyData = null
				// console.log('切换同频 暂停或播放')
				uni.$emit('reset-playing-status', historyData);
				return;
			}
			
			if (historyData.stId == data.stId && historyData.index != data.index) {
				// 播放中切换其他音频时发出事件 同题切换音频
				uni.$emit('reset-playing-status', historyData);
				// console.log('切换同题 不同音频')
				handlePlay(data)
				historyData = data;
				return;
			}

			if (historyData.stId != data.stId) {
				// 播放中切换其他音频时发出事件 切换其他试题
				uni.$emit('reset-playing-status', historyData);
				historyData = null
				// console.log('切换不同题')
				return;
			}
		}
	
		if (num == 1) {
			// console.log('执行播放 已拦截')
			num = 0;
			alreadyplay = true
			return;
		}
		// console.log('执行播放')
		handlePlay(data)
		historyData = data;
		num++;
		if (!alreadyplay) {
			num = 0;
			alreadyplay = true;
		}
	}
	function doPause() {
		handleStop()
	}
	function doStop(){
		handleStop()
	}
	uni.$emit('swiper-change', () => {
		num = 0;
	})
	// 播放
	uni.$on('do-yy-audio-play', doPlay)
	// 暂停
	uni.$on('do-yy-audio-stop', doPause)
	// 销毁
	uni.$on('destory-stop', doStop)
	onUnmounted(() => {
		// 播放
		uni.$off('do-yy-audio-play', doPlay)
		// 暂停
		uni.$off('do-yy-audio-stop', doPause)
		// 销毁
		uni.$off('destory-stop', doStop)
	})
</script>

<style>
</style>