<script>
	// APP 和 小程序平台
	// #ifdef  APP-PLUS || MP-WEIXIN

	import COS from 'cos-wx-sdk-v5';
	// #endif



	// #ifdef APP-PLUS
	import Aegis from 'aegis-weex-sdk';
	// #endif

	// #ifdef MP-WEIXIN
	import Aegis from 'aegis-mp-sdk';
	// #endif

	// #ifdef H5
	import Aegis from 'aegis-web-sdk';
	// #endif
	import printer from '@/components/kk-printer/index.js';
	import logger from './utils/logger'; // app.js
	import {
		genTestUserSig
	} from './debug/GenerateTestUserSig.js';
	
	// const aegis = new Aegis({
	// 	id: 'iHWefAYqKznuxWjLnr', // 项目key
	// 	reportApiSpeed: true, // 接口测速
	// });
	// uni.$aegis = aegis
	// 首先需要通过 uni.requireNativePlugin("ModuleName") 获取 module
	// #ifdef APP-PLUS
	var jpushModule = uni.requireNativePlugin("JG-JPush");
	var audioObj = uni.getBackgroundAudioManager();
	// #endif

	export default {

		globalData: {
			GaoDeAppKey: '', // 高德ios、android key
			GaoDeKey_amapkey: '', //app上填写高德web key、小程序上填写小程序的 key
			// #ifdef H5
			// mainurl: "/api/?s=",
			// #endif
			// #ifndef H5
			// mainurl: 'https://taketest.sdwanyue.com/api/?s=',
			// mainurl: 'https://take.sdwanyue.com/api/?s=',
			// mainurl: 'https://dev.xenjoyou.com/hiver',
			// mainurl: 'https://storage.xenjoyou.com/hiver',
			mainurl: 'http://192.168.100.42:8888/hiver',
			// #endif
			// weburl: 'https://taketest.sdwanyue.com/',
			weburl: 'https://takeout.sdwanyue.com/',
			// weburl: 'https://take.sdwanyue.com/',
			userinfo: '',
			safeAreaInsets: '',
			statusBarHeight: 0, //状态栏高度
			screenHeight: 0,
			windowHeight: 0, //页面高度
			pageHeight: 0,
			//安全区域
			safeArea: '',
			safeAreaBottom: 0,
			top: 0, //顶部高度
			menuButtonWidth: 0, //顶部按钮宽度
			menuButtonLeft: 0, //顶部按钮距离
			menuButtonHeight: 44,
			_i18n: '',
			$t: '',
			userInfo: {},
			registrationID: '',
			printData:[]
		},
		onLaunch: function() {
			let context = this;
			this.globalData._i18n = this.$i18n //这是原型链上的$i18n
			this.globalData.$t = function(str) {
				let $i18n = context.globalData.$i18n //这里去拿global的i18n
				return context.$t(str)
			}
			this.getAreaSize()
			console.log('jpushModule', jpushModule)
			// this.getRegistrationID()
			// #ifdef APP-PLUS
			jpushModule.initJPushService();
			
			jpushModule.setLoggerEnable(true);
			plus.screen.lockOrientation("portrait-primary");
			// // 设置别名
			// jpushModule.setAlias({
			// 	'alias': this.lxzl.getUserInfo().userId + '',
			// 	'sequence': 1
			// })
			//监听 极光推送连接状态
			this.getNotificationEnabled();
			jpushModule.addConnectEventListener(result => {
				console.log('----------------------------',result)
				let connectEnable = result.connectEnable
				if (connectEnable == true) {
					
					this.getRegistrationID()
				} else {
					
				}
				console.log('监听到了连接状态变化 --- ')
			});
			jpushModule.addNotificationListener(result => { //极光推送的消息通知回调
				jpushModule.setBadge(0);
				plus.runtime.setBadgeNumber(0);
				let notificationEventType = result.notificationEventType
				let woopId = result.extras && result.extras.dataType === 'woop' && result.extras.value;
				console.log("通知", result)
				
				this.globalData.printData.push(JSON.parse(result.android.inAppExtras))
				console.log("通知数据",this.globalData.printData)
				// printer.getIsData("1")
				printer.onPrint()
			});
			
			jpushModule.isPushStopped(res => {
				// code   0已停止推送  1未停止推送
				const {
					code
				} = res
				console.log(res, '安卓连接状态');
			})
			//#endif



			// 将原生插件挂载在 uni 上
			// #ifdef APP-PLUS
			// uni.$TUICalling = TUICalling;
			// #endif
			// 如果您已创建了 tim，请将 tim 实例挂载在 wx 上，且不可以修改 wx.$TIM（修改变量可能导致 TUICalling 组件无法正常使用）, 完成 TUICalling 初始化，
			// 如果您没有创建，可以不传

			// #ifdef APP-PLUS
			// if (this.NB.isAndroidOrIOS() == 'iOS') {
			// 	this.globalData.GaoDeAppKey = 'b1aadb7457eae6aa40a0c8ed58ae0456'
			// } else {
			// 	this.globalData.GaoDeAppKey = '43cff70e1deb9455185e2c8cf8363aec'
			// }
			if (this.NB.isAndroidOrIOS() == 'iOS') {
				this.globalData.GaoDeAppKey = 'f32a7750d2a2c3d7459f85185c867c4f'
			} else {
				this.globalData.GaoDeAppKey = 'a88be362cab2d0f18608bedcc47ace9a'
			}
			this.globalData.GaoDeKey_amapkey = '97a3e0a8bf3f3ca77193b81c48d92ee2'
			// #endif
			// #ifdef MP-WEIXIN
			this.globalData.GaoDeKey_amapkey = '52de86da47be2ea547c37dd382025a0c'
			// #endif

		},
		computed: {
			i18n() {
				return this.$t('index')
			},
		},
		methods: {
			getAreaSize() {
				var res = uni.getSystemInfoSync()
				// console.log(res)
				this.globalData.safeAreaInsets = res.safeAreaInsets
				this.globalData.windowHeight = res.windowHeight
				this.globalData.screenHeight = res.screenHeight
				this.globalData.safeArea = res.safeArea
				this.globalData.top = res.safeArea.top
				this.globalData.statusBarHeight = res.statusBarHeight
				this.globalData.pageHeight = this.globalData.windowHeight
				this.globalData.safeAreaBottom = res.screenHeight - res.safeArea.bottom
				this.globalData.pageHeight = this.globalData.windowHeight + 44 + this.globalData.top
				// #ifdef MP-WEIXIN
				var weixin = uni.getMenuButtonBoundingClientRect();
				// console.log(weixin)
				this.globalData.top = weixin.top
				this.globalData.menuButtonWidth = weixin.width
				this.globalData.menuButtonLeft = weixin.left
				this.globalData.menuButtonHeight = weixin.height
				// #endif
			},
			// TODO:
			resetLoginData() {

			},
			onTIMError() {},
			onSDKReady({
				name
			}) {
				const isSDKReady = name === uni.$TUIKitEvent.SDK_READY ? true : false
				uni.$emit('isSDKReady', {
					isSDKReady: true
				});
			},
			onNetStateChange() {},
			onSDKReload() {},
			onSdkNotReady() {},
			onKickedOut() {
				uni.showToast({
					title: '您被踢下线',
					icon: 'error'
				});
			},
			getRegistrationID() { //获取registerID
				jpushModule.getRegistrationID(result => {
					let registerID = result.registerID
					this.globalData.registrationID = registerID
					console.log("看这里", result)
					uni.setStorageSync("registerID", registerID)
					// uni.setStorageSync("signPerson", result.signPerson)
				})
			},
			getNotificationEnabled() {
				if (uni.getSystemInfoSync().platform == "ios") {
					jpushModule.requestNotificationAuthorization((result) => {
						let status = result.status
						if (status < 2) {
							this.noticMsgTool()
						}
					})
				} else {
					jpushModule.isNotificationEnabled((result) => { //判断android是否打开权限
						if (result.code == 0) { //如果为0则表示 未打开通知权限
							this.noticMsgTool()
						}
					})
				}
			},
			noticMsgTool() {
				if (uni.getSystemInfoSync().platform == "ios") {
					//苹果打开对应的通知栏
					uni.showModal({
						title: '通知权限开启提醒',
						content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
						showCancel: false,
						confirmText: '去设置',
						success: function(res) {
							if (res.confirm) {
								var app = plus.ios.invoke('UIApplication', 'sharedApplication');
								var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
								plus.ios.invoke(app, 'openURL:', setting);
								plus.ios.deleteObject(setting);
								plus.ios.deleteObject(app);
							}
						}
					});
				} else {
					//android打开对应的通知栏
					var main = plus.android.runtimeMainActivity();
					var pkName = main.getPackageName();
					var uid = main.getApplicationInfo().plusGetAttribute("uid");
					uni.showModal({
						title: '通知权限开启提醒',
						content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
						showCancel: false,
						confirmText: '去设置',
						success: function(res) {
							if (res.confirm) {
								var Intent = plus.android.importClass('android.content.Intent');
								var Build = plus.android.importClass("android.os.Build");
								//android 8.0引导
								if (Build.VERSION.SDK_INT >= 26) {
									var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
									intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
								} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
									var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
									intent.putExtra("app_package", pkName);
									intent.putExtra("app_uid", uid);
								} else { //(<21)其他--跳转到该应用管理的详情页
									intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
									var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
									intent.setData(uri);
								}
								// 跳转到该应用的系统通知设置页
								main.startActivity(intent);
							}
						}
					});
				}
			},
		},
		onShow: function() {

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

<style>
	@import url("./App.css");
</style>