import { getStorage, setStorage } from "./storage";

let port = uni.getSystemInfoSync().platform
let timer: any = null


/*************************************************
 * Android双击返回不退出应用
*/
export function blockExit() {
    switch (port) {
        case 'android':
            let main: any = plus.android.runtimeMainActivity();
            plus.runtime.quit = function () {
                main.moveTaskToBack(false);
            }
            plus.nativeUI.toast = (function (str) {
                if (str == '再次返回退出应用') {
                    plus.runtime.quit();
                } else {
                    uni.showToast({
                        title: '再次返回退出应用',
                        icon: 'none'
                    })
                }
            })
            break
        case 'ios':
            console.log('iOS不支持此功能')
            break
    }
}


/*************************************************
 * 安卓是否打开通知权限
*/
export function isOpenNotice() {
    switch (port) {
        case 'android':
            // 通知亮屏
            const MyPlugin = uni.requireNativePlugin('RC-NoticeMonitoring')
            MyPlugin.OpenNotification({
                WakeLock_packageName: 'jxcs.Android',
            })
            const notify = uni.requireNativePlugin('Ba-Notify')
            clearTimeout(timer)
            notify.isNotifyEnabled((res: any) => {
                if (!res.isNotifyEnabled) {
                    uni.jxConfirm({
                        title: '获取通知权限',
                        cancelText: '拒绝',
                        confirmText: '允许',
                        content: '为保证设备能及时收到订单信息，请打开允许通知，并打通知中的其他开关',
                        success: () => {
                            notify.goSetNotify()
                            timer = setTimeout(() => {
                                isBatteryOptimizations()
                                clearTimeout(timer)
                            }, 1000)
                        },
                        fail: () => {
                            timer = setTimeout(() => {
                                isBatteryOptimizations()
                                clearTimeout(timer)
                            }, 1000)
                        }
                    })
                } else {
                    isBatteryOptimizations()
                }
            })
            break
        case 'ios':
            console.log('iOS不支持此功能')
            break
    }
}


/*************************************************
 * 忽略电池优化
*/
export function isBatteryOptimizations() {
    switch (port) {
        case 'android':
            const MyPlugin = uni.requireNativePlugin('RC-NoticeMonitoring')
            clearTimeout(timer)
            MyPlugin.isBatteryOptimizations((res: number) => {
                if (res != 1) {
                    uni.jxConfirm({
                        title: '忽略电池优化',
                        cancelText: '拒绝',
                        confirmText: '允许',
                        content: '由于系统会对后台应用自动采取一些限制措施，为保证应用能在后台正常运行，请将程序设置为无限制模式',
                        success: () => {
                            MyPlugin.OpenBatteryOptimizations()
                            timer = setTimeout(() => {
                                clearTimeout(timer)
                            }, 1000)
                        },
                        fail: () => {
                            timer = setTimeout(() => {
                                clearTimeout(timer)
                            }, 1000)
                        }
                    })
                } else {
                    MyPlugin.OpenBatteryOptimizations()
                }
            })
            break
        case 'ios':
            console.log('iOS不支持此功能')
            break
    }
}
