import {trigger} from "../lib/event";
const Light = require("../lib/core");
import {parse2query,resolvePath} from "../lib/utils"
function initEvent() {
    window.LightJSBridge.onTabBarChange = function(args){
        trigger("ontabbarchange",args)
    };
    window.LightJSBridge.onPageAppear = function(args){
        trigger("viewappear",args)
    };
    window.LightJSBridge.onPageDisappear = function(){
        trigger("viewdisappear")
    };
    window.LightJSBridge.AppWillEnterForeground = function(){
        trigger("appappear")
    };
    window.LightJSBridge.AppDidEnterBackground = function(){
        trigger("appdisappear")
    };
    window.LightJSBridge.onAppActive = function(){
        trigger("appactive")
    };
    window.LightJSBridge.onAppInactive = function(){
        trigger("appinactive")
    };
    window.LightJSBridge.onScreenShot = function(data){
        data.screenShot=data.screenShot.replace(/\r\n/g,"");  //	 231663	native 插件新增支持jsn环境的监听屏幕截图配置,图片显示错误问题修复
        data.screenShot=data.screenShot.replace(/\s+/g,"");
        trigger("screenshot",data.screenShot)
    };
    //154941 light框架支持Light.on("viewback") 
    window.LightJSBridge.pageShouldBack = function(data){
        return trigger("viewback",{
            sync:true
        })
    };
    //TS:202104220132-财富经纪业务群_刘向锋
    window.LightJSBridge.onScreenCaptured = function(data){
        trigger("screencaptured",data.screenCaptured)
    };
}
module.exports = {
    init(){
        if (window.LightJSBridge) {
            initEvent()
        } else {
            document.addEventListener("deviceready", function () {
                initEvent()
            })
        }
    },
    start(router,options){
        const app = new Light.Vue({
            el : options.el|| "#main",
            router
        });
    },
    navigateBack(number){
        // number = number > window.history.length ? window.history.length : number;
        window.LightJSBridge.call('native.back', { number: number }, null);
    },
    navigateTo(path,params,options){
        //166093【框架】优化light.navigate路由跳转时无入参的路由
        let distURL = resolvePath(path.url, params);
        if(path.type === "jsnative"){
            location.href = `gmu://jsnative?startPage=${encodeURIComponent(distURL)}&${parse2query(options)}`
        }
        if(path.type === "web"){
            if(options.replace===false||path.url.indexOf("vhost.light.com")>-1){
                location.href = `gmu://web?startPage=${encodeURIComponent(distURL)}&${parse2query(options)}`
            }else{
                if(options.history!==false){
                    location.href= resolvePath(path.url, params);
                }else{
                    location.replace(resolvePath(path.url, params));
                }
            }
        }
        if(path.type === "native"){    
            //231597支持打开gmu://web?startPage=xxx（H5）
            location.href = path.url.indexOf('?')>-1 ? `${path.url}&${parse2query(params)}` : `${path.url}?${parse2query(params)}`;
        }
        //167515 小程序olight模块支持navigate跳转区分lightweb和miniapp
        if(path.type === "lightweb") {
            path.type = options.miniappContainer || path.type;
            location.href = `gmu://${path.type}?startPage=${encodeURIComponent(distURL)}&${parse2query(options)}`
        }
    },
    getCurrentFile(){
        return location.href.split(/([#])/)[0];
    },
    ajax:require("./pure").ajax
};