//水果预制体脚本
var TableReader =require("TableReader");

cc.Class({
    extends: cc.Component,

    properties: {
        chengzi: cc.Node,  //橙子水果节点
        skeletoNode: cc.Node,
        skeletoNodec: cc.Node,
        skeletoNode1: cc.Node,
        _parentJS: null,  //Game脚本的this
        _fruitData: null,  //水果自己的数据
        _levels: null,  //当前关卡数
        _fruitPool: null,  //对象池
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    init: function(parentJS,typeData,level,fruitPool){
        this._parentJS = parentJS;
        this._fruitData =typeData;
        this._levels =level;
        this._fruitPool =fruitPool;
        //水果的初始化
        this.chengzi.active =true;
        this.skeletoNode1.active =false;
        this.node.getComponent(cc.CircleCollider).enabled =true;
    },

    //发生碰撞后的回调函数
    onCollisionEnter: function (other,self) {
        // console.log("66666666666");
        cc.HQ.GameMgr.isGameOver =true;
        //调起手机振动
        if(cc.HQ.shakeState){
            cc.HQ.GameMgr.shortPhoneShake();
        }
        //播放切开水果的音效
        cc.HQ.soundMgr.playClickEffet(this._fruitData.effect);
        cc.HQ.Local.crashFruitNum++;

        //播放击中水果后的一些动画特效
        this.onPlaying(cc.HQ.Local.crashFruitNum);
        
    },

    //播放击中水果后的一些动画特效(击中水果，切开水果，水果进入杯中，装满果汁)
    onPlaying: function(goldNum){
        //水果击中时的位置处理
        this.onAngle();
        //水果上的特效
        var skeletoNode =this.skeletoNode.getComponent(sp.Skeleton);
        var skeletoNode1 =this.skeletoNode1.getComponent(sp.Skeleton);
      
        //击中水果特效
        skeletoNode.animation =this._fruitData.spineAnim1;

        //果汁爆开动画
        skeletoNode.setEventListener(function(){
            // console.log("果汁爆开特效");
            this.chengzi.active =false;
            this.node.getComponent(cc.CircleCollider).enabled =false;
            //切开水果特效
            this.skeletoNode1.active =true;
            skeletoNode1.animation =this._fruitData.spineAnim3;
            //切开水果所得金币
            this._parentJS.onAddGold(goldNum);

            // let localData =cc.HQ.Local.levelsData[this._levels];
            // let curData =localData[cc.HQ.Local.swampyNum];
            // let temp =cc.HQ.Local.onFruitPos(curData.posData);
            let temp = TableReader.getFruitArray(this._levels, cc.HQ.Local.SmallLevel);  //小关卡拥有的水果数
            let levelsInfo = TableReader.getTableRowById("Levels", this._levels);  //大关卡中拥有的小关数
            //水果切开动画
            skeletoNode1.setEventListener(function(){
                // console.log("弹出显形动画");
                this.node.runAction(cc.sequence(cc.moveTo(0.43,cc.p(-260,-592)),cc.callFunc(function(){
                    let childNum =this._parentJS.rotatingNode.children.length;
                    //回收水果节点
                    this.onRecyle(this.node,childNum,temp.count);
                    //杯子中的果汁数量展示
                    let spJuice =this._fruitData.juiceColor-1;
                    this._parentJS.waterCupUI.getComponent("waterCupUI").init(spJuice,temp.count);
                
                    console.log("不过关？？",this._parentJS.rotatingNode.children.length,temp.count);
                    let juiceLen =this._parentJS.waterCupUI.getComponent("waterCupUI").juiceLayout.children.length;
                    if(this._parentJS.rotatingNode.children.length== temp.count && juiceLen >= temp.count){   //&& juiceLen == temp.count
                        //下一关卡
                        this.onNextLevel();
                    }else{
                        if(!cc.HQ.Local.experWeapons){   //若不是体验关卡时，
                            //三飞镖齐飞数量显示
                            // this.onSanKnifeShow();
                        }
                        // cc.HQ.GameMgr.isInitGame =true;
                    }

                    //若皮肤体验次数为0,
                    if(cc.HQ.Local.weaponSkin <=0){
                        //显示体验次数还有几次
                        // let userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
                        let userCurWeapan =cc.HQ.GameMgr.onSetCurrSkin();  //显示当前装备的飞刀
                        let skin =TableReader.getTableRowByNum("knifeItems",userCurWeapan);
                        //是否显示飞刀皮肤特效
                        
                        //更换飞刀图片
                        if(cc.HQ.Local.weaponClones>0){
                            let sanKnife =this._parentJS.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.isInitGame =true;
                            cc.HQ.GameMgr.onReplaceKnife(this._parentJS.knifeSp,skin);  
                            if(cc.HQ.GameMgr.onCheckStarand(userCurWeapan)){   //特定的飞刀就显示飞刀特效
                                this._parentJS.pumpNode.active =true;
                                this._parentJS.pumpNode1.active =true;
                            }else{
                                this._parentJS.pumpNode.active =false;
                                this._parentJS.pumpNode1.active =false;  
                            }
                        }
                    }
                }.bind(this))));
            }.bind(this)); 

            //关卡进度条显示
            this._parentJS.passProgressBar.progress += (1 / temp.count)/ levelsInfo.chirldLevel.length;
        }.bind(this)); 
    },

    onAngle:function(){
        var angle = (this._parentJS.rotatingNode.rotation%360)*(-1);
        angle = Math.PI/180 * angle;
        var posx = this.node.position.x*Math.cos(angle)  - this.node.position.y*Math.sin(angle);
        var posy = this.node.position.x*Math.sin(angle) + this.node.position.y*Math.cos(angle);
        this.node.setPosition(cc.p(posx,posy));
        this.node.parent =this._parentJS.masterNode;
    },

    //下一关
    onNextLevel: function(){
        cc.HQ.GameMgr.isInitGame =false;
        //水果底图点预制体节点回收
        this.onRecylePoint();
    
        this.scheduleOnce(function(){
            // cc.HQ.Local.swampyNum++;
            if(!cc.HQ.Local.experWeapons){   //若不是体验关卡时，
                //三飞镖齐飞数量显示
                // this.onSanKnifeShow();
            }
            let levelsInfo = TableReader.getTableRowById("Levels", this._levels);
            if(levelsInfo.chirldLevel.length ==cc.HQ.Local.SmallLevel){
                cc.HQ.Local.SmallLevel =1;
                console.log("小关过完了");
                // cc.HQ.Local.swampyNum++;
                cc.HQ.Local.CurrentLevels++;
                if(cc.HQ.Local.weaponClones <=0){  //当是三飞镖时不弹三飞镖或皮肤弹框
                    if(cc.HQ.Local.CurrentLevels && cc.HQ.Local.CurrentLevels%5 ==1){
                        console.log("技能推送");
                        //收回免费给你体验的三飞镖，用回一飞镖
                        this.onFreeCancel();
                        if(!sdk.getConfig4().ipLimit){  //ip限制
                            if(sdk.getConfig1().sns){
                                let data ={"type":1,"title":"体验结束，继续使用三镖齐发","content":"X   5","explain":"一次发射飞镖三发,极大提高命中!"};
                                this._parentJS.bouncedTips.getComponent("bouncedTipsUI").init(data);
                                this._parentJS.bouncedTips.active =true;
                            }
                        }
                    }else{   
                        if(cc.HQ.Local.CurrentLevels ==2 || cc.HQ.Local.CurrentLevels ==4){
                            // console.log("皮肤一定给到",cc.HQ.Local.CurrentLevels);
                            if(!sdk.getConfig4().ipLimit){  //ip限制
                                if(sdk.getConfig1().sns){
                                    //皮肤推送
                                    this.onRandomSkin(true);
                                }
                            }
                        }else if(cc.HQ.Local.CurrentLevels && cc.HQ.Local.CurrentLevels%5 !=4){  //进入体验关的前一关，让它不要弹皮肤框
                            if(!sdk.getConfig4().ipLimit){  //ip限制
                                if(sdk.getConfig1().sns){
                                    //皮肤随机推送
                                    this.onRandomSkin(false);
                                }
                            }
                        }
                    }
                }

                if(!sdk.getConfig4().ipLimit){  //ip限制
                    if(sdk.getConfig1().sns){
                        //一刀切奖励推送
                        if(cc.HQ.GameMgr.isACut){
                            this._parentJS.yidaqieTipsUI.getComponent("yidaqieTipsUI").init(200);
                            this._parentJS.yidaqieTipsUI.active =true;
                        }
                    }
                }

                //推送主线皮肤奖励
                let taskId =cc.HQ.GameMgr.onCheckTaskSuccess();
                //主线通关皮肤推送弹框
                if(taskId){
                    this._parentJS.mainTaskTipsUI.getComponent("mainTaskTipsUI").init(taskId);
                    this._parentJS.mainTaskTipsUI.active =true;
                }

                //把果汁送出去
                this.onGrass(0);
                //显示胜利界面
                this._parentJS.victoryUI.active =true;
                this._parentJS.node.active =false;
                // cc.HQ.Local.swampyNum =0;
            }else{
                cc.HQ.Local.SmallLevel++;  //子关数++ 
                console.log("小关还没过完");
                this.onGrass(1);
            }
            //保存用户关卡数  (每过一大关)
            cc.HQ.GameMgr.setPassNum(cc.HQ.Local.CurrentLevels);

        }.bind(this),0.45);
    },

    //皮肤随机推送
    onRandomSkin: function(value){
        console.log("皮肤推送");
        let ran =null;
        if(value){
            ran =1;
        }else{
            ran =Math.random()>0.7 ? 1 :0;
        }
        if(ran){
            //皮肤默为当前并没拥有的皮肤中随机
            let ranArr =["17","18","19","20","21","22","23","24","25","26","27","28","29","30",
            "31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","93","94","95","96"];
            let arr =cc.HQ.GameMgr.getKnife();    //用户已拥有的飞刀
            if(typeof(arr)=="string"){
                arr =arr.split(",");
            }
            console.log("皮肤推送11111",arr);
            for (let i = 0; i < arr.length; i++) {
                let tempNumber = arr[i];
                for (let j = 0; j < ranArr.length; j++) {
                    if(ranArr[j] == tempNumber) {
                        ranArr.splice(j, 1);
                        break;
                    }
                }
            }
            console.log("皮肤随机",ranArr);
            if(ranArr.length){
                cc.HQ.Global.randomSort(ranArr);
                console.log("一个皮肤",ranArr[0]);
                let data ={"type":2,"skinId":ranArr[0],"title":"获得体验武器*10","content":"X   10","explain":"新皮肤，新体验，点击永久解锁!"};
                this._parentJS.bouncedTips.getComponent("bouncedTipsUI").init(data);
                this._parentJS.bouncedTips.active =true;
            }
        }    
    },

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

    //果汁处理
    onGrass: function(state){
        let cdFun =function(){
            this._parentJS.labPass.string ="第"+(cc.HQ.Local.CurrentLevels)+"关";
            //生成下一关的水果布局
            this._parentJS.onCreateFruitLayout(cc.HQ.Local.CurrentLevels);
            this._parentJS.rotatingNode.setPosition(0,800);
            this._parentJS.rotatingNode.runAction(cc.moveTo(0.2,cc.p(0,250))); 
            cc.HQ.GameMgr.isInitGame =true;
        }.bind(this);

        //果汁送出去，再生成下一关
        this._parentJS.waterCupUI.runAction(cc.sequence(cc.spawn(cc.moveTo(0.4,cc.p(-475,-592)),cc.callFunc(function(){
            //果汁预制体节点回收
            this._parentJS.waterCupUI.getComponent("waterCupUI").onPutNode();
        }.bind(this))),cc.callFunc(function(){
            this._parentJS.waterCupUI.setPosition(-308,-592);
            let widget = this._parentJS.waterCupUI.getComponent(cc.Widget);
            widget.isAlignOnce =false;
            widget.isAlignLeft =true;
            widget.left =9;
            widget.updateAlignment();
            if(state){
                cdFun();
            }
        }.bind(this))));    
    },

    //收回免费给你体验的三飞镖，用回一飞镖
    onFreeCancel: function(){
        cc.HQ.Local.experWeapons =false;
        this._parentJS.knifeNum.active =false;
        this._parentJS.knife.active =true;

        // let userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
        let userCurWeapan =cc.HQ.GameMgr.onSetCurrSkin();  //显示当前装备的飞刀
        let skin =TableReader.getTableRowByNum("knifeItems",userCurWeapan);
        //更换飞刀图片
        cc.HQ.GameMgr.onReplaceKnife(this._parentJS.knifeSp,skin);
        
    },

    //回收  //放入水果对象池
    onRecyle: function(node,num,count){
        this._fruitPool.put(node);    

        if(num ==count &&  cc.HQ.GameMgr.launchNum ==1){
            cc.HQ.GameMgr.isACut =true;
            cc.HQ.Global.showToast();
        }
    },

    //水果底图点预制体节点回收   //放入水果对象池
    onRecylePoint: function(){
        var length =this._parentJS.rotatingNode.children.length;
        for(var i=length-1;i>=0;i--){
            cc.HQ.GameMgr.fruitPointPool.put(this._parentJS.rotatingNode.children[i]);    
        }
    },

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