<script>
import Vue from "vue";

// voip相关
import { isWmpf } from "./const";
import { updatePushToken } from "./api/index";
import { config } from "./data/index";

import MqttService from "./utils/globalMqtt";

// 引入voip
const wmpfVoip = requirePlugin("wmpf-voip").default;

// voip事件监听
wmpfVoip.onVoipEvent((event) => {
    console.log(`onVoipEvent`, event);
});

// 设置voip结束页面
wmpfVoip.setVoipEndPagePath({
    url: "/pages/home/home",
    key: "Call",
});

export default {
    onLaunch() {
        this.$initMqtt();
        // 初始化云开发
        if (wx.cloud) {
            wx.cloud.init({});
            console.log("云开发初始化成功");
            // voip相关设置
            wmpfVoip.setCustomBtnText("去开门");
            if (isWmpf) {
                setTimeout(() => {
                    wmpf.getWmpfPushToken({
                        success: (resp) => {
                            console.log(`getWmpfPushToken`, resp);
                            updatePushToken({
                                sn: config.sn,
                                pushToken: resp.token,
                            });
                        },
                    });
                }, 1000);
            }
        }

        // 获取系统信息
        uni.getSystemInfo({
            success: function (e) {
                // #ifndef MP
                Vue.prototype.StatusBar = e.statusBarHeight;
                if (e.platform == "android") {
                    Vue.prototype.CustomBar = e.statusBarHeight + 50;
                } else {
                    Vue.prototype.CustomBar = e.statusBarHeight + 45;
                }
                // #endif

                // #ifdef MP-WEIXIN
                Vue.prototype.StatusBar = e.statusBarHeight;
                let custom = wx.getMenuButtonBoundingClientRect();
                Vue.prototype.Custom = custom;
                Vue.prototype.CustomBar =
                    custom.bottom + custom.top - e.statusBarHeight;
                // #endif

                // #ifdef MP-ALIPAY
                Vue.prototype.StatusBar = e.statusBarHeight;
                Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
                // #endif
            },
        });
    },
    onUnload() {
        MqttService.disconnectAll();
    },
    onShow() {
        console.log("onShow", "页面又进来了");
    },
    onHide() {
        // MqttService.disconnectAll();
    },
};
</script>
