import VueAdapt from "./lib/adapt"
import {bind,unbind,trigger} from "./lib/event"
import Logger from "./lib/logger"
import {extend} from "./lib/utils"
import {runInterceptor,registerInterceptor} from "./lib/interceptor"

let views = [],
    config={},
    VueRouter = VueAdapt.VueRouter;

//注册单个路由
function registerView(view) {
    views.push(view);
    return this;
}

//注册多个路由
function registerViews(viewArr) {
    viewArr.length && viewArr.forEach(view=>{
        views.push(view);
    })
    return this;
}

let Light = require("./lib/core");
extend(Light, {
    route:registerView,
    routes: registerViews,
    registerView,
    filter:registerInterceptor,
    runInterceptor,
    registerInterceptor,
    VueRouter:VueRouter,
    Logger,
    config(confi){
        this.extend(config,confi);
        return this;
    },
    /**
     *
     * @param options
     * @param options.el 挂载的父节点
     * @param options.routeMode 路由模式
     * @param options.routeMode 路由模式
     * @param options.viewport 设置运行页面的宽度（仅支持JSN）
     * @param cb
     */
    start(options,cb){
        const that = this;
        that._startNow(...arguments)
    },
    _startNow(options,cb) {
        const that = this;
        switch(arguments.length){
            case 0:
                cb = function () {
                };
                options = {};
                break;
            case 1:
                if(typeof options==="function"){
                    cb = options;
                    options = {};
                }else{
                    cb = function () {
                    };
                }
                break;
            case 2:
                break;
        }
        this.runInterceptor("start",[],function () {
            let viewCopy = [];
            viewCopy=viewCopy.concat(views);
            VueAdapt.initVueRouter({
                mode:options.routeMode||'hash'
            },viewCopy,(getRouter)=>{
                that.router = getRouter();
                that.getRouter = getRouter;
                require("./platform").start(that.router,options);
                trigger("appStarted");
                if(cb && typeof cb==="function") cb.call(null,that.router);
            });
        })
    },
    navigate(view, params, options){
        //navigate之前执行navigate拦截器,传递的值是当前视图，目标视图
        const that = this;
        let vCheck = views.filter(v=>v.path===`/${view}`)
        if(vCheck.length===1) vCheck=vCheck[0];
        else vCheck={}
        this.runInterceptor("navigate",[vCheck, params, options],function () {
            that._navigate.call(that,view, params, options)
        })
    },
    _navigate(view, params, options) {
        require("./platform").navigate(view, params, options);
    },
    /**
     * 路由回退
     *
     * @param number 要返回的页面层数，默认值为1
     */
    navigateBack: function (number) {
        //bug121649:number小于等于0时，不回退；与LightSDK.native.back()接口统一，增加提示。
        if(number == undefined) {
            number = 1;
        }
        if(!(/^[0-9]+$/.test(number))) {
            if(process.env.NODE_ENV === "dev") console.warn('[Light.navigateBack]number必须为字符串或整数类型的数字！');
            return;
        }
        if(number <= 0){
            if(process.env.NODE_ENV === "dev") console.warn('[Light.navigateBack]number的值必须大于或等于1！');
            return;
        }
        require("./platform").navigateBack(number);
    },
    inspect(){
        console.table(this);
    },
    bind,unbind,trigger,on:bind,off:unbind,extend,
    ajax(options){
        return require("./platform").ajax(options);
    },
    log(info){
        require("./platform").log(info);
        if(process.env.NODE_ENV==="dev"){
            //修复Uncaught TypeError: Illegal invocation(…)的问题
            console.log(info);
        }
    },
    install(Vue){
        this.Model = Vue;
        this.Vue = Vue;
        Vue.use(VueRouter);
        Vue.component("sub-view",Vue.component("router-view"))
        Vue.prototype.$light = this;
        require("./platform").init()
    }
});

export default Light;
