// 引入极光推送插件
//#ifdef APP-PLUS
var jpushModule = uni.requireNativePlugin("JG-JPush");
//#endif

//#ifdef H5
let jpushModule = 'h5端不知道为什么也会加载这玩意,不定义下面还会出错'
//#endif

export function appInit(phone) {
	// 如果未能加载,不执行后续
	if (!jpushModule) return

	// 开启debug模式
	jpushModule.setLoggerEnable(true);

	// 初始化SDK
	jpushModule.initJPushService();

	// 连接状态回调
	jpushModule.addConnectEventListener(result => {
		let connectEnable = result.connectEnable
		// true已连接, false未连接
		console.log("jpush连接", connectEnable)
	})

	// 通知数组
	let messageIDList = []

	// 通知事件回调
	jpushModule.addNotificationListener(result => {
		// 通过 notificationEventType字段区分是收到通知还是点击通知
		const {
			notificationEventType,
			messageID,
			title,
			content
		} = result

		messageIDList.push(messageID)

		if (notificationEventType == 'notificationOpened') {
			// 点击通知操作
			console.log('点击通知')
		} else if (notificationEventType == 'notificationArrived') {
			// 收到通知
			console.log('收到通知')
		}
	})

	// 获取应用程序的 RegistrationID。 只有当应用程序成功注册到 JPush 的服务器时才返回对应的值,否则返回空字符串
	jpushModule.getRegistrationID(result => {
		// RegistrationID单独发送消息用
		console.log(result);
		if (result.registerID) {
			console.log(result.registerID)
			uni.setStorageSync("register_id", result.registerID)
		}
	})
}

export function addUser(phone) {
	if (!jpushModule) return
	// 设置别名
	jpushModule.setAlias({
		"alias": phone,
		"sequence": 1
	})
}

export function delMsg() {
	if (!jpushModule) return
	// 设置别名
	// jpushModule.clearLocalNotifications()
	jpushModule.setBadge(0)
}

// 删除别名
export function deleteAlias() {
	if (!jpushModule) return
	jpushModule.deleteAlias({
		'sequence': 1
	})
}