<template>
	<view class='flex audio' :class="{active:status}" :style="{background:audioColor}" @click='play(audioId)'>
		<view class='mr-3'>
			<view class="wifi-symbol " :class="status?'active':''">
				<view class="wifi-circle first"></view>
				<view class="wifi-circle second"></view>
				<view class="wifi-circle third"></view>
			</view>
		</view>
		
		<view v-if="!create" class="ml-4">生成</view>
		<block v-else>
			<view v-if="Loading" class="loading_icon">
				<u-loading-icon size="18" color="#fff"></u-loading-icon>
			</view>
			<view v-else class="ml-4">{{status?'暂停':'阅读'}}</view>
		</block>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				context: null,
				duration: 100,
				status: false,
				task_id:'',
				Loading:false,
				currentid:''
				
			}
		},
		props: {
			url: {
				type: String,
				default: ''
			},
			content: {
				type: String,
				default: ''
			},
			audioColor: {
				type: String,
				default: '#5ba5ef'
			},
			index: {
				type: Number,
				default: 0
			},
			cancel: {
				type: Boolean,
				default:false
			},
			create: {
				type: Boolean,
				default:false
			},
			durationS: [String, Number],
			audioId: [String, Number]
		},
		created() {
			this.context = uni.createInnerAudioContext();
			// this.context.autoplay = true
			this.context.src = this.url;
			this.context.volume = 1;
			this.onEnded();
			uni.$on('stop', (id) => {
				if (id && id != this.audioId) {
					this.context.stop();
					this.status = false;
				} else if (!id) {
					this.context.stop();
					this.status = false;
				}
			})
		},
		methods: {
			play(id) { //点击播放
				if (this.url) {
					if (this.status) {
						this.context.pause();
						this.status = !this.status;
					} else {
						uni.$emit('stop', id)
						this.context.volume = 1;
						this.context.play()
						
						this.status = !this.status;
					}
				} else {
					if(id == this.currentid&&this.Loading){
						uni.showToast({
							title:'正在生成',
							icon:'none'
						})
						return;
					}
					if(id == this.currentid&&!this.Loading){
						// console.log('1');
						if (this.status) {
							this.context.pause();
							this.status = !this.status;
						} else {
							uni.$emit('stop', id)
							this.context.volume = 1;
							this.context.play()
							this.status = !this.status;
						}
						return;
					}
					this.create = true
					this.cancel = false
					this.Loading = true
					this.currentid = id
                    this.$http('ai.tts', {
                    	text: this.content
                    }).then(res => {
						if(res.code == 0){
							this.task_id = res.data.task_id
							
							this.aittsJob(id)
						}else{
							uni.showToast({
								title:'阅读失败',
								icon:'none'
							})
							this.Loading = false
							this.create = false
						}
					})
				}
			},
			aittsJob(id){
				
				this.$http('ai.ttsJob', {
					task_ids: this.task_id
				}).then(res => {
					if (res.code == 0) {
						if(this.cancel){
							this.status = false;
							this.Loading = false
							return;
						}
						if (res.data.task_status == 'Running') {
							setTimeout(() => {
								this.aittsJob()
							}, 1500);
							return;
						}
						let ress = {
							speech_url:res.data.task_result.speech_url,
							index:this.index
						}
						this.context.src = res.data.task_result.speech_url
						
						uni.$emit('stop', id)
						
						// this.status = !this.status;
						this.Loading = false
						this.$emit('aittsjob',ress);
						// this.context.play()
						
						
					} else {
						uni.showToast({
							title:'阅读失败',
							icon:'none'
						})
						this.Loading = false
						this.create = false
					}
				});
			},
			onEnded() { //播放结束
				this.context.onEnded(() => {
					this.status = false;
				})
			},
		}
	}
</script>

<style lang="scss">
	.audio {
		background: #68d7bb;
		height: 50rpx;
		border-radius: 60rpx;
		width: 150rpx;
		align-items: center;
        position: relative;
		// padding: 20rpx;
		&.active {
			opacity: 0.8;
		}

	}

	.flex {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.flex-1 {
		flex: 1;
	}

	.ml-3 {
		margin-right: 30rpx;
		color: #fff;
	}

	.ml-4 {
		font-size: 24rpx;
		margin-right: 20rpx;
		color: #fff;
		position: absolute;
		right: 0;
	}

	.mr-3 {
		margin-left: 30rpx;
	}

	.wifi-symbol {
		width: 50rpx;
		height: 50rpx;
		box-sizing: border-box;
		overflow: hidden;
		transform: rotate(135deg) translate3d(0, 0, 0);
		-webkit-transform: rotate(135deg) translate3d(0, 0, 0);
		backface-visibility: hidden;
		-webkit-backface-visibility: hidden;
	}

	.wifi-circle {
		border: 5rpx solid #fff;
		border-radius: 50%;
		position: absolute;
	}

	.first {
		width: 5rpx;
		height: 5rpx;
		background: #fff;
		top: 45rpx;
		left: 45rpx;
	}

	.second {
		width: 25rpx;
		height: 25rpx;
		top: 35rpx;
		left: 35rpx;
	}

	.third {
		width: 40rpx;
		height: 40rpx;
		top: 25rpx;
		left: 25rpx;
	}

	.active {
		.second {
			animation: fadeInOut 1s infinite 0.2s;
			-webkit-animation: fadeInOut 1s infinite 0.2s;
		}

		.third {
			animation: fadeInOut 1s infinite 0.4s;
			-webkit-animation: fadeInOut 1s infinite 0.4s;
		}
	}

	@keyframes fadeInOut {
		0% {
			opacity: 0;
			/*初始状态 透明度为0*/
		}

		100% {
			opacity: 1;
			/*结尾状态 透明度为1*/
		}
	}
	.loading_icon{
		margin-right: 18rpx;
	}
</style>