/* @flow */
import Path from './history-path'
import Store from './storage'
import { _history } from './history'

const Time = window.performance && window.performance.now
    ? window.performance
    : Date

function genKey() {
    return Time.now().toFixed(3)
}

export default class RouterStorage {

}

RouterStorage.install = function (Vue, option) {
    if (this.isInstall) {
        if (process.env.NODE_ENV == 'development')
            console.log('installed')
        return;
    }
    this.isInstall = true;

    Store.Resolve()

    Object.defineProperty(Vue.prototype, '$history', {
        get() { return _history }
    })

    Vue.mixin({
        created() {
            var vm = this.$root;

            if (_history.enterPath != '') return;

            if (!vm.$router || !vm.$route) {
                if (process.env.NODE_ENV == 'development')
                    console.warn('[HistoryStorage]:Please used VueRouter first.')
                return;
            }

            //先获得访问vue页面时的路径
            _history.enterPath = vm.$route.fullPath;
            _history.base = '/' + vm.$router.options.base;
            if (process.env.NODE_ENV == 'development')
                console.log('EnterPath:' + _history.enterPath)
            if (_history.enterPath == '/') {
                Store.Clear();
            }
            else {
                history.replaceState({ key: -1 }, '', _history.base + '/root');
                history.pushState({ key: genKey() }, '', _history.base);
                //有历史记录
                if (_history.routes.length > 0) {
                    for (var idx = 0; idx < _history.routes.length; idx++) {
                        history.pushState({ key: genKey() }, '', _history.base + _history.routes[idx]);
                    }
                    //进来时的路径与保存的历史记录中的最后一个不相同,追加
                    if (_history.routes[_history.routes.length - 1] !== _history.enterPath) {

                        _history.routes.push(_history.enterPath);
                        history.pushState({ key: genKey() }, '', _history.base + _history.enterPath);
                        Store.Save()
                    }
                }
                else if (vm.$route.fullPath !== '' && vm.$route.fullPath !== '/') {
                    if (process.env.NODE_ENV == 'development')
                        console.log('resolve routers from routeMatched')
                    for (var idx = 0; idx < vm.$route.matched.length - 1; idx++) {
                        var path = vm.$route.matched[idx].fullPath || vm.$route.matched[idx].path;
                        history.pushState({ key: genKey() }, '', _history.base + path);
                        _history.routes.push(path);
                    }
                    history.pushState({ key: genKey() }, '', _history.base + vm.$route.fullPath);
                    _history.routes.push(vm.$route.fullPath);
                    Store.Save();
                }
                _history.beforeState = history.state;
            }

            vm.$router.beforeEach((to, from, next) => {
                if (process.env.NODE_ENV == 'development') {
                    console.log('befaultState:' + JSON.stringify(_history.beforeState));
                    console.log('currentState:' + JSON.stringify(history.state));
                    console.log('to:' + to.path);
                }
                if (to.path == '/' || history.state && _history.beforeState && history.state.key && Number(_history.beforeState.key) > Number(history.state.key)) {
                    if (history.state.key == -1) {
                        if (process.env.NODE_ENV == 'development')
                            console.log('Is root,can\'t back!')
                        next(false);
                    } else {

                        if (process.env.NODE_ENV == 'development')
                            console.log('go back')
                        vm.$emit('goback')
                        //后退
                        _history.routes.pop();
                        next();
                    }
                }
                else {
                    if (process.env.NODE_ENV == 'development')
                        console.log('go forward')
                    vm.$emit('goforward')
                    next();
                    //前进
                    _history.routes.push(to.fullPath);
                    // if (typeof history.state != 'number')
                    //     history.replaceState({ key: genKey() }, '', to.fullPath)
                }
                _history.beforeState = history.state;
                Store.Save()
            })
        }
    })

    Vue.component('history-path', Path)
}

if (window.Vue) {
    window.Vue.use(RouterStorage)
}

@@ -1336,32 +1336,88 @@
          else %7B%0A
+                //%E5%9C%A8%E6%A0%B9%E8%B7%AF%E5%BE%84%E5%89%8D%E5%A4%9A%E5%8A%A0%E4%B8%80%E4%B8%AA%E8%AE%B0%E5%BD%95%EF%BC%8C%E9%98%B2%E6%AD%A2%E9%80%80%E5%90%8E%E6%97%B6%E8%B7%B3%E5%87%BAVue%EF%BC%8C%E6%97%A0%E6%B3%95%E5%86%8D%E6%89%A7%E8%A1%8C%E5%88%A4%E6%96%AD%E5%92%8C%E7%A6%81%E6%AD%A2%E5%90%8E%E9%80%80%E6%93%8D%E4%BD%9C%0A
                 
@@ -3127,32 +3127,35 @@
 %0A               
+ //
  if (process.env
@@ -3191,32 +3191,35 @@
 %0A               
+ //
      console.log
@@ -3284,36 +3284,39 @@
                 
+//
     
+ 
 console.log('cur
@@ -3368,32 +3368,35 @@
 %0A               
+ //
      console.log
@@ -3421,32 +3421,35 @@
 %0A               
+ //
  %7D%0A             
@@ -4447,153 +4447,8 @@
 h);%0A
-                    // if (typeof history.state != 'number')%0A                    //     history.replaceState(%7B key: genKey() %7D, '', to.fullPath)%0A
     

修复刷新操作后，后退到根路径时不能禁止再后退的问题（但前进的历史记录会丢失，将在下个版本修复）