// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

window.Global = {

    //Urls
    storeUrl:'https://api-touhu.duowan.com/product/getByType.do',
    skinBuyUrl:'https://api-touhu.duowan.com/product/buy.do',
    loginUrl:'https://api-touhu.duowan.com/auth/loginByGame.do',
    addReviveBoth:'https://api-touhu.duowan.com/user/addReviveBoth.do',
    userReport:'https://api-touhu.duowan.com/user/report.do',
    //SystemInfo
    ver:100,
    os:1,
    channel:"wexin",

    //皮肤ID
    bottleSkinId:-1,
    bottleSkinIndex:0,

    arrowSkinId:-1,
    arrowSkinIndex:0,

    //userInfo
    user:null,
    // user = {
    //     token:data.token,
    //     uid:user.id,
    //     openId:user.openId,
    //     nick:userInfo.nickName,
    //     avatarUrl:userInfo.avatarUrl,
    //     gender:userInfo.gender,
    //     gold:user.coin,
    //     score:user.score,
    //     revivedCount:0
    // };

    //分享过来的用户Id
    shareUid:-1,

    //
    showLoginPage:true,

    showTotalRank:false,
    showFriendRank:false,

    //处理分享加复活卡的方法
    dealAddReviveBoth(){
        if(this.shareUid!=-1){
            if(this.shareUid!=this.user.uid){
                console.log('addReviveBoth');
                wx.request({
                    url:Global.addReviveBoth,
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method:"POST",
                    data:{
                        token:this.user.token,
                        uid:this.user.uid,
                        channel:this.channel,
                        os:this.os, 
                        ver:this.ver,
                        targetUid:this.shareUid
                    },
                    success:(response)=>{
                        if(response.data.code!=undefined&&response.data.code==0){
                            this.user.revivedCount = this.user.revivedCount+1;
                            // wx.showToast({title:'复活卡：'+this.user.revivedCount,icon:null})
                        }
                    },
                    complete:()=>{
                        this.shareUid=-1;
                    }
                })
            }
        }
    },

    useReviveCard(){
        wx.request({
            url:'https://api-touhu.duowan.com//user/useRevive.do',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:"POST",
            data:{
                token:this.user.token,
                uid:this.user.uid,
                channel:this.channel,
                os:this.os,
                ver:this.ver,
            },
            success:(response)=>{
                if(response.data.code!=undefined&&response.data.code==0){
                    this.user.revivedCount = this.user.revivedCount-1;
                }
            },
            complete:()=>{
                this.shareUid=-1;
            }
        })
    },
};

wx.onShow(res => {
    // let shareTicket = res.shareTicket;
    // console.log(shareTicket);
    // if(shareTicket!=undefined){
    //     wx.showToast({
    //         title:shareTicket,
    //     })
    // }else{
    //     wx.showToast({
    //         title:'undefind',
    //     })
    // }

    if (typeof wx.getUpdateManager === 'function') {
        const updateManager = wx.getUpdateManager()
        
        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            console.log(res.hasUpdate)
        })
        
        updateManager.onUpdateReady(function () {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
        })
        
        updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
        })
    }

    wx.checkSession({
        success: function(){
          //session_key 未过期，并且在本生命周期一直有效
        },
        fail: function(){
          // session_key 已经失效，需要重新执行登录流程
          Global.showLoginPage = true;
          cc.director.loadScene("mainMenu");
        }
    })

    console.log('onShow res:');
    console.log(res);
    if(res!=null&&res.query!=null&&res.query.uid!=undefined){
        console.log('onShow query uid:'+res.query.uid);
        Global.shareUid = res.query.uid;
        if(Global.user!=null){
            Global.dealAddReviveBoth();
        }
    }
}); 