<script>
	import bleTool from '@/utils/BleHelper.js';
	import upgrade from '@/utils/update.js';

	// 延迟断开蓝牙：选择文件/录音等会触发 onHide，8 秒内返回则不断开
	const BLE_DISCONNECT_DELAY = 30000;
	let _bleDisconnectTimer = null;

	export default {

		onLaunch: function() {
			
			// #ifdef APP|APP-PLUS
			let appid = plus.runtime.appid;
			console.log("appid=", appid);

			if (appid === 'HBuilder') {
				// let store = uni.getStorageInfoSync();
				// store.keys.forEach((val, index, array) => {
				// 	if (val == "tokenTime") {
				// 		let time = uni.getStorageSync(val);
				// 		if (!time) {
				// 			time = 0;
				// 		}
				// 		let currTime = new Date().getTime();
				// 		if (currTime >= time) {
				// 			uni.removeStorageSync(val);
				// 			uni.removeStorageSync("token");
				// 			uni.removeStorageSync("clientID");
				// 		}
				// 	} else if (val == "token" || val == "clientID" || val == 'phone') {
				// 		console.log("忽略登陆信息");
				// 	} else {
				// 		uni.removeStorageSync(val);
				// 	}
				// });				
			}

			

			let system = uni.getSystemInfoSync();


			let initOS = () => {
				let ble = bleTool.getBleTool();

				uni.getPushClientId({
					success(res) {
						
						console.log("推送信息:", res);
						uni.setStorageSync('push_cid', res.cid);
					},
					fail(err) {
						console.error(err)
					}
				});
				if (!uni.setAppBadgeNumber) { //兼容鸿蒙的写法
					if (plus) {
						uni.setAppBadgeNumber = plus.runtime.setBadgeNumber;
					}

				}
				uni.onPushMessage((res) => {
					console.log("收到推送消息：", res); //监听推送消息
					if (res.type == 'click') {
						//将App角标设置为0，清空app在消息中心的所有消息
						uni.setAppBadgeNumber(0);
						plus && plus.push.clear();
						uni.navigateTo({
							url:'/pages/common/Msgs/deviceMsg',
							success(r) {
								console.log("11111");
								r.eventChannel.emit("pushMsg",res);
							}
						});
						return;
					}
					if(res.data.payload && res.data.payload.force==true){
						console.log("此消息是强制消息")
						//设备在线，丢弃消息，只显示强制的消息
						uni.createPushMessage({
							title: res.data.title,
							content: res.data.content,
							payload: res.data.payload
						});
						return;
					}
					console.error("设备在线,不是强制消息,丢弃此消息");
					

				});
				
				
			}

			if (system.osName == 'android') {
				if (plus.runtime.isAgreePrivacy()) {
					initOS();
				}
			} else {
				initOS();
			}



			console.log("system=", system);
			// #endif
		},
		onShow: function() {
			console.log('App Show');
			
			
			//将检查更新换到onshow,因为苹果用户喜欢一直挂着
			// #ifdef APP|APP-PLUS
			
			if (_bleDisconnectTimer) {
				clearTimeout(_bleDisconnectTimer);
				_bleDisconnectTimer = null;
			}
			
			let appid = plus.runtime.appid;
			console.log("appid=", appid);

			if (appid !== 'HBuilder') {
				upgrade.checkAndUpdateWgt();
			}

			uni.setKeepScreenOn({
				keepScreenOn: true
			}).then(res => {
				console.log("已打开屏幕常亮");
			}).catch(ex => {
				console.error("设置屏幕常亮失败,ex=", ex)
			});
			
			//监听网络变化
			uni.onNetworkStatusChange((res)=>{
				let title='网络连接已断开';
			
				if(res.isConnected){
					title='网络连接已恢复';
					
				}
				uni.showToast({
					title:title
					
				});
			});
			// #endif


		},		
		onHide: function() {
			console.log('App Hide');
			// #ifdef APP|APP-PLUS

			uni.offNetworkStatusChange(result=>{
				console.log("取消监听网络变化");
			});
			
			uni.setKeepScreenOn({
				keepScreenOn: false
			}).then(res => {
				console.log("已关闭屏幕常亮");
			}).catch(ex => {
				console.error("设置屏幕常亮失败,ex=", ex)
			})

			// 上传中不主动断开：语音上传进行中则不断开蓝牙
			let ble = bleTool.getBleTool();
			if (ble && ble.isVoiceUploading && ble.isVoiceUploading()) {
				console.log('App Hide: 语音上传中，不启动断开定时器');
				return;
			}
			// 延迟断开：选文件/录音会短暂 onHide，8 秒内返回则不断开
			if (_bleDisconnectTimer) clearTimeout(_bleDisconnectTimer);
			_bleDisconnectTimer = setTimeout(() => {
				_bleDisconnectTimer = null;
				
				if (ble) {
					console.log("App隐藏了,断开所有蓝牙设备,停止搜索");
					ble.StopSearch().catch(ex => {});
					ble.disconnectDevice().catch(ex => {});
					console.log("断开所有蓝牙设备");


				}
			}, BLE_DISCONNECT_DELAY);
			// #endif
		},
		onError(ex) {
			console.error("出现了未知的异常", ex);
		}
	}
