<script>
	import Vue from 'vue'
	import TIM from 'tim-wx-sdk';
	import TIMUploadPlugin from 'tim-upload-plugin'
	import sdkService from '@/api/sdk_service'
	
	export default {
		onLaunch: function() {
			// 全局变量 系统信息
			uni.getSystemInfo({
				success: e => {
					Vue.prototype.System = e;
					console.log(e)
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
			
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif
				}
			})
			
			// TIM
			uni.$TUIKit = TIM.create({
				SDKAppID: 1400651221
			})
			uni.$TUIKit.registerPlugin({
				'tim-upload-plugin': TIMUploadPlugin
			})
			// 0 普通级别，日志量较多，接入时建议使用
			// 1 release级别，SDK 输出关键信息，生产环境时建议使用
			// 2 告警级别，SDK 只输出告警和错误级别的日志
			// 3 错误级别，SDK 只输出错误级别的日志
			// 4 无日志级别，SDK 将不打印任何日志
			uni.$TUIKit.setLogLevel(4)
			uni.$TUIKitEvent = TIM.EVENT
			uni.$TUIKitVersion = TIM.VERSION
			uni.$TUIKitTypes = TIM.TYPES
			uni.$TUIKit.on(uni.$TUIKitEvent.SDK_READY, this.onSDKReady)
			uni.$TUIKit.on(uni.$TUIKitEvent.KICKED_OUT, this.onKickedOut)
			// 获取用户UserSig
			this.getUserSig()
			uni.$on('getUserSig', data => {
				this.getUserSig(data)
			})
		},
		globalData: {
			isSDKReady: false,
			bottom: 0
		},
		methods: {
			// 获取用户UserSig
			async getUserSig(data) {
				if (!data && !uni.getStorageSync('uid')) {
					return
				}
				const uid = data ? data.uid : uni.getStorageSync('uid')
				const result = await this.$axios.post(`${this.API.getUserSig}?uid=${uid}`)
				if (result) {
					// websocket
					this.getWebsocket(uid)
					// login
					sdkService.login({
						userSig: result
					}, res => {
						if (res.actionStatus === 'OK') {
							console.log('sdkService.login', data)
							// 0不是新用户，1新用户 新用户自动创建一个小队
							if (data && data.isNewUser === '1') {
								console.log('0不是新用户，1新用户 新用户自动创建一个小队')
								setTimeout(() => {
									this.addTeam(data.uid, data.teamName)
								}, 200)
							}
						}
					})
				}
			},
			// 创建小队
			addTeam (uid, teamName) {
				console.log('创建小队', uid, teamName)
				sdkService.createGroup({
					userID: uid,
					name: teamName,
					type: uni.$TUIKitTypes.GRP_PUBLIC
				}, async res => {
				console.log({
						createBy: uid,
						teamName: teamName,
						introduction: teamName,
						groupID: res.group.groupID
					})
					await this.$axios.post(this.API.teamCreate, {
						createBy: uid,
						teamName: teamName,
						introduction: teamName,
						groupID: res.group.groupID
					})
				})
			},
			// websocket
			getWebsocket (uid) {
				uni.connectSocket({
				  url: `wss://daifeiclub.com/ws/${uid}`
				})
				uni.onSocketMessage(res => {
					const data = JSON.parse(res.data)
					console.log(data)
				})
			},
			// ready
			onSDKReady({name}) {
				const isSDKReady = name === uni.$TUIKitEvent.SDK_READY ? true : false
				uni.$emit('isSDKReady', {
					isSDKReady: isSDKReady
				})
				this.$scope.globalData.isSDKReady = isSDKReady
			},
			// 用户被踢出
			onKickedOut () {
				uni.showModal({
					content: '您被踢下线',
					showCancel: false,
					success: res => {
						this.getUserSig()
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/wxcomponents/dist/common/index.wxss";
	@import "@/static/css/vant.scss";
	@import "@/static/css/app.scss";
	@import "@/static/css/icon.css";
	@import "@/static/css/style.css";

	body,
	html {
		min-height: 100%;
		height: auto;
	}

	page {
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		font-family: PingFang SC;
		line-height: 1.5;
		font-size: 28rpx;
		box-sizing: border-box;
		-moz-box-sizing: border-box;
		-webkit-box-sizing: border-box;
		color: $cc;
		position: relative;
		/* #ifdef H5 */
		padding-bottom: constant(safe-area-inset-bottom);/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom); /* 兼容 iOS >= 11.2 */
		/* #endif */
	}
	::-webkit-scrollbar {
		height: 0;
		width: 0;
		background-color: transparent;
		display: none;
	}
</style>
