//胜利界面节点脚本

cc.Class({
    extends: cc.Component,

    properties: {
        passProgressBar: cc.ProgressBar,  //关卡进度条
        labPass: cc.Label,  //关卡数显示
        lauchNodeUI: cc.Node,  //游戏界面节点
        homePageUI: cc.Node,  //首页界面节点
        rotatingNode: cc.Node,  //旋转节点
        layoutNode: cc.Node,
        juiceCup: cc.Prefab,
        labNum: cc.Label,
        labGold: cc.Label,
        nextPassBtn: cc.Node,  //下一关按钮节点
        bounsBtn: cc.Node,   //额外奖励按钮节点
        jiangliBtn: cc.Node, //奖励按钮节点
        zhuanPanUI: cc.Node,  //转盘界面节点
        zhuanpan: cc.Node,
        goldStacksTipsUI: cc.Node,  //领取奖励节点
        yidaqieTipsUI: cc.Node,  //一刀切奖励节点
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    onEnable: function(){
        console.log("胜利关卡",cc.HQ.Local.CurrentLevels);
        if(sdk.getConfig1().hz){
            let guessYouLike =this.node.getChildByName("guessYouLike");
            let floatBox =this.node.getChildByName("floatBox");
            if(!guessYouLike){
                cc.HQ.GameMgr.loadRes('Prefabs/guessYouLike',this.node).then(node=>{
                    node.x = 5;
                    node.y = -349;
                    node.getComponent('guessYouLikeUI').init(3);
                })
            }else{
                guessYouLike.setPosition(5,-349);  
            }
            if(!floatBox){
                cc.HQ.GameMgr.loadRes('Prefabs/floatBox',this.node).then(node=>{
                    console.log('loadFload',node);
                    node.getComponent('floatBoxUI').init(2);
                });
            }
        }
        //本地上传要保存的数据  (上传给微信)
        cc.HQ.GameMgr.setUserCloudStorage(cc.HQ.GameMgr.getPassNum());
        //排行榜数据请求
        cc.HQ.GameMgr.onFriendRankReq(2,cc.HQ.Local.CurrentLevels);

        cc.HQ.Local.waterGrass =(cc.HQ.Local.CurrentLevels-2)%4;
        //显示当局获得的金币数
        this.labNum.string ="获得奖励：    +"+cc.HQ.Local.curGold;
        // cc.HQ.Local.userGold +=cc.HQ.Local.curGold;
        let userGold =cc.HQ.GameMgr.getGold();
        let tempGold =cc.HQ.Local.curGold +Number(userGold);
        this.labGold.string =tempGold;
        //保存用户拥有的金币
        cc.HQ.GameMgr.setGold(tempGold);

        if(this.layoutNode.children.length){
            this.layoutNode.removeAllChildren();
        }
        let arr=[cc.p(-167,0),cc.p(-61,0),cc.p(45,0),cc.p(151,0)];
        for(let i=0;i<=cc.HQ.Local.waterGrass;i++){
            let grass = cc.instantiate(this.juiceCup);
            grass.getComponent("juiceCup").init(i);
            this.layoutNode.addChild(grass);
            grass.setPosition(arr[i]);
        }
        if(cc.HQ.Local.waterGrass ==3){
            this.bounsBtn.active =true;
            this.nextPassBtn.active =false;
            this.jiangliBtn.active =false;
            cc.HQ.Local.waterGrass =0;  
        }else{
            this.bounsBtn.active =false;
            if(cc.sys.platform == cc.sys.WECHAT_GAME) {
                if(sdk.getConfig1().hz){
                    this.jiangliBtn.active =true;
                }else{
                    this.jiangliBtn.active =false;
                }
            }
            this.nextPassBtn.active =true;
        }
    },

    //返回主界面按钮
    onHome: function(){
        cc.HQ.GameMgr.isACut =false;  //一刀切标记还原默认状态
        this.node.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;
    },

    //进入下一关按钮
    onNextPass: function(){
        cc.HQ.GameMgr.isACut =false;  //一刀切标记还原默认状态
        this.node.active =false;
        this.rotatingNode.children.length =0;
        this.lauchNodeUI.active =true;   //游戏界面为true就会调用onEnable生成水果布局
        this.rotatingNode.setPosition(0,800);
        this.rotatingNode.runAction(cc.moveTo(0.2,cc.p(0,250)));
        //下一关卡进度条显示
        this.passProgressBar.progress = 0;
        this.labPass.string ="第"+(cc.HQ.Local.CurrentLevels)+"关";
        
        //刷新banner广告
        //显示banner广告   (bannerID2或bannerID3)
        let bar =Math.random()>0.5 ? 1 : 2;
        if(cc.HQ.Local.CurrentLevels %2 ==0){
            this.onFlushBar(bar);
        }
    },

    onFlushBar: function(barNum){
        sdk.createBannerAd({},sdk.getConfig2().bannerArr[barNum]).destroy();
        sdk.BannerAd =null;
        var bannerAd =sdk.createBannerAd({},sdk.getConfig2().bannerArr[barNum]);
        bannerAd.show();
    },

    //额外奖励按钮  
    onReward: function(){
        console.log("转盘关卡!");
        cc.HQ.GameMgr.isACut =false;  //一刀切标记还原默认状态
        this.node.active =false;
        this.zhuanPanUI.active =true;
        this.zhuanpan.active =true;
    },

    //奖励翻倍按钮
    onJiangliBtn: function(){
        cc.HQ.GameMgr.isACut =false;  //一刀切标记还原默认状态
        
        sdk.shareAppMessage({type:14,query:""});
        //埋点
        // sdk.setSceneEvent("结算","点击","炫耀有奖按钮",{},null);
        let shareFun10 =function(){
            var victoryJS =cc.find("Canvas").getChildByName("victoryUI").getComponent("victoryUI");
            victoryJS.goldStacksTipsUI.getComponent("goldStacksTipsUI").init(this._data,6,cc.HQ.Local.curGold);
            victoryJS.goldStacksTipsUI.active =true;
        }
        cc.HQ.GameMgr.clickShare =true;
        cc.HQ.GameMgr.shareNum6++;
        cc.HQ.GameMgr.onSimulateShareBack(shareFun10,6);
    },

    // update (dt) {},
});
