import {ref, watch,computed} from "vue";
import {gooseGrowthTypeList} from "./constantConfig.js";
import {onHide} from "@dcloudio/uni-app"

const audioList = {
	0: [
		'/static/mp3/dan/01.MP3',
		'/static/mp3/dan/02.MP3',
		'/static/mp3/dan/03.MP3',
	],
	1: [
		'/static/mp3/xiaoe/01.MP3',
		'/static/mp3/xiaoe/02.MP3',
		'/static/mp3/xiaoe/03.MP3',
		'/static/mp3/xiaoe/04.MP3',
	],
	2: [
		'/static/mp3/zhonge/01.MP3',
		'/static/mp3/zhonge/02.MP3',
		'/static/mp3/zhonge/03.MP3',
		'/static/mp3/zhonge/04.MP3',
	],
	3: [
		'/static/mp3/dae/01.MP3',
		'/static/mp3/dae/02.MP3',
		'/static/mp3/dae/03.MP3',
		'/static/mp3/dae/04.MP3',
	],
}

const imageList = {
	0: ['/static/images/game/game-img1.gif', '/static/images/game/game-eat-img1.gif'],
	1: ['/static/images/game/game-img2.gif', '/static/images/game/game-eat-img2.gif'],
	2: ['/static/images/game/game-img3.gif', '/static/images/game/game-eat-img3.gif'],
	3: ['/static/images/game/game-img4.gif', '/static/images/game/game-eat-img4.gif'],
}


export function useAudio (grouthType) {
	const defaultGrouthType = ref(''); // 成长状态历史
	const audioIndex = ref(0); // 播放序列
	const innerAudioContext = ref(null);
	function createAudio() {
		const innerAudioContext = uni.createInnerAudioContext();
		innerAudioContext.autoplay = false;
		innerAudioContext.src = audioList[grouthType.value][audioIndex.value];
		innerAudioContext.onPlay(() => {
			// 播放
			uni.$emit('play-audio');
		});
		innerAudioContext.onError((res) => {
		  audioIndex.value = 0;
		  uni.$emit('play-error')
		  console.log(res.errMsg);
		  console.log(res.errCode);
		});
		innerAudioContext.onEnded(() => {
			// 播放结束
			uni.$emit('play-audio-ended')
			// 计算播放音频序列
			const list = audioList[grouthType.value];
			if (audioIndex.value >= list.length-1) {
				audioIndex.value = 0;
			} else {
				audioIndex.value = audioIndex.value+1;
			}
			// 更新音频源
			changeSource(list[audioIndex.value]);
		})
		return innerAudioContext;
	}
	function changeSource(url) {
		innerAudioContext.src= url;
		innerAudioContext.currentTime = 0;
	}
	
	onHide(() => {
		innerAudioContext.value.stop();
	})

	// 展开食物停止播放
	uni.$on('food-select-open', () => {
		innerAudioContext.value.stop();
		audioIndex.value = 0;
	})
	
	innerAudioContext.value = createAudio();
	
	function handlePlay() {
		innerAudioContext.value.destroy();
		innerAudioContext.value = null;
		innerAudioContext.value = createAudio();
		innerAudioContext.value.play()
	}
	
	watch(grouthType, () => {
		if (defaultGrouthType.value != grouthType.value) {
			const list = audioList[grouthType.value];
			// 当鹅成长状态变更时重置播放序列
			audioIndex.value = 0;
			defaultGrouthType.value = grouthType.value;
			changeSource(audioList[grouthType.value][0]);
		}
	})
	
	return {
		innerAudioContext,
		handlePlay
	}
}


export function useHuDong(props) {
	const imgUrl = ref('');
	
	function getDefaultImg() {
		const active = gooseGrowthTypeList.find(item => item.id == props.growthType);
		return active ? active.imgUrl: ''
	}

	// 初始化
	function init() {
		imgUrl.value = getDefaultImg();
	}

	function doTouch(handlePlay) {
		handlePlay();
	}
	
	// 播放开始切换动画
	uni.$on('play-audio', () => {
		imgUrl.value = imageList[props.growthType][1]
	})
	// 播放结束恢复默认
	uni.$on('play-audio-ended', () => {
		imgUrl.value = imageList[props.growthType][0]
	})
	// 展开食物恢复默认
	uni.$on('food-select-open', () => {
		imgUrl.value = imageList[props.growthType][0]
	})
	uni.$on('play-error',() => {
		imgUrl.value = imageList[props.growthType][0]
	})
	return {
		init,
		doTouch,
		imgUrl
	}
	
}
