package com.unit.websdk;

import android.app.Activity;
import android.content.Context;
import android.webkit.JavascriptInterface;

import com.unit.util.GlobalUtil;

public class WebJsBridge {
    private String name;
    private String jsBridgeName;

    public WebJsBridge(String name, String jsBridgeName) {
        this.name = name;
        this.jsBridgeName = jsBridgeName;
    }

    public String getJsBridgeName(){
        return jsBridgeName;
    }

    // 外跳浏览器
    @JavascriptInterface
    public void openBrowser(String url) {
        Context context = GlobalUtil.getAppContext();
        WebViewUtil.openBrowser(context, url);
    }

    // 打开一个新的webview页面的方法
    @JavascriptInterface
    public void openWebview(String url) {
        LigthWebview.open(GlobalUtil.getAppContext(), url);
    }

    // 点击广告按钮时关闭广告页面的方法
    @JavascriptInterface
    public void close() {
        try {
            Activity activity = LigthWebview.get(name);
            if (activity == null || activity.isDestroyed() || activity.isFinishing()) {
                LigthWebview.remove(name);
                return;
            }
            GlobalUtil.uiRun(() -> activity.finish(), 0);
        }catch (Throwable e){
            e.printStackTrace();
        }
    }
}
