let self =null;

cc.Class({
    extends: cc.Component,
    properties: {
        progressBar: cc.ProgressBar,  //进度条
        loadTip: cc.Label,  //进度显示
        logo:cc.Node, //动画节点
        _isload: 0,
    },

    // use this for initialization
    onLoad: function () {
        self =this;
        //游戏初始化
        this.initMgr();

        //微信登录
        sdk.WeChatLoginNoAuth((userinfo)=>{
            console.log('userinfo：', userinfo);
            // console.log("配置文件",sdk.getConfig1(),sdk.getConfig2(),sdk.getConfig3());
            //右上角分享
            cc.HQ.GameMgr.showShareMenu();
            //主域登录成功后先调这个接口保存用户的信息,发送给子域
            cc.HQ.GameMgr.set_sub_info();
            //把技术配置内容存入一个全局变量中
            cc.HQ.dConf =sdk.getConfig2();
            console.log("配置文件",sdk.getConfig4());

            //显示banner广告   (bannerID1)
            var bannerAd = sdk.createBannerAd({},sdk.getConfig2().bannerArr[0]);
            bannerAd.show();

            //加载资源，进度条显示  (真实进度条)
            cc.loader.onProgress =this.loadRes;
            cc.director.preloadScene("Game",function(){
                // cc.director.loadScene("Game");
            });
        },this);

        //获取碰撞检测系统
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;   //开启碰撞系统
        // manager.enabledDebugDraw = true;   //开启debug绘制
    },

    initMgr:function(){
        cc.HQ = {};//这是全局的公众变量
        //公共脚本
        cc.HQ.Global = require("Global");
        //游戏逻辑脚本
        cc.HQ.GameMgr = require("GameMgr");
        //本地配置脚本
        cc.HQ.Local = require("Local");
        //游戏中默认开启声音
        cc.HQ.soundMgr =require("SoundManage");
        cc.HQ.soundMgr.setAudio(true);
        //缓存
        cc.HQ.CacheManager = require("CacheManager");  
        //记录声音选择的状态(开启或关闭) 用个全局变量cc.HQ.suoundState来控制
        cc.HQ.suoundState =true;
        cc.HQ.shakeState =true;   //记录振动是否开启状态
        //弄个全局变量,用来控制是否显示群排行榜
        cc.HQ.showGroupRank =true;
        //动态加载预制体
        cc.HQ.Global.preLoadRes();
        //创建水果对象池
        cc.HQ.GameMgr.createFruit();


        //获取用户的关卡数
        let userCurPass =cc.HQ.GameMgr.getPassNum();  //当前用户所在的关卡数
        console.log("获取用户的关卡数",userCurPass);
        cc.HQ.Local.CurrentLevels =Number(userCurPass);
    },

    //加载Game场景的资源
    loadRes: function(completedCount,totalCount,item){
        if(totalCount ==0){
            return;
        }
        if(self._isload==1){
            return;
        }
        self.progressBar.progress +=completedCount / totalCount;
        self.loadTip.string ="Loading..." +Math.floor(completedCount / totalCount * 100) +"%";
        // self.logo.x = -244+(completedCount / totalCount)*470;
        self.logo.x = -417+(completedCount / totalCount)*470;
        
        if(completedCount / totalCount ==1){
            self._isload++;
            if(self._isload == 1){
                self.logo.x =242;
                cc.director.loadScene("Game");
            }
        }
    },

    // called every frame, uncomment this function to activate update callback

    update: function (dt) {
        if(this.progressBar.progress<0.4){
            this.progressBar.progress +=dt;
            let num =Math.floor(dt*1500) >45 ? 45 : Math.floor(dt*1500);
            this.loadTip.string ="Loading..." +num +"%";
            this.logo.x = -53;
        }
    },
});