/* eslint-disable */
var ANDROID = "android"
var IOS = "ios"
var curApp = "";
var ua = window.navigator.userAgent.toLowerCase();
if (/android/.test(ua)) {
    curApp = ANDROID
} else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
    curApp = IOS
}
else {
    document.write("curApp =>  其他类型 => " + ua)
}

if (curApp === ANDROID) {
    var bridge = {
        //default:this,
        call: function (b, a, c) {
            var e = "";
            "function" == typeof a && (c = a, a = {});
            a = {
                data: void 0 === a ? null : a
            };
            if ("function" == typeof c) {
                var g = "dscb" + window.dscb++;
                window[g] = c;
                a._dscbstub = g
            }
            a = JSON.stringify(a);
            if (window._dsbridge) e = _dsbridge.call(b, a);
            else if (window._dswk || -1 != navigator.userAgent.indexOf("_dsbridge")) e = prompt("_dsbridge=" + b, a);
            return JSON.parse(e || "{}").data
        },
        register: function (b, a, c) {
            c = c ? window._dsaf : window._dsf;
            window._dsInit || (window._dsInit = !0, setTimeout(function () {
                bridge.call("_dsb.dsinit")
            },
                0));
            "object" == typeof a ? c._obs[b] = a : c[b] = a
        },
        registerAsyn: function (b, a) {
            this.register(b, a, !0)
        },
        hasNativeMethod: function (b, a) {
            return this.call("_dsb.hasNativeMethod", {
                name: b,
                type: a || "all"
            })
        },
        disableJavascriptDialogBlock: function (b) {
            this.call("_dsb.disableJavascriptDialogBlock", {
                disable: !1 !== b
            })
        }
    }; !
        function () {//!function "执行"
            if (!window._dsf) {
                var b = {
                    _dsf: {
                        _obs: {}
                    },
                    _dsaf: {
                        _obs: {}
                    },
                    dscb: 0,
                    dsBridge: bridge,
                    close: function () {
                        bridge.call("_dsb.closePage")
                    },
                    _handleMessageFromNative: function (a) {
                        var e = JSON.parse(a.data),
                            b = {
                                id: a.callbackId,
                                complete: !0
                            },
                            c = this._dsf[a.method],
                            d = this._dsaf[a.method],
                            h = function (a, c) {
                                b.data = a.apply(c, e);
                                bridge.call("_dsb.returnValue", b)
                            },
                            k = function (a, c) {
                                e.push(function (a, c) {
                                    b.data = a;
                                    b.complete = !1 !== c;
                                    bridge.call("_dsb.returnValue", b)
                                });
                                a.apply(c, e)
                            };
                        if (c) h(c, this._dsf);
                        else if (d) k(d, this._dsaf);
                        else if (c = a.method.split("."), !(2 > c.length)) {
                            a = c.pop();
                            var c = c.join("."),
                                d = this._dsf._obs,
                                d = d[c] || {},
                                f = d[a];
                            f && "function" == typeof f ? h(f, d) : (d = this._dsaf._obs, d = d[c] || {},
                                (f = d[a]) && "function" == typeof f && k(f, d))
                        }
                    }
                },
                    a;
                for (a in b) window[a] = b[a];
                bridge.register("_hasJavascriptMethod",
                    function (a, b) {
                        b = a.split(".");
                        if (2 > b.length) return !(!_dsf[b] && !_dsaf[b]);
                        a = b.pop();
                        b = b.join(".");
                        return (b = _dsf._obs[b] || _dsaf._obs[b]) && !!b[a]
                    })
            }
        }();
}
else if (curApp == IOS) {

    var jsBridge = function (bridgeName, bridgeScheme) {
        this.bridgeName = bridgeName || 'WebViewJavascriptBridge'
        this.bridgeScheme = bridgeScheme || 'https'
        var ua = navigator.userAgent || navigator.vendor || window.opera
        var ANDROID = 1
        var IOS = 2
        var SUCCESS_CODE = 0
        ua = ua.toLowerCase()
        if (/android/.test(ua)) {
            this.app = ANDROID
        } else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
            this.app = IOS
        }

        /* 获取 bridge */
        this.getBridge = function (callback) {
            if (window[this.bridgeName]) {
                callback(window[this.bridgeName])
                return
            }
            if (this.app === ANDROID) {
                document.addEventListener(
                    'WebViewJavascriptBridgeReady',
                    function () {
                        callback(window[this.bridgeName])
                    },
                    false
                )
            } else if (this.app === IOS) {
                // WebViewJavascriptBridge 由native在注入  
                // https://github.com/marcuswestin/WebViewJavascriptBridge
                if (window.WVJBCallbacks) {
                    return window.WVJBCallbacks.push(callback)
                }
                window.WVJBCallbacks = [callback];
                var WVJBIframe = document.createElement('iframe');
                WVJBIframe.style.display = 'none';
                WVJBIframe.src = 'https://__bridge_loaded__';
                document.documentElement.appendChild(WVJBIframe);
                setTimeout(function () {
                    document.documentElement.removeChild(WVJBIframe);
                }, 0)
            }
        }
        /**
         * 给ios和android注册事件监听
         *
         * @memberof jsBridge
         */
        this.on = function (event, jsHandler) {
            this.registHandler(event, jsHandler)
        }

        /**
         * 默认监听事件
         *
         * @memberof jsBridge
         */
        this.registDefaultHandlerForApp = function () {
            this.on('reload', function () {
                location.reload()
            })
        }

        /**
         * 注册事件逻辑
         *
         * @memberof jsBridge
         */
        this.registHandler = function (event, jsHandler) {
            this.getBridge(function (bridge) {
                if (bridge && bridge.registerHandler && typeof bridge.registerHandler === "function") {
                    bridge.registerHandler(event, function (data2js, responseCallback) {
                        responseCallback = responseCallback || function () { }
                        jsHandler(data2js, responseCallback)
                    })
                }
            })
        }
        /**
         * 调用 app
         * @param {string} event 
         * @param {object} params 
         * @param {fun} callback 
         * @return JSON对象
         */
        this.callHandler = function (event, params, callback) {
            this.getBridge(function (bridge) {
                if (bridge && bridge.registerHandler && typeof bridge.registerHandler === "function") {
                    bridge.callHandler(event, params, callback)
                }
            })
        }
        this.registDefaultHandlerForApp()

        return this;
    }
}

