// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
window.loadmanager = {}
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

        loadmanager = this
        //常驻节点
        cc.game.addPersistRootNode(this.node);
        this.initScene()
        //初始化加载进度监听
        this.initLoadListener()

        this.loading = UtilsNode.getNode("loading",this.node)
        this.bg = UtilsNode.getNode("bg",this.node)


    },

    initLoadListener() {
        // this.progressTag = false;
        // cc.loader.onProgress = function (completeCount, totalCount) {
        //     if (this.progressTag) {
        //         this.progress = (1 * completeCount / totalCount).toFixed(1);
        //         console.log("进度条",this.progress + '%');
        //         // if (this.progressNode != null) {
        //         //     this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
        //         // }
        //
        //     }
        //
        // }.bind(this);
    },
    initScene() {
        this.scene = ["config", "game"]
    },
    showLoading(b) {
        this.loading.active = b
        this.bg.active = b
    },
    goto(index,b) {
        this.showLoading(true);
        // utils.toLoadScene("config",function () {
        //     this.progressTag = true;
        // }.bind(this))
        var scene = this.scene[index]
        this.loading.getComponent(cc.ProgressBar).progress= 0
        cc.director.preloadScene(scene, (completedCount, totalCount, item) => {
            cc.log('加载进度显示');
            cc.log("completedCount = " + completedCount + ",totalCount=" + totalCount);
            cc.log('百分比：' + Math.floor(completedCount / totalCount * 100));
            this.loading.getComponent(cc.ProgressBar).progress = completedCount / totalCount;
            if (totalCount - completedCount == 0) {
                // if (b) {
                //     this.loading.active = false
                //     utils.toLoadScene(scene)
                // }
                this.showLoading(false);
                utils.toLoadScene(scene)
            }

        }, (error) => {
            console.log(error);
        });
    }
    // update (dt) {},
});
