/** 将15寸和32寸共同用到的 函数内容，封装成一个js文件 */


import {
	nextTick,
	reactive,
	watch
} from 'vue';
import {
	getDataList
} from '../api/ChatApp';
import ttsUtils from './tts/ttsUtils';


// 发送消息

const handleSend = async (msgList) => {

	const data = {
		AppId: '0c8ff06b-91d9-43f7-b61e-ca5da1db15d4',
		AId: 'TK1234',
		content: ''
	}
	if (msgList.chatMsg != '') {
		msgList.demoVideo1 = false
		msgList.demoVideo.play()
		
		
		msgList.isloading[msgList.len] = true
		msgList.len++
		let obj = {
			otherContent: '',
			userContent: '',
			image: ""
		}
		data.content = msgList.chatMsg
		obj.userContent = msgList.chatMsg
		msgList.chatList.push(obj)

		obj.otherContent = await GetNewsList(msgList, data)
		console.log(obj.otherContent)
		// await sleep(16000);
		getChatContent(msgList, obj.otherContent, msgList.len)
		ttsUtils.toSpeak(obj.otherContent)


	}
	msgList.isClick = !msgList.isClick
	msgList.otherContent = ''
}

// 调用数据接口
const GetNewsList = async (msgList, data) => {
	msgList.chatMsg = ''
	var res = await getDataList(data)
	console.log(res)
	return res
}

// 逐字显示内容
const getChatContent = ((msgList, text, index) => {
	if (text == undefined) return
	msgList.timer = setInterval(() => {
		msgList.textCount++;
		if (msgList.textCount == text.length + 1) {
			msgList.otherChatList[index - 1] = text;
			clearInterval(msgList.timer);
		} else {
			// 取字符串子串
			let nowStr = text.substring(0, msgList.textCount);
			msgList.otherChatList[index - 1] = nowStr;
		}

	}, 50);
	console.log(msgList.timer)
	msgList.textCount = 0
	msgList.isloading[index - 1] = false
	msgList.isClick = !msgList.isClick

	// logo视频静止
	msgList.demoVideo.seek(0)
	msgList.demoVideo.pause()
})

// 滚动条回到最底部
const scrollToBottom = ((msgList) => {
	nextTick(() => {
		const query = uni.createSelectorQuery();
		query.select("#content-box").boundingClientRect()
		query.select("#content-overflow").boundingClientRect()
		query.exec(res => {
			const scrollViewHeight = res[0].height
			const scrollContentHeight = res[1].height
			if (scrollViewHeight < scrollContentHeight) {
				const scrollTop = scrollContentHeight - scrollViewHeight
				msgList.scrollTop = scrollTop
			}
		})
	})
})


// 背景颜色变换
const styleChange = ((styleData) => {
	styleData.value = !styleData.value
	if (styleData.value == true) {
		styleData.color = "black"
		styleData.fontColor = "#fff"
		styleData.btnColor = "#ccc"
		styleData.contentColor = "rgba(0,0,0,0.5)"
		styleData.labelColor = "rgb(105,105,105)"
		styleData.borderColor = "rgb(105,105,105)"
		styleData.videoSrc = '../../static/video/video.mp4'

	} else {
		styleData.color = "#fff"
		styleData.fontColor = "black"
		styleData.btnColor = "rgb(30,144,255)"
		styleData.contentColor = "#f5f5f5"
		styleData.labelColor = "#f5f5f5"
		styleData.borderColor = "rgb(30,144,255)"
		styleData.videoSrc = '../../static/video/whiteVideo.mp4'
	}

})

//快捷键输入
const shortcuts = ((msgList, item) => {
	msgList.chatMsg = item
	handleSend(msgList)
})

export {
	handleSend,
	styleChange,
	scrollToBottom,
	shortcuts
}