module.exports = {
	data() {
		return {
			config: {
				"asrConfig": {
					"class": "TencentASR",
					"secretKey": "",
					"secretId": "AKIDyvwuvrfikhYK1dDj9Vlv154zmVAjFdt0",
					"appId": 1310500600,
					"partnerId": "0002",
					"signatureUrl": "https://commercial-integration-asr-int-api.xiaoice.com/api/v3/asr/tencentSignature",
					"hotWordId": "2ab7be50d11f11ecbfd6525400aec391",
					"silenceTime": 240,
					"engineModelType": "16k_zh",
					"needVad": 1,
					"filterDirty": 1,
					"filterModal": 0,
					"filterPunc": 1,
					"vadSilenceTime": 240,
					"convertNumMode": 1,
					"wordInfo": 2
				},
				"character": "furao-casual-v4",
				"autoStart": true,
				"characterConfig": {
					"characters": {
						"furao-casual-v4": {
							"name": "furao-casual-v4-half",
							"id": "furao-casual-v4",
							"frameWidth": 568,
							"frameHeight": 1050,
							"faceWidth": 160,
							"faceHeight": 250,
							"facePositionLeft": 475,
							"facePositionTop": 240,
							"facePositionLeftOffset": -272,
							"facePositionTopOffset": -160,
							"frameRate": 25,
							"defaultIdle": "A-idle",
							"idles": {
								"A-idle": {
									"id": "A-idle",
									"version": 4,
									"frameImageLength": 260,
									"frameImageType": "webp",
									"frameImageBackupType": "png",
									"resetFrameSingleNum": 8,
									"resetFrames": [
										7,
										15,
										23,
										31,
										39,
										47,
										55,
										63,
										71,
										79,
										87,
										95,
										103,
										111,
										119,
										127,
										135,
										143,
										151,
										159,
										167,
										175,
										183,
										191,
										199,
										207,
										215,
										223,
										231,
										239,
										247,
										255
									]
								}
							},
							"gestures": {},
							"crops": {}
						}
					},
					"sourcePath": "https://commercial-cdn.xiaoice.com/assets/images/characters",
					"batch": 16,
					"minLoadFrameNum": 50
				},

				"chatConfig": {
					"class": "CXHubChat",
					"url": "https://commercial-fab.xiaoice.com/api/hub/organizations/org-qufangwang/agents/64af7b926e1ba4813c4bd6d7/environments/draft/sessions",
					"defaultReply": [{
						"text": "对不起，我没听清~",
						"gesture": ""
					}],
					"csChatUrl": "https://commercial-fab.xiaoice.com/api/hub/organizations/org-qufangwang/characters//sessions"
				},

				"logConfig": {
					"class": "AliTraceLogger",
					"metaData": {
						"role": "prod",
						"organizationId": "org-qufangwang",
						"partnerId": "dh-api",
						"agentId": "64af7b926e1ba4813c4bd6d7"
					}
				},

				"talkConfig": {
					"class": "CarouselTalk",
					"url": "https://commercial-fab.xiaoice.com",
					"options": {
						"path": "/api/v1/carousel/avatars/furao-casual-v4/socket.io",
						"wsPath": "/api/v1/carousel/avatars/furao-casual-v4/websocket",
						"transports": [
							"websocket",
							"polling"
						],
						"query": "?image_format=.webp&voice_id=64af7b92a9d616bd022f6b6b"
					},
					"maxRetryTime": 5,
					"retryTimeInterval": 100
				}
			},


			mockReplies: ["嘻嘻", "没听清", "你真厉害"],
			plugin: null,
		}
	},

	onLoad() {
		let _this = this
		this.plugin = requirePlugin("digital-human-plugin").api;
		console.log(this.plugin, 'this.plugin')
		this.plugin.init({
			...this.config,
			onReceivedAsrText: (text) => {
				console.log("获取文本:", text);
				setTimeout(this.customChat);
				// 返回false拦截默认行为（不调用内置chat接口和播放结果）
				return false;
			},
			onSuccess: () => {
				const texts = [{
						text: `你好啊`,
						gesture: ""
					},
					{
						text: `我叫韩晓东`,
						gesture: ""
					},
				];
				async function talk(texts) {
					// 轮播文本内容
					for (let i = 0; i < texts.length; i++) {
						await _this.plugin.human.talkAsync(
							texts[i].text,
							texts[i].gesture, {
								onTalkStartOnce: (playInfo) => {
									console.log("播放句子:", playInfo);
								},
								onTalkEndOnce: () => {
									console.log("播放句子完成");
								},
							}
						);
						console.log("完成时间:", Date.now());
					}
				}
				talk(texts);
			},
		})
		// #ifdef MP-WEIXIN
		// 内存告警处理
		wx.onMemoryWarning((level) => {
			this.plugin.clearCache()
		})
		wx.setInnerAudioOption({
			// IOS静音键的情况下允许播放声音
			obeyMuteSwitch: false,
			// 播放声音时中断其他app端声音
			mixWithOther: false,
		})
		// #endif
	},

	methods: {
		// // 模拟聊天回复接口
		async getReply(text) {
			return await uni.request({
				url: `${this.config.chatConfig.url}/1`,
				method: 'POST',
				data: {
					"text": text,
					"currentSceneInfo": {
						"sceneId": ""
					}
				},
				success: (res) => {
					console.log(res)
				}
			})
			// return await this.mockReplies[Math.floor(Math.random() * this.mockReplies.length)]
		},
		
		sendChat(e) {
			this.customChat(e.detail.value)
		},
		
		async customChat(text) {
			console.log(text)
			// 	// 调用自定义的api获取回复
			const reply = await this.getReply(text)
			// 	// 主动调用播放接口，并等待播放完成
			await this.plugin.human?.talkAsync(reply)
			console.log("回复完成");
		}
	}
}
