import { xunJianStores } from "@/store/modules/index";

const SYSTEM_REASON = "reason";
//Home键
const SYSTEM_HOME_KEY = "homekey";
//最近使用的应用键
const SYSTEM_RECENT_APPS = "recentapps";

let receiver, Intent = false;
export default {
    startListen: function (needStopSystem = false) {//开始监听，初始化
        //参数needStopSystem默认不阻止系统响应，如需阻止调用startListen(true)
        /*特别提醒，全面屏手势Home和recent部分机型是监听不到的，自行测试，如遇卡死电脑控制台直接重新运行项目即可解决！*/
        let that = this;
        let main = plus.android.runtimeMainActivity();
        that.openListen();
        if (needStopSystem) {
            plus.key.addEventListener("backbutton", function (e) {
                // main.stopLockTask()//按返回键恢复
                // main.startLockTask()//阻止系统home建和近期任务键
                // main.unregisterReceiver(receiver); //同时停止接收home和recent点击
                const pages = getCurrentPages(); // 获取当前页面栈
                const currentPage = pages[pages.length - 1]; // 最后一个元素即为当前页面
                xunJianStores().modal.show = currentPage.route === 'pages/login' ? true : false
                console.log('监听返回')
                e.preventDefault();
            });
        } else {
        }

    },
    openListen: function () {//注册监听
        let that = this;
        try {
            let main = plus.android.runtimeMainActivity();
            Intent = plus.android.importClass('android.content.Intent');
            let IntentFilter = plus.android.importClass('android.content.IntentFilter');
            var filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_USER_PRESENT);
            filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            filter.addAction('android.intent.action.MEDIA_BUTTON');
            receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
                onReceive: function (context, intent) { //实现onReceiver回调函数  
                    let act = intent.getAction();
                    if (act == Intent.ACTION_SCREEN_ON) {
                        console.log('开屏')
                    } else if (act == Intent.ACTION_SCREEN_OFF) {
                        console.log('锁屏')
                    } else if (act == Intent.ACTION_USER_PRESENT) {
                        console.log('解锁')
                    } else if (act == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
                        let systemReason = intent.getStringExtra(SYSTEM_REASON);
                        console.log("关闭原因", systemReason);
                        if (systemReason != null) {
                            if (systemReason == SYSTEM_HOME_KEY) {
                                // System.out.println("按下HOME键");
                                console.log('按下HOME键')
                                that.launchApp();//启动APP

                            } else if (systemReason == SYSTEM_RECENT_APPS) {
                                // System.out.println("按下多任务键");
                                console.log('按下多任务键')
                                that.launchApp();//启动APP
                            }
                        }
                    }
                    // main.unregisterReceiver(receiver);
                    /*在这里取消会出现第二次监听无效,可根据不同业务要求
                    在不同时刻停止接收两个按键的广播，我的处理是在返回的时候停*/
                    // context.unregisterReceiver(receiver);
                }
            });
            main.registerReceiver(receiver, filter); //注册监听  
        } catch (e) {
            console.error(e);
            that.toast('初始化错误');
        }
    },
    // 全屏函数
    fullScreen() {
        // Android全屏
        if (uni.getSystemInfoSync().platform === "android") {
            plus.android.invoke("setFullScreen", true);
        }

        // iOS全屏
        if (uni.getSystemInfoSync().platform === "ios") {
            // iOS无法通过API直接全屏，可以尝试隐藏所有导航栏
            plus.navigator.hideSystemBar();
        }
    },
    // 进入固定屏幕模式
    stopHomeEmit() {
        let main = plus.android.runtimeMainActivity();
        main.stopLockTask()//按返回键恢复
        main.startLockTask()//阻止系统home建和近期任务键
    },
    // 启动APP
    launchApp() {
        var isApp = plus.runtime.isApplicationExist({
            //查看安卓系统手机有没有下载这款app
            pname: 'android.dcloud.uskyMobile' //本地浏览器的包名
        })

        if (isApp) {
            //安装了app则运行
            plus.runtime.launchApplication(
                {
                    pname: "android.dcloud.uskyMobile",
                    newTask: false
                },
                (e) => {
                    console.log("e", e);
                }
            );
        }
        plus.runtime.launcher('shortcut');
        plus.runtime.restart();
    },
    // 退出APP
    quitApp() {
        plus.runtime.quit();
    },
    // 提示
    toast(content) {
        uni.showToast({
            title: content,
            icon: 'none'

        })
    }
}
