const BuildingManager = require("../utils/BuildingManager");
const ShareAction = require('./ShareAction');
const Notikey = require('../utils/GameEnum').GameNotificationKey;
const HomeApi = require("../net/HomeApi");

window.Global = {

    // 建筑管理
    BuildingManager: BuildingManager.instance,

    debug: true,

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

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

    //SystemInfo
    ver: 1,
    os: 1,//1 android,2 ios
    channel: CC_WECHATGAME ? "weixin" : "LuciferChannel",

    user: null,

    homeUpdate: true,

    wechatScoreKey: 'starCount',

    // 开发中的城市Id
    devCityId: 1,

    buildRes: null,

    needLogin: false,
    /// 最后一个缩短技能cd的技能等级
    rcdSkillLevel: 0,
};

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;
                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) {
        //         GameEvent.fire(Notikey.HomeReloadData);
        //     } else {
        //         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()));
    });

    wx.showShareMenu({
        withShareTicket: false,
        success: function (res) {

        },
        fail: function (res) {

        },
        complete: function (res) {
            }
    });

    wx.onShareAppMessage(function (res) {
        // 用户点击了“转发”按钮
        if (res.from === 'button') {
            // 来自页面内转发按钮
        } else if (res.from === 'menu') {

        }
        return {
            title: '确认过眼神，你就是我要的C位',
            imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share1.jpg',
            success: function (res) {
                console.error(res);
                    // 转发成功
                if (!UserInfo.IsEmpty(target) && !UserInfo.IsEmpty(callback)) {
                    callback.call(target, res.shareTickets);
                    //res.shareTickets不为空则是群
                }
                },
            fail: function (res) {
                    // 转发失败
                console.error(res);
                },
            complete: function (res) {

            }
        }
    });
}