package com.bytedance.sdk.component.adexpress.jnr;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.MutableContextWrapper;
import android.text.TextUtils;
import android.webkit.WebView;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import com.bytedance.sdk.component.ef.wsy;
import com.bytedance.sdk.component.utils.gzf;
import com.bytedance.sdk.component.utils.wey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes.dex */
public class jnr {
    private static int ajl = 10;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private static volatile jnr f6540ef = null;
    private static final byte[] jnr = new byte[0];
    private static int wu = 10;
    private final AtomicBoolean wsy = new AtomicBoolean(false);
    private List<wsy> fm = new ArrayList();
    private List<wsy> ro = new ArrayList();

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private Map<Integer, lb> f6541lb = new HashMap();
    private Map<Integer, yz> yz = new HashMap();

    private jnr() {
        com.bytedance.sdk.component.adexpress.fm.fm.lb lbVarLb = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().lb();
        if (lbVarLb != null) {
            ajl = lbVarLb.vt();
            wu = lbVarLb.ku();
        }
    }

    private void ef(wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        if (gzf.ro(wsyVar.getScene())) {
            gzf.fm(wsyVar);
            return;
        }
        if (this.fm.size() >= ajl) {
            try {
                Context context = wsyVar.getContext();
                if (context instanceof MutableContextWrapper) {
                    ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
                }
                wsyVar.fhx();
                return;
            } catch (Throwable th) {
                th.getMessage();
                return;
            }
        }
        if (this.fm.contains(wsyVar)) {
            return;
        }
        try {
            Context context2 = wsyVar.getContext();
            if (context2 instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context2).setBaseContext(context2.getApplicationContext());
                wsyVar.setRecycler(true);
                this.fm.add(wsyVar);
                lb();
            }
        } catch (Throwable th2) {
            lb();
            th2.getMessage();
        }
    }

    public static jnr fm() {
        if (f6540ef == null) {
            synchronized (jnr.class) {
                try {
                    if (f6540ef == null) {
                        f6540ef = new jnr();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        return f6540ef;
    }

    public boolean ajl(wsy wsyVar) {
        if (wsyVar == null) {
            return false;
        }
        try {
            Context context = wsyVar.getContext();
            if (context instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
            }
            wsyVar.fhx();
            return true;
        } catch (Throwable th) {
            th.getMessage();
            return true;
        }
    }

    @UiThread
    public void jnr(wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        gzf.lb(wsyVar);
        wsyVar.b_("SDK_INJECT_GLOBAL");
        wsy(wsyVar);
        ef(wsyVar);
    }

    public void lb(wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        if (gzf.ro(wsyVar.getScene())) {
            gzf.fm(wsyVar);
            return;
        }
        if (this.ro.size() >= wu) {
            try {
                Context context = wsyVar.getContext();
                if (context instanceof MutableContextWrapper) {
                    ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
                }
                wsyVar.fhx();
                return;
            } catch (Throwable th) {
                th.getMessage();
                return;
            }
        }
        if (this.ro.contains(wsyVar)) {
            return;
        }
        try {
            Context context2 = wsyVar.getContext();
            if (context2 instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context2).setBaseContext(context2.getApplicationContext());
                wsyVar.setRecycler(true);
                this.ro.add(wsyVar);
                yz();
            }
        } catch (Throwable th2) {
            yz();
            th2.getMessage();
        }
    }

    public void ro(wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        gzf.yz(wsyVar);
        wu(wsyVar);
        if (gzf.ro(wsyVar.getScene())) {
            gzf.ro(wsyVar);
            return;
        }
        try {
            Context context = wsyVar.getContext();
            if (context instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
            }
            wsyVar.fhx();
        } catch (Throwable th) {
            th.getMessage();
        }
    }

    public void wsy(wsy wsyVar) {
        WebView webView;
        if (wsyVar == null || (webView = wsyVar.getWebView()) == null) {
            return;
        }
        lb lbVar = this.f6541lb.get(Integer.valueOf(webView.hashCode()));
        if (lbVar != null) {
            lbVar.fm(null);
        }
        wey.fm("WebViewPool", "unRegisterJavascriptInterface: express jsb recycle webview will remove javascript interfaceSDK_INJECT_GLOBAL");
        wsyVar.b_("SDK_INJECT_GLOBAL");
    }

    public void wu(wsy wsyVar) {
        WebView webView;
        lb lbVar;
        wey.fm("WebViewPool", "updateTTAndroidObject: express jsb recycle webview will not remove javascript interfaceSDK_INJECT_GLOBAL");
        if (wsyVar == null || (webView = wsyVar.getWebView()) == null || (lbVar = this.f6541lb.get(Integer.valueOf(webView.hashCode()))) == null) {
            return;
        }
        lbVar.fm(null);
    }

    @UiThread
    public void yz(wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        gzf.lb(wsyVar);
        wsyVar.b_("SDK_INJECT_GLOBAL");
        wsy(wsyVar);
        lb(wsyVar);
    }

    public wsy fm(Context context, String str) {
        wsy.lb lbVar = wsy.lb.ADS_V4;
        if (!gzf.ro(lbVar)) {
            return null;
        }
        if (!com.bytedance.sdk.component.adexpress.yz.jnr.fm(str) || gzf.fm(lbVar) > 1) {
            return gzf.fm(context, null, 0, lbVar);
        }
        return null;
    }

    public int yz() {
        return this.ro.size();
    }

    public void fm(wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        gzf.lb(wsyVar);
        wsyVar.b_("SDK_INJECT_GLOBAL");
        wsy(wsyVar);
        if (gzf.ro(wsyVar.getScene())) {
            gzf.fm(wsyVar);
            return;
        }
        try {
            Context context = wsyVar.getContext();
            if (context instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
            }
            wsyVar.fhx();
        } catch (Throwable th) {
            th.getMessage();
        }
    }

    @Nullable
    public wsy ro(Context context, String str) {
        wsy.lb lbVar = wsy.lb.ADS_V3;
        if (gzf.ro(lbVar)) {
            if (!com.bytedance.sdk.component.adexpress.yz.jnr.fm(str) || gzf.fm(lbVar) > 1) {
                return gzf.fm(context, null, 0, lbVar);
            }
            return null;
        }
        if (yz() <= 0) {
            return null;
        }
        if (com.bytedance.sdk.component.adexpress.yz.jnr.fm(str) && yz() <= 1) {
            yz();
            return null;
        }
        wsy wsyVarRemove = this.ro.remove(0);
        if (wsyVarRemove == null) {
            return null;
        }
        try {
            Context context2 = wsyVarRemove.getContext();
            if (context2 instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context2).setBaseContext(context.getApplicationContext());
                wsyVarRemove.setRecycler(false);
                yz();
            }
            return wsyVarRemove;
        } catch (Throwable unused) {
            yz();
            return null;
        }
    }

    @SuppressLint({"JavascriptInterface"})
    public void fm(wsy wsyVar, ro roVar) {
        WebView webView;
        if (wsyVar == null || roVar == null || (webView = wsyVar.getWebView()) == null) {
            return;
        }
        lb lbVar = this.f6541lb.get(Integer.valueOf(webView.hashCode()));
        if (lbVar != null) {
            lbVar.fm(roVar);
        } else {
            lbVar = new lb(roVar);
            this.f6541lb.put(Integer.valueOf(webView.hashCode()), lbVar);
        }
        wey.fm("WebViewPool", "registerJavascriptInterface: express jsb recycle webview will register javascript interface every timeSDK_INJECT_GLOBAL");
        wsyVar.fm(lbVar, "SDK_INJECT_GLOBAL");
    }

    @Nullable
    public wsy lb(Context context, String str) {
        wsy.lb lbVar = wsy.lb.ADS;
        if (gzf.ro(lbVar)) {
            if (!com.bytedance.sdk.component.adexpress.yz.jnr.fm(str) || gzf.fm(lbVar) > 1) {
                return gzf.fm(context, null, 0, lbVar);
            }
            return null;
        }
        if (lb() <= 0) {
            return null;
        }
        if (com.bytedance.sdk.component.adexpress.yz.jnr.fm(str) && lb() <= 1) {
            lb();
            return null;
        }
        wsy wsyVarRemove = this.fm.remove(0);
        if (wsyVarRemove == null) {
            return null;
        }
        try {
            Context context2 = wsyVarRemove.getContext();
            if (context2 instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context2).setBaseContext(context.getApplicationContext());
                wsyVarRemove.setRecycler(false);
                lb();
            }
            return wsyVarRemove;
        } catch (Throwable unused) {
            lb();
            return null;
        }
    }

    public void ro() {
        for (wsy wsyVar : this.fm) {
            if (wsyVar != null) {
                try {
                    Context context = wsyVar.getContext();
                    if (context instanceof MutableContextWrapper) {
                        ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
                    }
                    wsyVar.fhx();
                } catch (Throwable th) {
                    th.getMessage();
                }
            }
        }
        this.fm.clear();
        for (wsy wsyVar2 : this.ro) {
            if (wsyVar2 != null) {
                try {
                    Context context2 = wsyVar2.getContext();
                    if (context2 instanceof MutableContextWrapper) {
                        ((MutableContextWrapper) context2).setBaseContext(context2.getApplicationContext());
                    }
                    wsyVar2.fhx();
                } catch (Throwable th2) {
                    th2.getMessage();
                }
            }
        }
        this.ro.clear();
    }

    @SuppressLint({"JavascriptInterface"})
    public void fm(WebView webView, com.bytedance.sdk.component.fm.wey weyVar, String str) {
        if (webView == null || weyVar == null || TextUtils.isEmpty(str)) {
            return;
        }
        yz yzVar = this.yz.get(Integer.valueOf(webView.hashCode()));
        if (yzVar != null) {
            yzVar.fm(weyVar);
        } else {
            yzVar = new yz(weyVar);
            this.yz.put(Integer.valueOf(webView.hashCode()), yzVar);
        }
        wey.fm("WebViewPool", "registerJavascriptInterfaceForJsB2: jsb 3.0 register javascript interface every time".concat(String.valueOf(str)));
        webView.addJavascriptInterface(yzVar, str);
    }

    public int lb() {
        return this.fm.size();
    }

    public void fm(WebView webView, String str) {
        wey.fm("WebViewPool", "unRegisterJavascriptInterfaceForJsB2: jsb 3.0 recycle webview will remove javascript interface".concat(String.valueOf(str)));
        if (webView == null || TextUtils.isEmpty(str)) {
            return;
        }
        yz yzVar = this.yz.get(Integer.valueOf(webView.hashCode()));
        if (yzVar != null) {
            yzVar.fm(null);
        }
        webView.removeJavascriptInterface(str);
    }

    public void fm(int i10) {
        synchronized (jnr) {
            ajl = i10;
        }
    }

    @SuppressLint({"JavascriptInterface"})
    public void ro(wsy wsyVar, ro roVar) {
        WebView webView;
        if (wsyVar == null || roVar == null || (webView = wsyVar.getWebView()) == null) {
            return;
        }
        lb lbVar = this.f6541lb.get(Integer.valueOf(webView.hashCode()));
        if (lbVar != null) {
            wey.fm("WebViewPool", "registerOnceJavascriptInterface: express jsb recycle webview will not register javascript interface in reuse webviewSDK_INJECT_GLOBAL");
            lbVar.fm(roVar);
        } else {
            lb lbVar2 = new lb(roVar);
            this.f6541lb.put(Integer.valueOf(webView.hashCode()), lbVar2);
            wey.fm("WebViewPool", "registerOnceJavascriptInterface: express jsb recycle webview will register once javascript interfaceSDK_INJECT_GLOBAL");
            wsyVar.fm(lbVar2, "SDK_INJECT_GLOBAL");
        }
    }

    public void ro(WebView webView, com.bytedance.sdk.component.fm.wey weyVar, String str) {
        if (webView == null || weyVar == null || TextUtils.isEmpty(str)) {
            return;
        }
        yz yzVar = this.yz.get(Integer.valueOf(webView.hashCode()));
        if (yzVar != null) {
            wey.fm("WebViewPool", "registerOnceJavascriptInterfaceForJsB2: jsb 3.0 will not register javascript interface in reuse webview".concat(String.valueOf(str)));
            yzVar.fm(weyVar);
        } else {
            wey.fm("WebViewPool", "registerOnceJavascriptInterfaceForJsB2: jsb 3.0 register once javascript interface in created webview".concat(String.valueOf(str)));
            yz yzVar2 = new yz(weyVar);
            this.yz.put(Integer.valueOf(webView.hashCode()), yzVar2);
            webView.addJavascriptInterface(yzVar2, str);
        }
    }

    public void ro(WebView webView, String str) {
        yz yzVar;
        wey.fm("WebViewPool", "updateWebViewBridge: jsb 3.0 recycle webview will not remove javascript interface".concat(String.valueOf(str)));
        if (webView == null || TextUtils.isEmpty(str) || (yzVar = this.yz.get(Integer.valueOf(webView.hashCode()))) == null) {
            return;
        }
        yzVar.fm(null);
    }

    public void ro(int i10) {
        synchronized (jnr) {
            wu = i10;
        }
    }
}
