const BASE_Friend_URL = 'http://chat.yuxindazhineng.com'

// 获取好友列表
export const getChatFriend = async (sendId) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_Friend_URL}/api/chatList/getChatFriend`,
			method: "POST",
			header: {
				'Content-Type': 'application/json'
			},
			data: {
				size: 500,
				current: 1,
				sendId: sendId
			},
			success: (res) => {
				if (res.statusCode === 200) {
					const respond = res.data
					console.log("获取到的好友列表消息", respond);
					if (respond?.code === 0 || respond?.msg === '成功') {
						const friendList = respond.data?.records
						if (friendList.length > 0) {
							const receiverId = friendList[0].receiver
							// 再加一层判断，防止 receiver 为空
							if (receiverId) {
								uni.setStorageSync('receiverId', receiverId)
							}
						}
						resolve(friendList)
					} else {
						const msg = res.data.error || '获取好友列表出错啦'
						reject(msg)
					}

				} else {
					reject(`获取好友列表失败：${res.statusCode}`)
				}
			},
			fail: (err) => {
				const msg = err.errMsg || '网络错误'
				reject(msg)
			}
		})
	})
}

// 获取群聊列表
export const getGroup = async (sendId) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_Friend_URL}/api/group/getGroup`,
			method: "POST",
			header: {
				'Content-Type': 'application/json'
			},
			data: {
				size: 500,
				current: 1,
				contactId: sendId
			},
			success: (res) => {
				if (res.statusCode === 200) {
					const respond = res.data
					if (respond?.code === 0 || respond?.msg === '成功') {
						const groupList = respond.data?.records
						resolve(groupList)
					} else {
						const msg = res.data.error || '获取群聊列表出错啦'
						reject(msg)
					}

				} else {
					reject(`获取群聊列表失败：${res.statusCode}`)
				}
			},
			fail: (err) => {
				const msg = err.errMsg || '网络错误'
				reject(msg)
			}
		})
	})

}

// 获取与好友的对话内容
export const getFriendMessages = async (sessionId, current = 1) => {
	console.log('请求的好友消息id：', sessionId);
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_Friend_URL}/api/message/list`,
			method: 'POST',
			data: {
				"sessionId": sessionId,
				"size": 100,
				"current": current,
				"beginTime": "",
				"endTime": ""
			},
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				if (res.statusCode === 200) {
					const messagesInfo = res.data
					// console.log("获取到的好友信息：",messagesInfo);
					if (messagesInfo.code === 0) {
						// uni.setStorageSync('currentSessionId', messagesInfo?.data
						// 	?.sessionId)
						const messageList = messagesInfo?.data?.records || [];
						const reversedList = messageList.reverse();
						resolve(reversedList);
						// resolve(messagesInfo?.data?.records)
					} else {
						const msg = res.data.error || '获取好友消息出错啦'
						reject(msg)
					}
				} else {
					reject(`请求失败：${res.statusCode}`)
				}

			},
			fail: (err) => {
				const msg = err.errMsg || '网络错误'
				reject(msg)
			}
		})
	})
}


// 获取群聊的对话内容
export const getGroupMessages = async (groupId, current = 1) => {
	console.log('请求的群聊消息id：', groupId);
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_Friend_URL}/api/group/getGroupList`,
			method: 'POST',
			data: {
				"current": current,
				"size": 100,
				"groupId": groupId,
				"beginTime": "",
				"endTime": ""
			},
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				if (res.statusCode === 200) {
					const messagesInfo = res.data
					console.log("获取到的群聊信息：", messagesInfo);
					if (messagesInfo.code === 0) {
						// uni.setStorageSync('currentSessionId', messagesInfo?.data
						// 							?.groupId)
						const messageList = messagesInfo?.data?.records || []
						const reversedList = messageList.reverse()
						resolve(reversedList)
						// resolve(messagesInfo?.data?.records)
					} else {
						const msg = res.data.error || '获取群聊消息出错啦'
						reject(msg)
					}
				} else {
					reject(`请求失败：${res.statusCode}`)
				}

			},
			fail: (err) => {
				const msg = err.errMsg || '网络错误'
				reject(msg)
			}
		})
	})
}

// 获取群成员
export const getGroupMemberList = async (groupId) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_Friend_URL}/api/group/getGroupMemberList`,
			method: "POST",
			header: {
				'Content-Type': 'application/json'
			},
			data: {
				"groupId": groupId,
				"current": 1,
				"size": 100
			},
			success: (res) => {
				if(res.statusCode ===200){
					const respond = res.data
					if(respond.code === 0){
						const groupMemberList = respond.data.records
						resolve(groupMemberList)
					} else {
						const msg = res.data.error || '获取群成员出错啦'
						reject(msg)
					}
					
				}else{
					reject(`请求群成员失败：${res.statusCode}`)
				}
			},
			fail: (err) => {
				const msg = err.errMsg || '网络错误'
				reject(msg)
			}
		})
	})
}

// 上传文件
export const uploadFileToServer = (filePath) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${BASE_Friend_URL}/api/file/upload`,
			filePath: filePath,
			name: 'file',
			success: (res) => {
				const data = JSON.parse(res.data)
				if (data.code === 0) {
					resolve(data.data)
				} else {
					reject(data.msg || '上传失败')
				}
			},
			fail: (err) => {
				reject(err.errMsg || '网络错误')
			}
		})
	})
}
