//时钟队列
const timers = [];
//时钟类型
const TIMER_TYPE = {
    //超时时钟
    TIMEOUT: 1,
    //循环时钟
    INTERVAL: 2
};
var getCurrentRoute = () => {
    let pages = getCurrentPages();
    if (pages.length > 0) {
        return pages[pages.length - 1].__route__.split('?')[0];
    } else {
        return 'GLOBAL';
    }
};
//时钟统一处理器
const timerHandler = {
    setTimeout(func, microSeconds, isKeep) {
        if (typeof func !== 'function') {
            throw {
                message: 'setTimeout第一个参数需要传入一个function'
            };
        }
        let ms = microSeconds || 0;
        let keep = isKeep === true ? true : false;

        let timer = setTimeout(func, ms);

        timers.push({
            //创建的时钟
            timer: timer,
            //是否要全局保留
            isKeep: keep,
            //时钟类型
            type: TIMER_TYPE.TIMEOUT,
            //在哪个页面上创建的定时器
            owner: getCurrentRoute()
        });
        return {
            //创建的时钟
            timer: timer,
            //是否要全局保留
            isKeep: keep,
            //时钟类型
            type: TIMER_TYPE.TIMEOUT,
            //在哪个页面上创建的定时器
            owner: getCurrentRoute()
        };
    },
    setInterval(func, microSeconds, isKeep) {
        if (typeof func !== 'function') {
            throw {
                message: 'setInterval第一个参数需要传入一个function'
            };
        }
        let ms = microSeconds || 0;
        let keep = isKeep === true ? true : false;

        let timer = setInterval(func, ms);

        timers.push({
            //创建的时钟
            timer: timer,
            //是否要全局保留
            isKeep: keep,
            //时钟类型
            type: TIMER_TYPE.INTERVAL,
            //在哪个页面上创建的定时器
            owner: getCurrentRoute()
        });

        console.log('timers : ', timers);
        return {
            //创建的时钟
            timer: timer,
            //是否要全局保留
            isKeep: keep,
            //时钟类型
            type: TIMER_TYPE.INTERVAL,
            //在哪个页面上创建的定时器
            owner: getCurrentRoute()
        };
    },
    clearTimers() {
        /***
         * 拷贝原有的时钟队列到临时队列中，并清空原有的时钟队列
         */
        let oldTimers = [];
        timers.forEach(timer => {
            oldTimers.push(timer);
        });
        timers.length = 0;

        /***
         * 遍历临时时钟队列，将不保存并且不是本页面创建的计时器清除掉
         */
        let currentPage = getCurrentRoute();
        let timer = oldTimers.shift();

        while (timer) {
            //1、计时器不需要保存；2、非本页面创建的计时器
            if (!timer.isKeep && currentPage != timer.owner) {
                this.clearTimer(timer);
            } else {
                //不清除的时钟重新保存起来
                timers.push(timer);
            }
            timer = oldTimers.shift();
        }
    },
    clearTimer(timer) {
        if (timer && timer.type) {
            switch (timer.type) {
                case TIMER_TYPE.TIMEOUT:
                    clearTimeout(timer.timer);
                    break;
                case TIMER_TYPE.INTERVAL:
                    clearInterval(timer.timer);
                    break;
                default:
                    break;
            }
        }
    }
};


export default timerHandler;