if (!jsBridge) {
    jsBridge = function () { };
}
jsBridge.prototype.curApp = curApp

//=====================================================================================
//==================================增加新的应用方法====================================
//=====================================================================================
// if(/wallet/.test(ua)){
//   console.log("在钱包环境")
// }
// else{
//   console.log("不是在钱包环境")
// }
/**
 * 调用app的分享微信朋友圈
 * 扫描结果是返回字符串
 */
jsBridge.prototype.friendCircleShare = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('friendCircleShare', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('friendCircleShare', params, success)
    }
}

/**
 * 调用app的分享微信
 * 扫描结果是返回字符串
 */
jsBridge.prototype.wechatShare = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('wechatShare', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('wechatShare', params, success)
    }
}

/**
 * 获取语言类型
 */
jsBridge.prototype.getLang = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getLang', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getLang', params, success)
    }
}

/**
 * 获取登录Token
 */
jsBridge.prototype.getToken = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call("getToken", params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler("getToken", params, success)
    }
}

/**
 * 获取手机号码
 */
jsBridge.prototype.getPhone = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call("getPhone", params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler("getPhone", params, success)
    }
}

/**
 * 获取平台Id
 */
jsBridge.prototype.getPlatformId = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call("getPlatformId", params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler("getPlatformId", params, success)
    }
}

/**
 * 关闭当前webview
 */
jsBridge.prototype.closeCurrentWebview = function (params) {
    if (this.curApp == ANDROID) {
        dsBridge.call('closeCurrentWebview', params)
    }
    else if (this.curApp == IOS) {
        this.callHandler('closeCurrentWebview', params)
    }
}

/**
 * 获取当前钱包的bty的地址
 */
