//转盘界面节点脚本
var TableReader =require("TableReader");

cc.Class({
    extends: cc.Component,

    properties: {
        knife:cc.Node,  //飞镖节点
        again:cc.Node,  //再射一次按钮节点
        jiangli:cc.Node,  //奖励节点
        next:cc.Node,  //下一关按钮节点
        homeBtn: cc.Node,  //主页面按钮节点
        startBtn: cc.Node,  //开始按钮
        // mask:cc.Node,
        spriteArr: [cc.SpriteFrame], 
        pic: cc.Sprite,
        labNum: cc.Label, 
        labGold: cc.Label,  //用户拥有的金币
        passProgressBar: cc.ProgressBar,  //关卡进度条
        labPass: cc.Label,  //关卡数显示
        lauchNodeUI: cc.Node,  //游戏界面节点
        homePageUI: cc.Node,  //首页界面节点
        rotatingNode: cc.Node,  //旋转节点
        zhuanPanUI: cc.Node,  //转盘界面节点
        weaponIcon: cc.Sprite, //显示要抽的皮肤
        videoIcon: cc.Node,  //看视频图标
        _knifePos: null,  //飞镖位置
        _ranArr: [],  //转盘可抽的武器皮肤
      
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.director.getPhysicsManager().enabled = true; //打开物理世界
        this._knifePos = this.knife.getPosition();
        //显示拥有的金币
        let userGold =cc.HQ.GameMgr.getGold();
        this.labGold.string =userGold;
    },

    onEnable: function(){
        //再射一次按钮上，要不要显示视频图片
        let state =cc.HQ.GameMgr.onShareVideoState(sdk.getConfig2().model.v5);
        if(state){
            this.videoIcon.active =true;
        }else{
            this.videoIcon.active =false;
        }
        this.startBtn.active =true;
        this.knife.active = false;
        //显示要抽的皮肤
        this.onRandomSkin();
        //还原默认设置
        this.again.active = false;
        this.jiangli.active = false;
        this.next.active = false;
        this.homeBtn.active =false;
        this.getComponent(cc.RigidBody).awake = true;
    },

    //开始转盘游戏
    onStartBtn: function(event){
        event.target.active = false;
        this.knife.setPosition(this._knifePos);
        this.knife.rotation = 0;
        this.knife.active = true;
        var seq = cc.repeatForever(cc.rotateBy(2,360));
        this.node.runAction(seq);
    },

    //发射飞镖
    onKnifeLaunch:function(){
        if(this.knife.active == true && this.knife.rotation == 0){
            this.knife.getComponent("zhuanPanKnife").onLauch();
        }
    },

    //碰撞回调
    onBeginContact: function (contact, selfCollider) {
        console.log("碰撞刚体");
        // console.log(this.node.group,this.getComponent(cc.RigidBody).awake);
        this.node.stopAllActions();
        var rotate_now = this.node.rotation%360;
        var temp =0;
        if(rotate_now<55&&rotate_now>=0){
            // console.log("20");
            this.pic.spriteFrame = this.spriteArr[0];
            this.labNum.string = "+20";
            temp =20;
        }
        if(rotate_now<=90&&rotate_now>=55){
            // console.log("50");
            this.pic.spriteFrame = this.spriteArr[0];
            this.labNum.string = "+50";
            temp =50;
        }
        if(rotate_now<160&&rotate_now>90){
            // console.log("10");
            this.pic.spriteFrame = this.spriteArr[0];
            this.labNum.string = "+10";
            temp =10;
        }
        if(rotate_now<=180&&rotate_now>=160){
            // console.log("刀");
            // this.pic.spriteFrame = this.spriteArr[1];
            let tempData =TableReader.getTableRowByNum("knifeItems",this._ranArr[0]);
            if(!tempData){
                this.pic.spriteFrame = this.spriteArr[1];
            }else{
                //飞镖皮肤图片显示
                cc.HQ.GameMgr.onReplaceKnife(this.pic,tempData);
                this.labNum.string  = "永久解锁";
                temp =0;
            }
        }
        if(rotate_now>180&&rotate_now<235){
            // console.log("20s");
            this.pic.spriteFrame = this.spriteArr[0];
            this.labNum.string = "+20";
            temp =20;
        }
        if(rotate_now>=235&&rotate_now<=270){
            // console.log("100");
            this.pic.spriteFrame = this.spriteArr[0];
            this.labNum.string= "+100";
            temp =100;
        }
        if(rotate_now>270&&rotate_now<325){
            // console.log("10s");
            this.pic.spriteFrame =this.spriteArr[0];
            this.labNum.string = "+10";
            temp =10;
        }
        if(rotate_now>=325&&rotate_now<360){
            // console.log("80");
            this.pic.spriteFrame = this.spriteArr[0];
            this.labNum.string = "+80";
            temp =80;
        }
        
        // this.knife.active = false;
        if(sdk.getConfig1().sns){
            this.again.active = true;
        }else{
            this.again.active = false;
        }
        this.jiangli.active = true;
        this.next.active = true;
        this.homeBtn.active =true;
        // this.mask.active = true;

        this.knife.runAction(cc.sequence(
            cc.moveBy(0.05, 0, 5),
            cc.moveBy(0.05, 0, -5),
        ));
        this.node.runAction(cc.sequence(
            cc.moveBy(0.05, 0, 5),
            cc.moveBy(0.05, 0, -5),
        ));

        //转盘获得提示
        if(temp){
            cc.HQ.Global.showTips(2,"恭喜获得"+temp+"金币!",0.3);
            //显示获得的金币
            let userGold =cc.HQ.GameMgr.getGold();
            let tempGold =temp +Number(userGold);
            //保存用户拥有的金币
            cc.HQ.GameMgr.setGold(tempGold);
            this.labGold.string =tempGold;
        }else{
            let arr =cc.HQ.GameMgr.getKnife();   //用户已拥有的飞刀
            arr =cc.HQ.GameMgr.onChangeData(arr);

            if(arr.indexOf(this._ranArr[0]) ==-1){
                arr.push(this._ranArr[0]);
                cc.HQ.GameMgr.setKnife(arr);
                console.log("皮肤已存",arr);
                //当前装备的飞刀皮肤
                cc.HQ.GameMgr.setCurKnife(this._ranArr[0]);
                cc.HQ.Global.showTips(2,"恭喜获得新武器!",0.3);
            }else{
                cc.HQ.Global.showTips(2,"此武器你已拥有!",0.3);
            }
            
            //若获得了新的武器皮肤，转盘上的皮肤就要重新刷新
            // this.onRandomSkin();
        }
    },

    onRandomSkin: function(){
        //皮肤默为商城最后一页面转盘可抽的
        this._ranArr =[49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64];
        let arr =cc.HQ.GameMgr.getKnife();   //用户已拥有的飞刀
        arr =cc.HQ.GameMgr.onChangeData(arr);
        console.log("我已拥有的",arr);
        for (let i = 0; i < arr.length; i++) {
            let tempNumber = arr[i];
            for (let j = 0; j < this._ranArr.length; j++) {
                if(this._ranArr[j] == tempNumber) { 
                    this._ranArr.splice(j, 1);
                    break;
                }
            }
        }
        console.log("转盘可抽皮肤",this._ranArr);
        if(this._ranArr.length){
            cc.HQ.Global.randomSort(this._ranArr);
            console.log("一个皮肤",this._ranArr[0]);
            let tempData =TableReader.getTableRowByNum("knifeItems",this._ranArr[0]);
            //飞镖皮肤图片显示
            cc.HQ.GameMgr.onReplaceKnife(this.weaponIcon,tempData);
        }else{
            //没有皮肤可抽了
        }
    },

    //再射一次按钮
    onAgainBtn(){
        // this.knife.active = true;
        //分享或视频模式
        cc.HQ.GameMgr.shareOrVideo(sdk.getConfig2().model.v5,this.onVideo,this.onShare,1); 
        //埋点
        // sdk.setSceneEvent("转盘","点击","再射一次按钮",{},null);
    },

    //回到主页按钮
    onHome: function(){
        this.node.active =false;
        this.zhuanPanUI.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;
    },

    //下一关按钮
    onNextBtn: function(){
        this.node.active =false;
        this.zhuanPanUI.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)+"关";
    },

    //调视频
    onVideo: function(data,type,isValue){
        sdk.videoSuccess = function(){
            //视频成功处理逻辑
            var zhuanpanJS =cc.find("Canvas").getChildByName("zhuanPanUI").getChildByName("zhuanpan").getComponent("zhuanPanUI");
            //获得再射一次
            zhuanpanJS.knife.setPosition(zhuanpanJS._knifePos);
            zhuanpanJS.knife.rotation = 0;
            zhuanpanJS.knife.getComponent("zhuanPanKnife").onDaiji();
            zhuanpanJS.again.active = false;
            zhuanpanJS.jiangli.active = false;
            zhuanpanJS.next.active = false;
            zhuanpanJS.homeBtn.active =false;
            // this.mask.active = false;
            var seq = cc.repeatForever(cc.rotateBy(2,360));
            zhuanpanJS.node.runAction(seq);
            zhuanpanJS.getComponent(cc.RigidBody).awake = true;
            
            if(type =="cycle1"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="share";
                }
                zhuanpanJS.videoIcon.active =false;   
            }else if(type =="cycle2"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="video";
                }
                zhuanpanJS.videoIcon.active =true;
            }else if(type =="cycle3"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="video";
                }
                zhuanpanJS.videoIcon.active =true;
            }else if(type =="cycle4"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="share";
                }
                zhuanpanJS.videoIcon.active =false;
            }
            sdk.setToServer("VideoOrShareKey1","VideoOrShareData1",data,86400);
        }.bind(this);
        sdk.videoFail = function(){
            //视频失败处理逻辑
            console.log("视频未看完");
        };
        var videoAd = sdk.createRewardedVideoAd(sdk.getConfig2().videoIdArr[0]);
        videoAd.load().then(() => videoAd.show());
    },

    onShare: function(data,type,isValue){
        
        sdk.shareAppMessage({type:7,query:""});
        let shareFun4 = ()=>{
            console.log("老子响应的是转盘");
            var zhuanpanJS =cc.find("Canvas").getChildByName("zhuanPanUI").getChildByName("zhuanpan").getComponent("zhuanPanUI");
            //获得再射一次
            zhuanpanJS.knife.setPosition(zhuanpanJS._knifePos);
            zhuanpanJS.knife.rotation = 0;
            zhuanpanJS.knife.getComponent("zhuanPanKnife").onDaiji();
            zhuanpanJS.again.active = false;
            zhuanpanJS.jiangli.active = false;
            zhuanpanJS.next.active = false;
            zhuanpanJS.homeBtn.active =false;
            // this.mask.active = false;
            var seq = cc.repeatForever(cc.rotateBy(2,360));
            zhuanpanJS.node.runAction(seq);
            zhuanpanJS.getComponent(cc.RigidBody).awake = true;
            
            if(type =="cycle1"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="share";
                }
                zhuanpanJS.videoIcon.active =false;
            }else if(type =="cycle2"){
                if(isValue){
                    cc.HQ.dConf.sv.type ="video";
                }
                zhuanpanJS.videoIcon.active =true;
            }else if(type =="cycle3"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="video";
                }
                zhuanpanJS.videoIcon.active =true;
            }else if(type =="cycle4"){
                if(isValue){
                    cc.HQ.dConf.sv1.type ="share";
                }
                zhuanpanJS.videoIcon.active =false;
            }
            sdk.setToServer("VideoOrShareKey1","VideoOrShareData1",data,86400);
        }
        cc.HQ.GameMgr.clickShare =true; 
        cc.HQ.GameMgr.shareNum8++;
        cc.HQ.GameMgr.onSimulateShareBack(shareFun4,8);    
    },

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