/**
 * 游戏结算界面的逻辑处理脚本
 */

cc.Class({
    extends: cc.Component,

    properties: {
        lauchNodeUI: cc.Node, //主游戏界面
        homePageUI: cc.Node, //首页界面
        rotatingNode: cc.Node,  //旋转节点
        passProgressBar: cc.ProgressBar,  //关卡进度条
        spRank: cc.Sprite,
        moreBtn: cc.Node,  //再来一次按钮
        showRewardBtn: cc.Node,  //炫耀有奖按钮
        lingjinbiBtn: cc.Node,  //领金币按钮
        moreGameUI: cc.Node, //更多游戏界面节点
        deathUI: cc.Node,  //游戏结束界面节点
        labGold: cc.Label,  //炫耀有奖按钮，分享成功后给100金币用户
        goldStacksTipsUI: cc.Node,  //领取奖励节点
        storeUI: cc.Node,  //商城界面节点
        shiwanUI: cc.Node,  //试玩游戏界面节点
        _state: false,  //true渲染子域内容， false停止渲染子域内容
        _tex: null, //显示排行榜的精灵图片
        _sharedCanvas: null,
        _deathJS:null,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    onEnable: function(){
        if(!sdk.getConfig4().ipLimit){  //ip限制
            if(sdk.getConfig1().hz){
                this.moreGameUI.active =true;  //显示更多游戏面板
                let guessYouLike =this.node.getChildByName('content').getChildByName("guessYouLike");
                if(!guessYouLike){
                    cc.HQ.GameMgr.loadRes('Prefabs/guessYouLike',this.node.getChildByName('content')).then(node=>{
                        node.x = 0;
                        node.y = -320;
                        node.getComponent('guessYouLikeUI').init(3);
                    });
                }else{
                    guessYouLike.setPosition(0,-320);
                }
            }
            //再来一次按钮/炫耀有奖按钮开关
            if(sdk.getConfig1().fs){
                this.moreBtn.active =true;
                this.showRewardBtn.active =true;
                this.lingjinbiBtn.active =true;
            }else{
                this.moreBtn.active =false;    
                this.showRewardBtn.active =false;
                this.lingjinbiBtn.active =false;
            } 
        }

        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            this._state =true; 
            if(!this._tex){
                console.log("需要newTexture2D");
                this._tex = new cc.Texture2D();
            }
            var openDataContext = wx.getOpenDataContext();
            this._sharedCanvas = openDataContext.canvas;
            //和设计分辨率大小一样
            this._sharedCanvas.width = 750;
            this._sharedCanvas.height = 1334;  
        } 
    },

    //再来一次按钮
    onMoreBtn: function(){  
        this._state =false;  //停止渲染子域内容
        this.node.active =false;
       
        this.lauchNodeUI.active =true;
        this.lauchNodeUI.active =false;
        this.homePageUI.active =true;
        this.rotatingNode.children.length =0;
        //返回首页后，小关从0开始切,进度条显示请0
        // cc.HQ.Local.swampyNum =0;
        cc.HQ.Local.SmallLevel =1;
        this.passProgressBar.progress = 0;
        this.homePageUI.active =false;
        this.lauchNodeUI.active =true;
    },

    //炫耀一下按钮
    showoffBtn: function(){
        
        sdk.shareAppMessage({type:6,query:""});
        //埋点
        // sdk.setSceneEvent("结算","点击","炫耀有奖按钮",{},null);
        let shareFun8 =function(){
            var settleJS =cc.find("Canvas").getChildByName("settlementUI").getComponent("settlementUI");
            settleJS.goldStacksTipsUI.getComponent("goldStacksTipsUI").init(this._data,5,100);
            settleJS.goldStacksTipsUI.active =true;
        }
        cc.HQ.GameMgr.clickShare =true;
        cc.HQ.GameMgr.shareNum6++;
        cc.HQ.GameMgr.onSimulateShareBack(shareFun8,6);
    },

    //主界面按钮
    onHome: function(){
        this._state =false;  //停止渲染子域内容
        this.node.active =false;
        // this.deathUI.active =false;
        this.lauchNodeUI.active =false;
        this.homePageUI.active =true;
        this.rotatingNode.children.length =0;
        //返回首页后，小关从0开始切,进度条显示请0
        // cc.HQ.Local.swampyNum =0;
        cc.HQ.Local.SmallLevel =1;
        this.passProgressBar.progress = 0;
    },

    //换武器按钮
    onWeapons: function(){
        this._state =false;  //停止渲染子域内容
        this.node.active =false;
        // this.deathUI.active =false;
        this.lauchNodeUI.active =false;
        this.rotatingNode.children.length =0;
        //返回首页后，小关从0开始切,进度条显示请0
        // cc.HQ.Local.swampyNum =0;
        cc.HQ.Local.SmallLevel =1;
        this.passProgressBar.progress = 0;

        //显示商城界面节点
        this.storeUI.getComponent("storeUI").initBegin(0);
        this.storeUI.active =true;
    },

    //试玩游戏领取金币
    onShiwan: function(){
        this.shiwanUI.active =true;
    },

    //渲染子域的内容
    renderSsubContent: function(){
        if(this._tex){
            var openDataContext = wx.getOpenDataContext();
            this._sharedCanvas = openDataContext.canvas;
            // //和设计分辨率大小一样
            // sharedCanvas.width = 750;
            // sharedCanvas.height = 1334;
            this._tex.initWithElement(this._sharedCanvas);
            this._tex.handleLoadedTexture();
            this.spRank.spriteFrame = new cc.SpriteFrame(this._tex);
        } 
    },

    update (dt) {
        if(this._state){        
            this.renderSsubContent();
        }
    }
});
