/**
 * 游戏结束后的逻辑处理脚本
 */

cc.Class({
    extends: cc.Component,

    properties: {
        labTime: cc.Label,  //倒计时文本
        daojishi0: cc.Sprite, 
        daojishi: cc.Sprite,  //倒计时呈现的图片
        settlementUI: cc.Node,  //结算界面节点
        labUserGold: cc.Label, //用户拥有的金币数
        jiangli: cc.Node,
        fuhuoBtn: cc.Node,  //继续游戏按钮节点
        getfuhuoBtn: cc.Node,  //获取复活卡按钮节点
        labFuhuoka: cc.Label,  //复活卡个数
        dikuangBg: cc.Node,  //复活内容节点
        videoIcon: cc.Node,  //看视频图标
        _times: 10,  //倒计时时间
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
    },

    onEnable: function(){
        console.log("死亡");
        //获取复活卡按钮上，要不要显示视频图片
        let state =cc.HQ.GameMgr.onShareVideoState(sdk.getConfig2().model.v4);
        console.log("显不显示视频图片1",state);
        if(state){
            this.videoIcon.active =true;
        }else{
            this.videoIcon.active =false;    
        }
        //使用复活卡内容按钮开关
        if(!sdk.getConfig4().ipLimit){  //ip限制
            if(sdk.getConfig1().hz3){
                this.dikuangBg.active =true;
                let guessYouLike =this.node.getChildByName('content').getChildByName("guessYouLike");
                if(!guessYouLike){
                    cc.HQ.GameMgr.loadRes('Prefabs/guessYouLike',this.node.getChildByName('content')).then(node=>{
                        console.log('loadFload',node);
                        node.x = 0;
                        node.y = -561;
                        node.getComponent('guessYouLikeUI').init(3);
                    });
                }else{
                    guessYouLike.setPosition(0,-561);
                }
            }else{
                this.dikuangBg.active =false;
            }
        }
        if(cc.HQ.Local.continueState<=15){
            this.fuhuoBtn.getComponent(cc.Button).interactable =true;
            cc.HQ.GameMgr.tureToashBtn(this.fuhuoBtn,2);
        }else{
            this.fuhuoBtn.getComponent(cc.Button).interactable =false;
            //按钮置灰
            cc.HQ.GameMgr.tureToashBtn(this.fuhuoBtn,1);
        }

        //保存用户关卡数
        //cc.HQ.GameMgr.setPassNum(cc.HQ.Local.CurrentLevels);
        //本地上传要保存的数据  (上传给微信)
        cc.HQ.GameMgr.setUserCloudStorage(cc.HQ.GameMgr.getPassNum());

        //隐藏本关奖励节点，显示当前用户拥有的金币数
        this.jiangli.active =false;
        let userGold =cc.HQ.GameMgr.getGold();
        this.labUserGold.string = userGold;
      
        this._times =10;
        //显示倒计时时间
        this.labTime.string =this._times;
        //显示倒计时图片
        this.daojishi.fillRange =1;
        this.schedule(this.onCountdown,1);

        //当有复活卡时，获取复活卡按钮隐藏，反之，使用复活卡按钮隐藏
        // let fuhuoka =cc.HQ.GameMgr.getFuhuoNum();
        // if(Number(fuhuoka) >=1){
        //     this.fuhuoBtn.active =true;
        //     this.getfuhuoBtn.active =false;
        // }else{
        //     this.fuhuoBtn.active =false;
        //     this.getfuhuoBtn.active =true;
        // }
        // this.labFuhuoka.string ="X"+fuhuoka;
    },

    //倒计时逻辑处理
    onCountdown: function(){
        //播放小刀飞出音效
        cc.HQ.soundMgr.playClickEffet(12);
        this._times--;
        this.labTime.string =this._times;
        if(this._times==0){
            this.daojishi.fillRange =0;
            this.unschedule(this.onCountdown);
            this.node.active =false;  //隐藏本页面
            //结束排行榜数据请求
            cc.HQ.GameMgr.onFriendRankReq(2,cc.HQ.Local.CurrentLevels);
            this.settlementUI.active =true;  //打开结算页面
            return;    
        }
        var fillRange = this.daojishi.fillRange;
        fillRange = fillRange > 0 ? fillRange -= 0.1 : 1;
        this.daojishi.fillRange = fillRange;
    },

    //使用复活卡     视频复活按钮
    onFuhuo: function(){
        console.log("@@@@@分享3");
        cc.HQ.GameMgr.isInitGame =true;
        let fuhuoka =cc.HQ.GameMgr.getFuhuoNum();
        fuhuoka =Number(fuhuoka) -1;
        cc.HQ.GameMgr.setFuhuoNum(fuhuoka);
        this.labFuhuoka.string ="X"+fuhuoka;  //显示剩余复活卡数
        //使用复活卡提示
        cc.HQ.Global.showTips(2,"使用了一张复活卡!",0.3);
        cc.HQ.Local.continueState++;
        this.node.active =false; //隐藏本页面，继续游戏
        this.jiangli.active =true;
    },

    //获取复活卡
    onGetFuhuo: function(){
        //分享或视频模式
        cc.HQ.GameMgr.shareOrVideo(sdk.getConfig2().model.v4,this.onVideo,this.onShare,1);
        //埋点
        // sdk.setSceneEvent("死亡","点击","是否复活-获取复活卡按钮",{},null);
    },

    //跳过按钮
    onHome: function(){
        this.node.active =false;  //隐藏本页面
        //结束排行榜数据请求
        cc.HQ.GameMgr.onFriendRankReq(2,cc.HQ.Local.CurrentLevels);
        this.settlementUI.active =true;  //显示要打开的页面
        this.unschedule(this.onCountdown);  //关闭定时器
    },

    //调视频
    onVideo: function(data,type,isValue){
        var death =cc.find("Canvas").getChildByName("deathUI").getComponent("deathUI");
        death.unschedule(death.onCountdown);  //关闭定时器
        sdk.videoSuccess = function(){
            //视频成功处理逻辑
            var deathJS =cc.find("Canvas").getChildByName("deathUI").getComponent("deathUI");
            //获得复活卡道具
            //当复活卡到达上限5个时，获得复活卡按钮是不可点击的
            // let fuhuoka =cc.HQ.GameMgr.getFuhuoNum();
            // fuhuoka =1;
            // cc.HQ.GameMgr.setFuhuoNum(fuhuoka);
            // deathJS.labFuhuoka.string ="X"+fuhuoka;  //显示剩余复活卡数
            // //获得复活卡提示
            // cc.HQ.Global.showTips(2,"获得一张复活卡!",0.3);
            // deathJS.getfuhuoBtn.active =false;
            // deathJS.fuhuoBtn.active =true;

            cc.HQ.GameMgr.isInitGame =true;
            cc.HQ.Local.continueState++;
            deathJS.node.active =false; //隐藏本页面，继续游戏
            deathJS.jiangli.active =true;

            if(type =="cycle1"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="share";
                }
                deathJS.videoIcon.active =false;
            }else if(type =="cycle2"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="video";
                }
                deathJS.videoIcon.active =true;
            }else if(type =="cycle3"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="video";
                }
                deathJS.videoIcon.active =true;
            }else if(type =="cycle4"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="share";
                }
                deathJS.videoIcon.active =false;
            }
            sdk.setToServer("VideoOrShareKey1","VideoOrShareData1",data,86400);
        }.bind(this);
        sdk.videoFail = function(){
            //视频失败处理逻辑
            console.log("视频未看完");
            // var deathJS1 =cc.find("Canvas").getChildByName("deathUI").getComponent("deathUI");
            // deathJS1.unschedule(deathJS1.onCountdown,1);  //定时器
        };
        var videoAd = sdk.createRewardedVideoAd(sdk.getConfig2().videoIdArr[0]);
        videoAd.load().then(() => videoAd.show());
    },

    onShare: function(data,type,isValue){
        var death =cc.find("Canvas").getChildByName("deathUI").getComponent("deathUI");
        death.unschedule(death.onCountdown);  //关闭定时器

        sdk.shareAppMessage({type:4,query:""});
        let shareFun3 = ()=>{
            console.log("老子响应的是复活卡");
            var deathJS =cc.find("Canvas").getChildByName("deathUI").getComponent("deathUI");
            //获得复活卡道具
            // //当复活卡到达上限5个时，获得复活卡按钮是不可点击的
            // let fuhuoka =cc.HQ.GameMgr.getFuhuoNum();
            // fuhuoka =1;
            // cc.HQ.GameMgr.setFuhuoNum(fuhuoka);
            // deathJS.labFuhuoka.string ="X"+fuhuoka;  //显示剩余复活卡数
            // //获得复活卡提示
            // cc.HQ.Global.showTips(2,"获得一张复活卡!",0.3);
            // deathJS.getfuhuoBtn.active =false;
            // deathJS.fuhuoBtn.active =true;
            cc.HQ.GameMgr.isInitGame =true;
            cc.HQ.Local.continueState++;
            deathJS.node.active =false; //隐藏本页面，继续游戏
            deathJS.jiangli.active =true;
            
            if(type =="cycle1"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="share";
                }
                deathJS.videoIcon.active =true;
            }else if(type =="cycle2"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="video";
                }
                deathJS.videoIcon.active =true;
            }else if(type =="cycle3"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="video";
                }
                deathJS.videoIcon.active =true;
            }else if(type =="cycle4"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="share";
                }
                deathJS.videoIcon.active =false;
            }
            sdk.setToServer("VideoOrShareKey1","VideoOrShareData1",data,86400);
        }
        cc.HQ.GameMgr.clickShare =true;
        cc.HQ.GameMgr.shareNum3++;
        cc.HQ.GameMgr.onSimulateShareBack(shareFun3,3);
    },

    // update (dt) {
    // }

});
