//弹金币框脚本
var TableReader =require("TableReader");

cc.Class({
    extends: cc.Component,

    properties: {
        labNum: cc.Label,  //获得的金币数
        labGold: cc.Label,  //主页上用户拥有的金币     
        content: cc.Node,  //内容节点 
        gameBg1: cc.Node,
        gameBg2: cc.Node, 
        bgIcon: cc.Node,  //奖励物品背景
        jinbiIcon: cc.Sprite,  //奖励的物品
        sp: cc.SpriteFrame,
        knifeIcon: cc.Sprite,  //主界面上飞刀皮肤图片
        _data: null,
        _type: null,  //弹框类型不同, 1表示积分樯获得的东西，2表示添加小程序获得的东西
        _tempGold: null,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    init: function(data,type,tempGold){
        this._data =data;
        this._type =type;
        this._tempGold =tempGold;
    },

    onEnable: function(){
        var actionScaleIn = cc.sequence(cc.scaleTo(0.3, 1.05, 1.05).easing(cc.easeCubicActionOut()),cc.scaleTo(0.1, 1, 1).easing(cc.easeCubicActionOut()));
        this.content.scale = 0.5;
        this.content.runAction(actionScaleIn);

        let zongGold =0;
        let gold =cc.HQ.GameMgr.getGold(); //显示用户金币数 
        if(this._type ==1 ||this._type ==3 ||this._type ==5 ||this._type ==6){
            this.gameBg1.active =true;
            this.gameBg2.active =false;
            this.bgIcon.active =true;
            this.jinbiIcon.spriteFrame =this.sp;
            //显示获得的金币数
            if(this._tempGold>=0){
                this.labNum.string ="+"+this._tempGold;
                //用户拥有的金币数加上领取的金币数 
                zongGold =Number(gold)+this._tempGold;
            }else{
                this.labNum.string ="+1000";   
                //用户拥有的金币数加上领取的金币数 
                zongGold =Number(gold)+1000;
            }
        }else if(this._type ==2){
            this.gameBg1.active =false;
            this.gameBg2.active =true; 
            this.labNum.string ="+8888";
            zongGold =Number(gold)+8888;

            let arr =cc.HQ.GameMgr.getKnife();   //用户已拥有的飞刀
            arr =cc.HQ.GameMgr.onChangeData(arr);
            if(arr.indexOf(61) ==-1){
                arr.push(61);
                cc.HQ.GameMgr.setKnife(arr);
                //当前装备的飞刀皮肤
                cc.HQ.GameMgr.setCurKnife(61);
                cc.HQ.Global.showTips(2,"恭喜获得极品飞镖!",0.3);
            }else{
                cc.HQ.Global.showTips(2,"你已拥有此极品飞镖!",0.3);
            }
        }else{
            console.log("签到奖励",this._tempGold);
            //签到奖励
            this.gameBg1.active =true;
            this.gameBg2.active =false;
            this.bgIcon.active =false;
            if(typeof(this._tempGold)=="string"){
                //奖励的飞镖皮肤图片显示
                let tempData =TableReader.getTableRowByNum("knifeItems",Number(this._tempGold));
                cc.HQ.GameMgr.onReplaceKnife(this.jinbiIcon,tempData);
                this.labNum.string ="超级奖励";
                zongGold =Number(gold);
            }else{
                this.jinbiIcon.spriteFrame =this.sp;
                this.labNum.string ="+"+this._tempGold;
                //奖励金币
                zongGold =Number(gold)+this._tempGold;
            }
        }
        cc.HQ.GameMgr.setGold(zongGold);
        this.labGold.string =zongGold;
    },

    onSure: function(){
        if(this._type ==1){
            cc.HQ.Global.showTips(2,"已领取1000金币!",1.5);
            //埋点
            sdk.setSceneEvent("试玩领金币","点击",this._data.name+this._data.coin+"按钮-领取成功",{},null);
        }else if(this._type ==2){
            cc.HQ.Global.showTips(2,"已领取8888金币与极品飞镖!",1.5);
            let skin =null;
            let userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
            skin =TableReader.getTableRowByNum("knifeItems",Number(userCurWeapan));
            //更换飞刀图片
            cc.HQ.GameMgr.onReplaceKnife(this.knifeIcon,skin);
            //埋点
        }else if(this._type ==3){
            cc.HQ.Global.showTips(2,"已领取离线收益金币!",1.5);     //离线金币、
            //埋点
        }else if(this._type ==4){
            if(typeof(this._tempGold)=="string"){
                let arr =cc.HQ.GameMgr.getKnife();   //用户已拥有的飞刀
                arr =cc.HQ.GameMgr.onChangeData(arr);
                if(arr.indexOf(Number(this._tempGold)) ==-1){
                    arr.push(Number(this._tempGold));
                    cc.HQ.GameMgr.setKnife(arr);
                    //当前装备的飞刀皮肤
                    cc.HQ.GameMgr.setCurKnife(this._tempGold);
                    let skin =null;
                    let userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
                    skin =TableReader.getTableRowByNum("knifeItems",Number(userCurWeapan));
                    //更换飞刀图片
                    cc.HQ.GameMgr.onReplaceKnife(this.knifeIcon,skin);
                    cc.HQ.Global.showTips(2,"恭喜获得超级奖励!",1);
                }else{
                    cc.HQ.Global.showTips(2,"你已拥有此飞镖!",1);
                }  
            }    
        }else if(this._type ==5){
            cc.HQ.Global.showTips(2,"已领取奖励金币!",1.5);     //炫耀有奖金币
            //炫耀有奖分享成功后，加到首页
            var settleJS =cc.find("Canvas").getChildByName("settlementUI").getComponent("settlementUI");
            settleJS.onHome();
        }else{
            //胜利界面奖励翻倍后，开始下一关
            var victoryJS =cc.find("Canvas").getChildByName("victoryUI").getComponent("victoryUI");
            victoryJS.onNextPass();
        }
        this.node.active =false;
    },

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