

// 将自己写的事件监听文件导入
// var DWEvenlistener = require('./DWEvenlistener');

var DWEvenlistener = require('./DWEventListener');
var BuildingManager = require("../utils/BuildingManager");
const ShareAction = require('./ShareAction');
const Notikey = require('../utils/GameEnum').GameNotificationKey;

window.Global = {

    // 创建一个事件监听实例, 用来实现跨节点监听事件
    GameEvent: DWEvenlistener.instance,
    
    // 建筑管理
    BuildingManager: BuildingManager.instance,

    debug: true,

    /**
     * 点击别人分享的链接需要进行的操作 加好友
     */
    shareType: ShareAction.NONE,

    /**
     * 点击别人分享的链接获取到这个用户的uid,默认-1
     */
    shareUid: -1,

    //SystemInfo
    ver: 100,
    os: 1,//1 android,2 ios
    channel: "weixin",

    user: null,

    homeUpdate: true,

    wechatScoreKey: 'starCount',

    // 在这里自由切换城市皮肤
    cityId: 1,

    buildRes: null,
};

if (cc.sys.platform === cc.sys.WECHAT_GAME) {
    wx.onShow(({ query, shareTicket }) => {
        if (typeof wx.getUpdateManager === 'function') {
            const updateManager = wx.getUpdateManager()

            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate));
            })

            updateManager.onUpdateReady(function () {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
            })

            updateManager.onUpdateFailed(function () {
                // 新的版本下载失败
            })
        }

        console.log('Global onShow' + JSON.stringify(query) + '======================================');
        if (query != undefined && query != null) {
            if (query.uid != undefined) {
                // Global.processShareAction(query.uid, query.shareType);
                Global.shareUid = query.uid;
                Global.shareType = query.shareType;
                Global.GameEvent.fire(Notikey.ProcessShareAction);//处理通过点击分享链接进入游戏的各种操作，加好友.....
            }

            if (shareTicket != undefined && shareTicket != null) {

            }
        }

        let onShowTimestamp = Date.parse(new Date());
        let onHideTimestamp = cc.sys.localStorage.getItem("onHideTimestamp");

        if (onHideTimestamp !== 0) {
            let sec = (onShowTimestamp - onHideTimestamp) / 1000;
            if (sec > 180) {
                Global.GameEvent.fire(Notikey.HomeReloadData);
            } else {
                Global.GameEvent.fire(Notikey.HomeUpdateData, sec);
            }
        }

        console.log("onShowTimestamp: " + onShowTimestamp);
        console.log("onHideTimestamp: " + onHideTimestamp);
    });

    wx.onHide(() => {
        console.log('Global onHide ===================================');

        cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
    });
}