</script>

<style lang="scss">
	@import 'vk-uview-ui/index.scss';

	/* #ifdef APP-ANDROID */
	//苹果应用以下样式后Slider不可拖动
	uni-slider .uni-slider-handle-wrapper {
		border-radius: 20rpx;
	}

	uni-slider .uni-slider-thumb {
		width: 66rpx !important;
		height: 80rpx !important;
		margin-top: -40rpx !important;
		border-radius: 16rpx !important;
		margin-left: -72rpx !important;

	}

	uni-slider .uni-slider-handle-wrapper {
		height: 88rpx;
		position: relative;

		// }
		/* #endif */
		/* #ifndef APP-NVUE|APP-PLUS-NVUE */
		.custom-file-picker .file-picker__box-content {
			background: rgba(26, 26, 26, 1);
			border: none !important;
			border-radius: 45rpx;
			width: 180rpx;
			height: 180rpx;
		}
		
		.uni-file-picker.custom-file-picker {
			overflow: inherit !important;
		}
		
		.custom-file-picker .icon-add {
			height: 5rpx !important;
			width: 70rpx !important;
		}
		
		.uni-picker-view-mask {
			background: rgba(42, 42, 42, 1);
			z-index: -1;
		}
		
		.uni-picker-view-content {
			position: inherit;
		}
		
		.uni-picker-view-wrapper {
			background: rgba(42, 42, 42, 1);
		}
		
		.green {
			color: #aed600 !important;
		}
		
		.red {
			color: #FF0000 !important;
		}
		
		.maincontent {
			width: 100%;
			min-height: 100vh;
			height: auto;
			padding: 30rpx;
			box-sizing: border-box;
		
		}
		
		.contentBg {
			background-color: #121212;
			color: #ffffffde;
		}
		
		.uni-app--maxwidth,
		.uni-body {
			background-color: #121212;
		}
		
		.fleft {
			float: left;
		}
		
		.fright {
			float: right;
		}
		
		.clear {
			clear: both;
		}
		
		.displayNone {
			display: none !important;
		}
		
		.visibilityHidden {
			visibility: hidden !important;
		}
		
		.center {
			display: flex;
			flex-direction: row;
			flex-wrap: nowrap;
			align-content: center;
			justify-content: center;
			align-items: center;
		}
		.colCenter{
			display: flex;
			flex-direction: column;
			flex-wrap: wrap;
			align-content: center;
			justify-content: center;
			align-items: center;
		}
		.navbarRight .img {
			width: 35rpx;
			height: 35rpx;
			margin-right: 30rpx;
		}
		.navbarRight .img:last-child{
			margin-right: 10rpx !important;
		}
		.uni-navbar--fixed {
			top: 0rpx;
		}
		
		::v-deep .uni-navbar--fixed {
			top: 0px;
		}
		
		::v-deep .uni-navbar__placeholder {
			display: none !important;
		}
		::v-deep .mescroll-upwarp{
			text-align: center;
		}
		/* #endif */
		

		@font-face {
			font-family: "PingFang SC";
			src: url("~@/static/fonts/PingFangSC.ttf") format("opentype");
		}

		@font-face {
			font-family: "PingFangBold";
			src: url("~@/static/fonts/PingFangSC.ttf") format("opentype");
		}
</style>