//游戏界面逻辑脚本
var TableReader =require("TableReader");

cc.Class({
    extends: cc.Component,

    properties: {
        fruitPres: [cc.Prefab],  //水果预制体
        fruitPoint: cc.Prefab,  //水果的底图点
        masterNode: cc.Node,
        rotatingNode: cc.Node,  //水果的父节点
        knife: cc.Node,   //一把飞镖节点
        knifeSp: cc.Sprite, //一把飞镖显示的皮肤
        knifeNum: cc.Node, //三把飞镖节点
        pumpNode: cc.Node,  //飞刀特效
        pumpNode1: cc.Node, //飞刀特效1
        bouncedTips: cc.Node,   //说明提示弹窗节点
        passProgressBar: cc.ProgressBar,  //关卡进度条
        labPass: cc.Label,  //关卡数显示
        waterCupUI: cc.Node,  //果汁在杯中展示
        victoryUI: cc.Node, //胜利界面节点
        threeDartBtn: cc.Node, //三镖齐发按钮
        textBg: cc.Node,
        labSanNum: cc.Label,  //显示三镖齐发还有几次用完
        labTinian: cc.Label, //显示体验次数
        labGold: cc.Label, //本关奖励金币数
        jiangli: cc.Node,
        moreGameUI: cc.Node, //更多游戏界面节点
        storeUI: cc.Node,  //商城界面节点
        baodaoBtn: cc.Node,  //宝刀推荐按钮
        yidaqieTipsUI: cc.Node,  //一刀切奖励节点
        mainSkinShow: cc.Node,
        mainSkin: cc.Sprite,
        mainTaskNode: cc.Node,
        labLevel: cc.Label,
        mainTaskTipsUI: cc.Node,  //通关奖励节点
        _fruitPreShow: [],   //水果预制显示
        _diffConst: 1, //难度系数
        _isRote: false,  //节点运动分为两个：1,下落，2，旋转
        _tips:false,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    onLoad () {
        this.onSetNode();
    },

    onSwitch: function(){
        //三镖齐发按钮开关
        if(!sdk.getConfig4().ipLimit){  //ip限制
            if(sdk.getConfig1().hz2){
                this.threeDartBtn.active =true;
            }else{
                this.threeDartBtn.active =false;
            }   
        } 
    },

    //将游戏初始化，(如：重新开始、再来一次游戏)
    onEnable: function(){
        if(!sdk.getConfig4().ipLimit){  //ip限制
            if(sdk.getConfig1().hz){
                this.moreGameUI.active =false;  //隐藏更多游戏面板
            }
        }
        this.onBegin();
    },

    onSetNode: function(){
        //前后台切换 监听
        cc.game.on(cc.game.EVENT_HIDE, function(){
            console.log("游戏进入后台");
            this.doSomeThing();
        },this);
        cc.game.on(cc.game.EVENT_SHOW, function(){
            console.log("重新返回游戏");
            // this.doSomeThing();
        },this);
    },

    onBegin: function(){
        //三飞镖没有用完继续可以使用
        this.onSanKnifeShow();
        this.labPass.string ="第"+(cc.HQ.Local.CurrentLevels)+"关";
        //隐藏投诉按钮
        cc.HQ.GameMgr.feedbackButtonHide();
        cc.HQ.Local.curGold =0;    //当前本局获得金币 进入界面是0
        this.labGold.string = cc.HQ.Local.curGold;
        this.jiangli.active =true;
        //果汁预制体节点回收  (果汁清空)
        this.waterCupUI.getComponent("waterCupUI").onPutNode();
        //*******飞刀与水果布局 ********/
        //检查武器皮肤 ，是否需要更换皮肤
        this.onCheckSkin();

        //生成水果布局 (对应当前的关卡)
        this.onCreateFruitLayout(cc.HQ.Local.CurrentLevels);
        this.rotatingNode.runAction(cc.moveTo(0.2,cc.p(0,250)));

        //三镖齐发按钮开关
        this.onSwitch();
    },

    //检查武器皮肤 ，是否需要更换皮肤
    onCheckSkin: function(){
        let skin =null;
        let userCurWeapan =null;
        if(cc.HQ.Local.weaponSkin>0){  //皮肤体验的还有次数
            console.log("1234557777lauchNodeUI",cc.HQ.Local.knifeSkinId)
            skin =TableReader.getTableRowByNum("knifeItems",cc.HQ.Local.knifeSkinId);
            // this.threeDartBtn.active =false;   //隐藏三飞镖按钮
        }else{
            // userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
            userCurWeapan =cc.HQ.GameMgr.onSetCurrSkin();  //显示当前装备的飞刀
            console.log("123455lauchNodeUI",userCurWeapan)
            skin =TableReader.getTableRowByNum("knifeItems",userCurWeapan);
            // this.threeDartBtn.active =true;   //显示三飞镖按钮
        }
        //更换飞刀图片
        if(cc.HQ.Local.weaponClones && cc.HQ.Local.weaponClones>0){
            // cc.HQ.GameMgr.onReplaceKnife(this.knifeNum.getComponent(cc.Sprite),skin);
            let sanKnife =this.knifeNum.getComponent("sanlaunch");
            let arr =[sanKnife.feibiao1,sanKnife.feibiao2,sanKnife.feibiao3];
            for(let i=0;i<arr.length;i++){
                //更换飞刀图片
                // cc.HQ.GameMgr.onReplaceKnife(arr[i].getComponent(cc.Sprite),skin);
                let spriteWeapon =arr[i].getChildByName("knifeSp").getComponent(cc.Sprite);
                cc.HQ.GameMgr.onReplaceKnife(spriteWeapon,skin);
                if(cc.HQ.GameMgr.onCheckStarand(userCurWeapan)){   //特定的飞刀就显示飞刀特效
                    // this.pumpNode.active =true;
                    // this.pumpNode1.active =true;
                    arr[i].getChildByName("pumpNode").active =true;
                    arr[i].getChildByName("pumpNode1").active =true;
                }else{
                    // this.pumpNode.active =false;
                    // this.pumpNode1.active =false; 
                    arr[i].getChildByName("pumpNode").active =false;
                    arr[i].getChildByName("pumpNode1").active =false;
                }
            }
        }else{
            // cc.HQ.GameMgr.onReplaceKnife(this.knife.getComponent(cc.Sprite),skin);  
            cc.HQ.GameMgr.onReplaceKnife(this.knifeSp,skin);
            if(cc.HQ.GameMgr.onCheckStarand(userCurWeapan)){   //特定的飞刀就显示飞刀特效
                this.pumpNode.active =true;
                this.pumpNode1.active =true;
            }else{
                this.pumpNode.active =false;
                this.pumpNode1.active =false;  
            }
        }    
    },

    //处理屠龙刀逻辑
    onBaodaoWeapons: function(){
        //若已拥有屠龙刀，则不显示屠龙宝刀按钮
        console.log("屠龙刀使用时间1",cc.HQ.GameMgr.getBaodaoTime());
        let baodaoTime =cc.HQ.GameMgr.getBaodaoTime();
        let myArr =cc.HQ.GameMgr.getKnife();
        if(Number(baodaoTime)>0){   //有屠龙刀存储时间
            let curDate =Date.now();   //当天的当前时间
            console.log("屠龙刀使用时间2",curDate,curDate -Number(baodaoTime),myArr);
            if(Number(curDate -Number(baodaoTime)) > 60000){   //使用时间就算用完了
                if(typeof(myArr)=="string"){
                    myArr =myArr.split(",");
                }
                if(myArr.indexOf(82) != -1){
                    myArr.splice(myArr.indexOf(82),1);
                    console.log("屠龙刀使用时间3",myArr);
                    //屠龙刀使用时间到后，默认装备经典皮肤   (去掉屠龙皮肤)
                    cc.HQ.GameMgr.setKnife(myArr);
                    cc.HQ.GameMgr.setCurKnife(1);
                    // cc.HQ.GameMgr.setBaodaoTime(0); //屠龙刀记录时间重置
                }else{
                    if(cc.HQ.GameMgr.bdtips){
                        //屠龙刀提示
                        cc.HQ.Global.showTips(2,"屠龙刀本次体验结束!",0.8);
                        cc.HQ.GameMgr.bdtips =false;
                    }
                    if(!sdk.getConfig4().ipLimit){  //ip限制
                        if(sdk.getConfig1().gzh){
                            //显示屠龙宝刀按钮
                            this.baodaoBtn.active =true;
                        }else{
                            //隐藏屠龙宝刀按钮
                            this.baodaoBtn.active =false;
                        } 
                    }
                }
            }else{
                if(!sdk.getConfig4().ipLimit){  //ip限制
                    if(sdk.getConfig1().gzh){
                        //不显示屠龙宝刀按钮
                        this.baodaoBtn.active =false;
                    }else{
                        //隐藏屠龙宝刀按钮
                        this.baodaoBtn.active =false;   
                    }
                }
            }
        }else{   //未有屠龙刀存储时间
            if(!sdk.getConfig4().ipLimit){  //ip限制
                if(sdk.getConfig1().gzh){
                    //显示屠龙宝刀按钮
                    this.baodaoBtn.active =true;
                }else{
                    //隐藏屠龙宝刀按钮
                    this.baodaoBtn.active =false;     
                }
            }
        }    
    },

    //水果的布局
    onCreateFruitLayout: function(levelData){
        //处理屠龙刀逻辑
        this.onBaodaoWeapons();
        if(cc.HQ.Local.CurrentLevels && cc.HQ.Local.CurrentLevels%5 ==0){
            console.log("体验关卡");
            if(cc.HQ.Local.SmallLevel ==1){   //提示只弹第一次  
                //体验关卡提示
                cc.HQ.Global.showTips(2,"三镖齐发体验关",1.5);
            }
            // if(cc.HQ.Local.weaponClones <=0){   //如果已经是三发,就不奖了
                this.onFree();
            // }
        }else{
            cc.HQ.Local.experWeapons =false;
        }
        let mainTaskLevel =cc.HQ.GameMgr.onCheckMainTask(); //主线通关任务
        console.log("主线通关任务",mainTaskLevel);
        let skin =TableReader.getTableRowByNum("knifeItems",mainTaskLevel.mainSkinId);
        if(mainTaskLevel.isSucc){
            this.labLevel.string =mainTaskLevel.isSucc;
            cc.HQ.GameMgr.onReplaceKnife(this.mainSkin,skin);
        }else{
            this.mainSkinShow.active =false;
            this.mainSkin.node.active =false;
            this.mainTaskNode.active =false;
        }

        this.rotatingNode.stopAllActions();
        this._fruitPreShow =[];
        //游戏关卡是10大关一循环.
        var level =levelData%401 ?levelData%401 :1;
        //关卡显示
        this.onNormalPass(level);
    },

    //体验关卡免费给三飞镖体验
    onFree: function(){
        cc.HQ.Local.experWeapons =true;
        this.knifeNum.active =true;
        this.knife.active =false;

        // let userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
        let userCurWeapan =cc.HQ.GameMgr.onSetCurrSkin();  //显示当前装备的飞刀
        let skin =TableReader.getTableRowByNum("knifeItems",userCurWeapan);
        // if(cc.HQ.Local.weaponClones){
            let sanKnife =this.knifeNum.getComponent("sanlaunch");
            let arr =[sanKnife.feibiao1,sanKnife.feibiao2,sanKnife.feibiao3];
            for(let i=0;i<arr.length;i++){
                //更换飞刀图片
                // cc.HQ.GameMgr.onReplaceKnife(arr[i].getComponent(cc.Sprite),skin);
                let spriteWeapon =arr[i].getChildByName("knifeSp").getComponent(cc.Sprite);
                cc.HQ.GameMgr.onReplaceKnife(spriteWeapon,skin);
                if(cc.HQ.GameMgr.onCheckStarand(userCurWeapan)){   //特定的飞刀就显示飞刀特效
                    // this.pumpNode.active =true;
                    // this.pumpNode1.active =true;
                    arr[i].getChildByName("pumpNode").active =true;
                    arr[i].getChildByName("pumpNode1").active =true;
                }else{
                    // this.pumpNode.active =false;
                    // this.pumpNode1.active =false; 
                    arr[i].getChildByName("pumpNode").active =false;
                    arr[i].getChildByName("pumpNode1").active =false;
                }
            }
        // }  

        //显示三镖齐发还剩几发  (在体验关中就不显示了也不消耗)
        this.textBg.active =false;
        this.labSanNum.node.active =false;
    },

    //正常关卡逻辑
    onNormalPass: function(level){
        console.log("当前关卡数",level,cc.HQ.Local.swampyNum);
        //读取关卡配置表数据
        let posArray = TableReader.getPositionArray(level, cc.HQ.Local.SmallLevel);
        let fruitData = TableReader.getFruitArray(level, cc.HQ.Local.SmallLevel);
        // let fruitIscale = TableReader.getLevelIsScale(level, cc.HQ.Local.SmallLevel);
        // let bigDistance = cc.HQ.Global.random1(80, 100);
        // let smallDistance = cc.HQ.Global.random1(90, 130);
        // let time1 = cc.HQ.Global.random1(0, 2);
        // let time2 = cc.HQ.Global.random1(0, 1);
        // let isScaleData ={"big":bigDistance,"small":smallDistance,"isScale":fruitIscale,"t1":time1,"t2":time2};
        for(let i in posArray){
            //添加水果底图点预制体
            this.onGenerate(this.rotatingNode,posArray[i]);
            //添加水果预制体 
            this.onCreateAndReuse(this.rotatingNode,level,posArray[i],fruitData.fruitArray[i]);
        }
        //水果展示区域节点旋转
        this.rotatingNode.runAction(cc.repeatForever(this.onFruitTrack(level, cc.HQ.Local.SmallLevel)));
        //游戏初始化完成
        cc.HQ.GameMgr.isInitGame =true;
        cc.HQ.GameMgr.launchNum =0;
    },

    //从对象池请求对象
    onGenerate: function(parentNode,tempData){
        var point = null;
        if (cc.HQ.GameMgr.fruitPointPool.size() > 0) {  //通过 size 接口判断对象池中是否有空闲的对象
            point =cc.HQ.GameMgr.fruitPointPool.get();
        }else {    //如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
            point = cc.instantiate(this.fruitPoint);
        }
        point.parent = parentNode; // 将生成的加入节点树  
        point.setPosition(tempData.x,tempData.y);
    },

    //获得运动轨迹
    onFruitTrack: function(levelId, chirldLevel){
        // console.log("大小关onFruitTrack",levelId, chirldLevel);
        let actArray = TableReader.getActArray(levelId, chirldLevel);
        let tempActArray = new Array();
        for (let i = 0; i < actArray.length; i++) {
            let actItemInfo = actArray[i];
            let actionItem = cc.rotateBy(actItemInfo.time, actItemInfo.rotation).easing(cc.easeInOut(actItemInfo.eTime));
            tempActArray.push(actionItem);
        }
        if(tempActArray.length == 1){
            tempActArray.push(cc.callFunc(() => {}));
        }
        return cc.sequence(tempActArray);        
    },

    //先去水果对象池中取，有的话就直接取。没有的话就创建,然后放到水果对象池中   
    onCreateAndReuse: function(parentNode,level,tempData,temp2Data){
        let fruitPre =null;
        fruitPre =cc.HQ.GameMgr.recyleKindFruit(temp2Data);
        this.onGenerateFruit(fruitPre,parentNode,level,tempData,temp2Data);
    },

    //从水果对象池中取水果预制
    onGenerateFruit: function(fruitPool,parentNode,level,tempData,temp2Data){
        let fruit = null;
        let typeData =null;
        if (fruitPool.size() > 0) {  //通过 size 接口判断水果对象池中是否有空闲的对象
            fruit =fruitPool.get();
        }else {    //如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
            fruit =cc.instantiate(this.fruitPres[temp2Data]);   //取对应水果的预制体
        }
        //-----水果是否需要缩放
        // let isScale =sData.isScale;
        // let ScaleController =fruit.getComponent("ScaleController");
        // ScaleController.setData(sData.big,sData.small); //缩放范围随机
        // ScaleController.setIsScale(isScale,sData.t1,sData.t2);  //是否需要缩放

        typeData =cc.HQ.Local.fruitType[temp2Data];
        fruit.parent = parentNode; // 将生成的加入节点树
        fruit.getComponent("fruit").init(this,typeData,level,fruitPool);
        fruit.setPosition(tempData.x,tempData.y);
    },

    //发送飞镖
    onRegisteredEvent: function(){
        var level =cc.HQ.Local.CurrentLevels%401 ?cc.HQ.Local.CurrentLevels%401 :1;
        console.log("不能发送飞镖？？",cc.HQ.GameMgr.isInitGame);
        if(!cc.HQ.GameMgr.isInitGame){
            return;
        }
        // let localData =cc.HQ.Local.levelsData[level];
        // let curData =localData[cc.HQ.Local.swampyNum];
        // let temp =cc.HQ.Local.onFruitPos(curData.posData);
        let temp = TableReader.getFruitArray(level, cc.HQ.Local.SmallLevel);
        console.log("@@@",this.rotatingNode.children.length,temp.count);
        console.log("1111",cc.HQ.GameMgr.isInitGame);
        
        if(this.rotatingNode.children.length <=temp.count){  //表示此关已过
            //进行下一关这中间时间，在此间隔是不允许发射小刀)
            cc.HQ.GameMgr.isInitGame =false;
        }
        if(cc.HQ.GameMgr.isInitGame){
            cc.HQ.GameMgr.isInitGame =false;
            cc.HQ.GameMgr.isGameOver =false;
            // if(cc.HQ.Local.weaponClones){
            //     //播放小刀飞出音效 (三飞镖音效)
            //     cc.HQ.soundMgr.playClickEffet(12);
            // }else{    
            //     //播放小刀飞出音效
            //     cc.HQ.soundMgr.playClickEffet(11);
            // } 
            //播放小刀飞出音效
            cc.HQ.soundMgr.playClickEffet(11);
            cc.HQ.Local.crashFruitNum =0;
            cc.HQ.GameMgr.launchNum++;
            //飞刀处理逻辑
            this.onManage();
        } 
    },

    //处理飞镖的逻辑
    onManage: function(){
        //   cc.HQ.Local.weaponClones =5;
        if(cc.HQ.Local.experWeapons){
            this.knifeNum.getComponent("sanlaunch").onLauch();
        }else if(cc.HQ.Local.weaponClones && cc.HQ.Local.weaponClones>0){   //不在体验关中，三飞镖才会消耗次数
            cc.HQ.Local.weaponClones--;
            this.knifeNum.getComponent("sanlaunch").onLauch();
            //显示三镖齐发还剩几发
            this.labSanNum.node.active =true;
            this.textBg.active =true;
            this.labSanNum.string ="X"+cc.HQ.Local.weaponClones;
            if(cc.HQ.Local.weaponClones<=0){
                this.labSanNum.node.active =false;
                this.textBg.active =false;
            }
        }else{
            this.knife.getComponent("launch").onLauch();
        }
        //皮肤体验次数
        if(cc.HQ.Local.weaponSkin >=0){
            cc.HQ.Local.weaponSkin--;
        }
    },

    //三镖齐发按钮
    onThreeDartBtn: function(){
        if(cc.HQ.Local.experWeapons || (cc.HQ.Local.weaponClones && cc.HQ.Local.weaponClones>0)){
            cc.HQ.Global.showTips(2,"体验中!",0.2);
            return;
        }
        //埋点
        // sdk.setSceneEvent("游戏中","点击","三镖齐发-点击领取按钮",{},null);
        let data ={"type":1,"title":"获得三镖齐发*5","content":"X   5","explain":"一次发射飞镖三发,极大提高命中!"};
        this.bouncedTips.getComponent("bouncedTipsUI").init(data);
        this.bouncedTips.active =true;
    },

    //特效皮肤武器按钮 (屠龙宝刀)
    onBaodaoBtn: function(){
        let data ={"type":3,"skinId":82,"title":"获得屠龙宝刀","content":"","explain":"宝刀屠龙，无双特效，限时体验!"};
        this.bouncedTips.getComponent("bouncedTipsUI").init(data);
        this.bouncedTips.active =true;    
    },

    //换武器按钮
    onGenghuWeaponBtn: function(){
        //隐藏banner广告
        var bannerAd = sdk.createBannerAd({});
        bannerAd.hide();
        //隐藏投诉按钮
        cc.HQ.GameMgr.feedbackButtonHide();
        //显示商城界面节点
        this.storeUI.getComponent("storeUI").initBegin(1);
        this.storeUI.active =true;    
        //埋点
        // sdk.setSceneEvent("游戏中","点击","换武器按钮",{},null);
    },

    //获得金币
    onAddGold: function(gold){
        cc.HQ.Local.curGold +=gold;    
        this.labGold.string = cc.HQ.Local.curGold;
    },

    //进行后台后，关闭所有声音
    doSomeThing: function(){
        cc.audioEngine.stopAll();
    },

    //三飞镖数量显示
    onSanKnifeShow: function(){
        console.log("三飞镖用没用完",cc.HQ.Local.weaponClones);
        if(cc.HQ.Local.weaponClones && cc.HQ.Local.weaponClones>0){
            this.knife.active =false;
            this.knifeNum.active =true;
            this.labSanNum.node.active =true;
            //显示三镖齐发还剩几发
            this.textBg.active =true;
            this.labSanNum.string ="X"+cc.HQ.Local.weaponClones;
        }else{
            // console.log("111111111111111");
            if(!this.knife.active){
                this.knife.active =true;
                this.knifeNum.active =false;
                this.threeDartBtn.active =true;  //三镖齐发按钮显示
                this.labSanNum.node.active =false;
                this.textBg.active =false;
            }
        }
        // cc.HQ.GameMgr.isInitGame =true;
    },
 
    // update (dt) {},
});
