//试玩游戏预制脚本

cc.Class({
    extends: cc.Component,

    properties: {
        icon: cc.Sprite,  //游戏图标
        labName: cc.Label,  //游戏名字
        labPrice: cc.Label,  //领取金币数
        playingBtn: cc.Node,
        startBtn: cc.Node, //
        // goldStacksTipsUI: cc.Node,  //领取金币节点
        _appId:null,
        _path:null,
        _sysId:null,
        _status:null,
        _saveTime: null,  //此游戏试玩存的时间
        _parentJS: null,  //shiwanUI脚本
        _data: null,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    start () {
        var self = this;
        cc.game.on(cc.game.EVENT_SHOW, function(){
            // console.log("resume.....");
            if(cc.HQ.GameMgr.clickShiwan){
                // console.log("当前试玩游戏名",cc.HQ.GameMgr.shiwanGameSysid);
                sdk.getFromServer("leavewhich"+cc.HQ.GameMgr.shiwanGameSysid,"leave",null,(d)=>{
                    if(d){
                        var nodePar = self.node.parent.getChildByUuid(d);
                        console.log("UUIDUUID",d);
                        sdk.getFromServer("leaveTime","Time",null,(d)=>{
                            console.log(d);
                            if(d == undefined){
                                // cc.HQ.Global.showTips(2,"已领取100金币!",1.5);
                                // console.log("HHHHHHHHHHHH",nodePar.getComponent("gameList"));
                                sdk.Post(sdk.ip2+"/tuijian/addtry",{"sysid":nodePar.getComponent("gameList")._sysId,"uid":sdk.getUser().uid},function(data){
                                    let tempData =nodePar.getComponent("gameList")._data;
                                    self._parentJS.goldStacksTipsUI.getComponent("goldStacksTipsUI").init(tempData,1);
                                    self._parentJS.goldStacksTipsUI.active =true;

                                    nodePar.getComponent("gameList").playingBtn.active =false;
                                    nodePar.getComponent("gameList").startBtn.active =true;
                                    self.node.parent.insertChild(nodePar,self.node.parent.childrenCount);
                                    self.node.parent.getComponent(cc.Layout).updateLayout();
                                })
                            }else{
                                cc.HQ.Global.showTips(2,"请完整体验游戏!",1.5);
                            }
                            cc.HQ.GameMgr.clickShiwan =false;
                        });

                        sdk.setToServer("leavewhich"+cc.HQ.GameMgr.shiwanGameSysid,"leave",null,2); 
                    }
                })
            }
        },this);
    },

    init: function(data,parentJS){
        this._parentJS =parentJS;
        this._data =data;
        // console.log("试玩游戏内容",this._data);
        //显示图片icon
        this.showSpHead(this.icon.node,data.iconImageUrl);
        this.labName.string =data.name;  //游戏名字
        this.labPrice.string ="+"+data.coin;
        this._saveTime =data.saveTime;
        this._appId = data.appId;
        this._path = data.path;
        this._sysId =data.sysid;
        this._status =data.status;
    },

    //显示图片
    showSpHead: function(node,avatarUrl){
        if (CC_WECHATGAME) {
            try {
                let image = wx.createImage();
                image.onload = () => {
                    try {
                        let texture = new cc.Texture2D();
                        texture.initWithElement(image);
                        texture.handleLoadedTexture();
                        node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                    } catch (e) {
                        cc.log(e);
                        node.active = false;
                    }
                };
                image.src = avatarUrl;
            }catch (e) {
                cc.log(e);
                node.active = false;
            }
        } else {
            cc.loader.load({
                url: avatarUrl, type: 'jpg'
            }, (err, texture) => {
                node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
            });
        }
    },

    onGamePlaying(){
        cc.HQ.GameMgr.clickShiwan =true;  //试玩点击
        // console.log("点击当前试玩游戏名",this._sysId);
        cc.HQ.GameMgr.shiwanGameSysid =this._sysId;
        sdk.getServerTime((d)=>{
            console.log("获取服务器时间",d.nowTime);
            sdk.setToServer("leaveTime","Time",d.nowTime,15);
            //游戏存储 1天，  7天
            if(this._saveTime ==1){
                sdk.setToServer("leavewhich"+this._sysId,"leave",this.node.uuid,86400);
            }else{
                sdk.setToServer("leavewhich"+this._sysId,"leave",this.node.uuid,-1);
            }
        });

        console.log("试玩",this._appId,this._path);
        cc.HQ.GameMgr.onPlayingOtherGame(this._appId,this._path);
        //埋点
        sdk.setSceneEvent("试玩领金币","点击",this._data.name+this._data.coin+"按钮",{},null);
    },

    onStart: function(){
        cc.HQ.GameMgr.onPlayingOtherGame(this._appId,this._path);
        //埋点
        sdk.setSceneEvent("试玩领金币","点击",this._data.name+this._data.coin+"按钮-领取成功-开始游戏",{},null);
    },
    // update (dt) {},
});
