import _classCallCheck from "babel-runtime/helpers/classCallCheck";
import _createClass from "babel-runtime/helpers/createClass";

var LoadProgress = function () {
    function LoadProgress(requestNum, onChange, onFinish) {
        _classCallCheck(this, LoadProgress);

        this.onChange = onChange;
        this.onFinish = onFinish;
        this.progress = 0;
        this.requestNum = requestNum > 100 ? 100 : requestNum;
        this.finishNum = 0;
        this.share = parseInt(100 / this.requestNum, 10);
        if (requestNum <= 0) {
            this.progress = 100;
            this.onProgressChange();
        }
    }

    _createClass(LoadProgress, [{
        key: "onProgressChange",
        value: function onProgressChange() {
            if (this.progress === 100 && this.incProgress) {
                clearInterval(this.incProgress);
                this.onFinish();
            }
            return this.onChange && this.onChange(this.progress);
        }
    }, {
        key: "start",
        value: function start() {
            var _this = this;

            if (this.progress < 100) {
                this.incProgress = setInterval(function () {
                    if (_this.progress < _this.share * (_this.finishNum + 1) - 1 && _this.progress < 99) {
                        _this.progress = _this.progress + 1;
                        _this.onProgressChange();
                    }
                }, 100);
            }
        }
    }, {
        key: "inc",
        value: function inc() {
            if (this.finishNum < this.requestNum) {
                this.finishNum++;
                this.progress = this.share * this.finishNum;
                this.onProgressChange();
            }
        }
    }, {
        key: "done",
        value: function done() {
            this.finishNum = this.requestNum;
            this.progress = 100;
            this.onProgressChange();
        }
    }]);

    return LoadProgress;
}();

export default LoadProgress;