<script>
	import {
		EMClient
	} from "@/EaseIM";
	import {
		emConnectListener,
		emMountGlobalListener
	} from "@/EaseIM/emListener";
	import {
		emConnect,
		emUserInfos,
		emConversation
	} from "@/EaseIM/emApis";
	import {
		CONNECT_CALLBACK_TYPE,
		HANDLER_EVENT_NAME
	} from "@/EaseIM/constant";
	import {
		emHandleReconnect
	} from "@/EaseIM/utils";
	// import readAppInfo from './utils/readAppInfo.js';
	const {
		fetchUserInfoWithLoginId,
		fetchOtherInfoFromServer
	} =
	emUserInfos();
	//获取会话列表
	const {
		fetchConversationFromServer,
		fetchPinConversationFromServer,
		getLocalConversations
	} = emConversation();


	export default {
		onLaunch() {
			console.log('App Launch')
			//传给监听callback回调
			return;


			const connectedCallback = (type) => {
				console.log(">>>>>IM连接回调", type);
				if (type === CONNECT_CALLBACK_TYPE.CONNECT_CALLBACK) {
					this.onConnectedSuccess();
				}
				if (type === CONNECT_CALLBACK_TYPE.DISCONNECT_CALLBACK) {
					this.onDisconnect();
				}
				if (type === CONNECT_CALLBACK_TYPE.RECONNECTING_CALLBACK) {
					this.onReconnecting();
				}
			};

			/* 链接所需监听回调 */
			emConnectListener(connectedCallback);
			/* 全局类型监听集合、消息、联系人、群组等... */
			emMountGlobalListener();
			this.handleAutoLoginEaseIM();

		},
		computed: {
			loginStoreStatus() {
				return this.$store.state.LoginStore.loginStatus;
			},
			loginStoreUserBaseInfos() {
				return this.$store.state.LoginStore.loginUserBaseInfos;
			},
		},
		onShow: function() {
			console.log('appshow===>>>>>>')
			// 读取url参数信息，存储app传递的用户相关信息
			// readAppInfo(() => {
			// 	// uni.redirectTo({
			// 	// 	url: "/pages/my/index"
			// 	// });
			// });
			// uni.redirectTo({
			// 	url: "/pages/my/index"
			// });



		},
		methods: {
			//获取会话列表
			getConversationFromServer() {

				const uIds = [];
				EMClient.getServerConversations({
					pageSize: 100,
					cursor: ''
				}).then((res) => {
					res.data.conversations && res.data.conversations.forEach((item) => {
						console.log('会话item', item)
						uIds.push(item.conversationId)
					})
					console.log('用户列表id', uIds);
					uni.setStorage({
						key: 'uIds',
						data: uIds
					})
					// EMClient.fetchUserInfoById(uIds).then((resData)=>{
					// 	console.log('用户列表',resData.data);
					// })
				}).catch((error) => {
					console.log(error);
				})
			},
			//登录环信
			async loginWithUserId() {

				try {
					const res = await loginWithPassword(
						uni.getStorageSync('im_userid'),
						uni.getStorageSync('im_pass')
					);
					this.$store.commit('SET_LOGIN_USER_BASE_INFOS', {
						loginUserId: uni.getStorageSync('im_userid'),
					});
					this.setEMUserLoginInfosToStorage(
						uni.getStorageSync('im_userid').toLowerCase(),
						res.accessToken
					);
				} catch (error) {
					console.log('>>>>>>环信登录失败', error);
					uni.showToast({
						title: '登录失败',
						icon: 'none',
					});
				} finally {

				}
			},
			setEMUserLoginInfosToStorage(userId, token) {
				const params = {
					key: `EM_LOGIN_INFOS`,
					data: {
						userId: userId,
						token: token
					},
				};
				uni.setStorage({
					...params
				});
			},
			//自动登录
			handleAutoLoginEaseIM() {
				const {
					actionEMReconnect
				} = emHandleReconnect();
				const loginInfos = uni.getStorageSync(`EM_LOGIN_INFOS`);
				console.log('loginInfos', loginInfos)
				if (!loginInfos) return;
				actionEMReconnect();
			},
			onConnectedSuccess() {
				const {
					loginUserId
				} = this.loginStoreUserBaseInfos || {};
				const finalLoginUserId = loginUserId || EMClient.user;
				if (!this.loginStoreStatus) {
					this.fetchLoginUserNeedData();
					uni.hideLoading();
					console.log(">>>>>>开始跳转至会话列表页面");
					//
					this.getConversationFromServer();
					uni.redirectTo({
						url: "../home/home?myName=" + finalLoginUserId,
					});
				}
				this.$store.commit("SET_LOGIN_USER_BASE_INFOS", {
					loginUserId: finalLoginUserId,
				});
				this.$store.commit("SET_LOGIN_STATUS", true);

			},
			onDisconnect() {
				const {
					closeEaseIM
				} = emConnect();
				const {
					actionEMReconnect
				} = emHandleReconnect();
				//断开回调触发后，如果业务登录状态为true则说明异常断开需要重新登录
				if (!this.loginStatus) {
					uni.showToast({
						title: "退出登录",
						icon: "none",
						duration: 2000,
					});
					uni.redirectTo({
						url: "../login/login",
					});
					closeEaseIM();
				} else {
					console.log(">>>>>需执行重登逻辑");
					//执行通过token进行重新登录
					actionEMReconnect();
				}
			},
			onReconnecting() {
				uni.showToast({
					title: "IM 重连中...",
					icon: "none",
				});
			},
			//获取登录所需基础参数
			async fetchLoginUserNeedData() {
				await this.$store.dispatch("fetchFriendList");
				//获取好友用户属性
				await this.$store.dispatch("fetchFriendUserInfoCollection");
				//获取当前登录用户好友信息
				await this.$store.dispatch("fetchLoginUserProfile");
				await this.$store.dispatch("fetchBlockUserList");
				// 在线状态订阅
				await this.$store.dispatch("subscribePresenceStatus");
				this.fetchJoinedGroupList();
				//初始化缓存本地的新邀请列表
				this.$store.commit("INIT_RECEIVE_INVITE_LIST");
			},
			//获取加入的群组列表
			async fetchJoinedGroupList() {
				//获取群组列表
				await this.$store.dispatch("fetchJoinedGroupList", {
					isInit: true,
				});
			},
		},

		onHide: function() {
			console.log('App Hide')
		},
	}
</script>

<style>
	/*每个页面公共css */
	* {
		padding: 0;
		margin: 0;
		/* background-color: rgb(247, 248, 252); */
		/* background-color: green; */
	}

	@import "@/uni_modules/uview-ui/index.scss";
</style>