jsBridge.prototype.getCurrentBTYAddress = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getCurrentBTYAddress', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getCurrentBTYAddress', params, success)
    }
}

/**
 * 获取设备唯一编号
 */
jsBridge.prototype.getDeviceId = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getDeviceId', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getDeviceId', params, success)
    }
}

/**
 * 获取托管账户的私钥
 *
 * @return
 */
jsBridge.prototype.getTGLogin = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getTGLogin', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getTGLogin', params, success)
    }
}

/**
 * 设置标题
 */
jsBridge.prototype.setTitle = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('setTitle', { title: params.title }, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('setTitle', { title: params.title }, success)
    }
}

/**
 * 树莓派-获取当前wifi
 */
jsBridge.prototype.getCurrentWifi = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getCurrentWifi', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getCurrentWifi', params, success)
    }
}

/**
 * 树莓派-导入助记词
 */
jsBridge.prototype.importSeed = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('importSeed', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('importSeed', params, success)
    }
}

/**
 * 树莓派-获取设备列表与IP
 */
jsBridge.prototype.getDeviceList = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getDeviceList', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getDeviceList', params, success)
    }
}

/**
 * 签名
 */
jsBridge.prototype.sign = function (param, success) {
    if (!param.createHash) {
        return;
    }
    if (!param.exer) {
        delete param.exer
    }
    if (typeof (param.isWithhold) === "undefined") {
        delete param.isWithhold
    }
    else {
        param.isWithhold = param.isWithhold ? 1 : -1;
    }

    if (this.curApp == ANDROID) {
        dsBridge.call('sign', param, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('sign', param, success)
    }
}

/**
 * signTxGroup
 */
jsBridge.prototype.signTxGroup = function (param, success) {
    if (!param.createHash) {
        return;
    }
    if (!param.exer) {
        delete param.exer
    }
    if (typeof (param.isWithhold) === "undefined") {
        delete param.isWithhold
    }
    else {
        param.isWithhold = param.isWithhold ? 1 : -1;
    }

    if (this.curApp == ANDROID) {
        dsBridge.call('signTxGroup', param, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('signTxGroup', param, success)
    }
}

/**
 * 配置私钥(cachePriv: 1=>缓存, -1:清空缓存)
 */
jsBridge.prototype.configPriv = function (param, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('configPriv', { cachePriv: param.cachePriv }, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('configPriv', { cachePriv: param.cachePriv }, success)
    }
}

/**
 * 扫码
 */
jsBridge.prototype.scanQRCode = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('scanQRCode', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('scanQRCode', params, success)
    }
}


/**
 * 传红包地址
 */
jsBridge.prototype.getRedUrl = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getRedUrl', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getRedUrl', params, success)
    }
}


/**
 * 打电话
 */
jsBridge.prototype.bridge_call = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_call', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_call', params, success)
    }
}

/* 跳到导航软件 */
jsBridge.prototype.bridge_navigate = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_navigate', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_navigate', params, success)
    }
}

/* 跳到导航软件 */
jsBridge.prototype.bridge_getGps = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_getGps', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_getGps', params, success)
    }
}

/* 退出 */
jsBridge.prototype.bridge_quitApp = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_quitApp', params)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_quitApp', params, success)
    }
}

/* 去直播 */
jsBridge.prototype.bridge_live = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_live', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_live', params, success)
    }
}

/* 获取刘海高度 */
jsBridge.prototype.getNotchHeight = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('getNotchHeight', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('getNotchHeight', params, success)
    }
}

/* 获取直播信息 */
jsBridge.prototype.bridge_getliveInfo = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_getliveInfo', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_getliveInfo', params, success)
    }
}


/* 去直播间 */
jsBridge.prototype.bridge_gotoLiveRoom = function (params, success) {
    if (this.curApp == ANDROID) {
        dsBridge.call('bridge_gotoLiveRoom', params, success)
    }
    else if (this.curApp == IOS) {
        this.callHandler('bridge_gotoLiveRoom', params, success)
    }
}

export default jsBridge;
