let platform = null;
if(process.env.RUNTIME === "web"){
    if(navigator.userAgent.toLowerCase().indexOf("lightos")!==-1){
        platform = require("./web");
    }else{
        platform = require("./pure");
    }

    if(typeof weex !== "undefined"){
        platform = require("./jsnative-web");
    }
}
if(process.env.RUNTIME === "native"){
    platform = require("./jsnative");
}
if(process.env.RUNTIME === "miniapp"){
    platform = require("./miniapp");
}

module.exports = {
    init(){
        platform.init();
    },
    start(router, options){
        platform.start(router, options);
    },
    deviceReady(cb){
        if(process.env.RUNTIME === "web"&&navigator.userAgent.toLowerCase().indexOf("lightos")!==-1 && !window.LightJSBridge){
            document.addEventListener("deviceready",function(){
                cb()
            })
        // }else if(process.env.RUNTIME === "miniapp"){
        //     if(__thread__.config){
        //         cb()
        //     }else{
        //         let config = null;
        //         Object.defineProperty(__thread__,"config",{
        //             set(t){
        //                 if(!config){
        //                     config = t;
        //                     cb()
        //                 }else{
        //                     config = t;
        //                 }
        //             },
        //             get(){
        //                 return config;
        //             },
        //             enumerable : true,
        //             configurable : true
        //         })
        //     }
        }else{
            cb()
        }
    },
    log(info){
        //119229 App.log转调lightsdk中的log接口输出日志到原生
        if (process.env.RUNTIME === "web") {
            if (navigator.userAgent.toLowerCase().indexOf("lightos") !== -1) {
                if(!window.LightJSBridge) {
                    document.addEventListener("deviceready", function () {
                        LightJSBridge.call('log.info', {
                            content:info
                        })
                    })
                } else {
                    LightJSBridge.call('log.info', {
                        content:info
                    })
                }

            }
        }else if (process.env.RUNTIME === "native"){
            weex.requireModule('LightJSBridge').call('log.info', {
                content:info
            })
        }
    },
    navigateBack(number){
        platform.navigateBack(number);
    },
    /**
     * @param view 路径,可以支持多种类型,规则视图不能有视图参数
     *  1. https://demo.lightyy.com/index.html?a=1#/view/abc
     *  2. https://demo.lightyy.com/index.native.js?a=1#/view/abc
     *  3. index.native.js?a=1#/view/abc
     *  4. index.html?a=1#/view/abc
     *  5. #/view/abc
     *  6. view/abc
     *  7.gmu://xxx
     *  8.@vhost/app.native.js#/view
     *  9.@vhost/index.html#/view
     *  10.abc.vhost.light.com/index.html#/view
     *  H5运行时的默认navigate行为是单页模式,而jsn默认是视图跳转模式//
     * @param params
     * @param options 是否产生历史记录
     */
    navigate(view, params, options) {
        options = options || {};
        params = params || {};

        let path = processPath(view);
        //160945 【Light小程序】【双进程渲染】支持小程序地址互相跳转
        if(!path.type) {
            if (/\.js$/.test(path.url.split(/([?#])/)[0]) && !/\.miniapp\.js$/.test(path.url.split(/([?#])/)[0])) {
                path.type = "jsnative";
            } else if (/\.miniapp\.js$/.test(path.url.split(/([?#])/)[0])) {
                path.type = "lightweb";
            } else {
                path.type = "web";
            }
        }
        platform.navigateTo(path, params, options);
    },
    ajax(options){
        platform.ajax(options);
    }
};

function processPath(view) {
    let path = {};

    //支持打开gmu://web?startPage=xxx
    //gmu://feedback
    let regCheck = /^gmu:\/\/([^?]*)/i.exec(view)
    if (regCheck) {
        path.url = view;
        path.module = regCheck[1];
        path.type = "native";
        return path;
    }

    //Http协议资源
    //vhost.light.com
    if (
        (/^(http|https|file):\/\//i.test(view))||
        (/^([^.]*).vhost.light.com/i.test(view))
    ) {
        path.url = view;
        return path;
    }
 

    //vhost://abc
    regCheck = /^vhost:\/\/([^\/]*)(.*)$/i.exec(view)
    if (regCheck) {
        //168297【框架】统一jsn/h5/miniapp跳转vhost协议
        path.url = `${regCheck[1]}.vhost.light.com${regCheck[2]}`;
        return path;
    }

    //index.html?a=1#/view/abc
    if (
        (/(([^.]*)\.(html|js))/.exec(view))
    ) {
        var newUrl = platform.getCurrentFile().split('/').reverse()[0];
        path.url = platform.getCurrentFile().replace(newUrl, '') + view;
        return path;
    }

    //hash
    regCheck =  /^(#\/)?(.*)/i.exec(view);
    if (regCheck) {
        //175285 修复replace为true时跳转视图白屏的问题
        regCheck[2] = regCheck[2].replace(/^\//,'');
        path.view = '/'+regCheck[2];
        path.url = `${platform.getCurrentFile()}#/${regCheck[2]}`;
        return path;
    }
}
