/**
 * scrollTo.js
 *
 * @Author: jruif
 * @Date: 2018/1/2 上午11:04
 */

import slideTo from './slideTo';

var noop = function noop() {};

/**
 * 滚动到某一位置
 * @param beginValue {number} 起点
 * @param endValue {number} 终点
 * @param scrollTime {number} 毫秒数
 * @param tweenName {string} 缓动动画: http://easings.net/zh-cn || https://github.com/chenglou/tween-functions
 * @param callback {function} 执行完动画后到回调
 */
var scrollTo = function scrollTo(beginValue, endValue, scrollTime) {
    var tweenName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'easeOutQuint';
    var callback = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : noop;
    var _window = window,
        pageXOffset = _window.pageXOffset,
        scrollTo = _window.scrollTo;
    // const totalDuration = scrollTime * 60 / 1000; // 1s 60fps
    // let currentTime = 1;
    // tweenFunction[tweenName](currentTime, beginValue, endValue, totalDuration)
    // const run = () => {
    //     const tweenValue = tweenFunction[tweenName](currentTime++, beginValue, endValue, totalDuration);
    //     if (handleRAF) {
    //         cancelAnimationFrame(handleRAF);
    //     }
    //     scrollTo(pageXOffset, tweenValue);
    //     if (tweenValue > endValue) {
    //         handleRAF = requestAnimationFrame(run);
    //     } else {
    //         setTimeout(callback, 50);
    //     }
    // };
    // run();

    slideTo({
        beginValue: beginValue,
        endValue: endValue,
        time: scrollTime,
        tweenName: tweenName,
        stepCallback: function stepCallback(value) {
            scrollTo(pageXOffset, value);
        },
        endCallback: callback
    });
};

export default scrollTo;