// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

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: {

        //     type: cc.Node,
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.init();
    },

    barBaseW: null,
    barSpr: null,
    barSf: null,
    init() {
        this.bar = cc.find('bar', this.node);

        this.barBaseW = this.bar.width;
        this.barSpr = this.bar.getComponent(cc.Sprite);
        this.barSf = this.barSpr.spriteFrame;

        this.setProgress(0);
    },

    setProgress(progress) {

        setTimeout(() => {
            if (!this.barSf) {
                return;
            }
            const w = this.barBaseW * progress;

            const rect = this.barSf.getRect();
            rect.width = w;
            this.bar.width = w;
            this.barSpr.spriteFrame.setRect(rect);
        }, 1);
      
    },

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