package com.bytedance.sdk.component.utils;

import android.content.Context;
import android.content.MutableContextWrapper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.bytedance.sdk.component.ef.wsy;
import com.bytedance.sdk.component.utils.lse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public class gzf {
    private static final HashMap<String, fm> fm = new HashMap<>();

    public static class fm {
        public final HashSet<String> fm = new HashSet<>();

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private final ArrayList<WebView> f6685lb = new ArrayList<>();
        public final int ro;
        private final String yz;

        public fm(String str, JSONObject jSONObject) {
            this.yz = str;
            this.ro = jSONObject.optInt("max_count");
            JSONArray jSONArrayOptJSONArray = jSONObject.optJSONArray("scene");
            if (jSONArrayOptJSONArray != null) {
                for (int i10 = 0; i10 < jSONArrayOptJSONArray.length(); i10++) {
                    String strOptString = jSONArrayOptJSONArray.optString(i10);
                    if (!TextUtils.isEmpty(strOptString)) {
                        this.fm.add(strOptString);
                    }
                }
            }
        }

        public HashSet<String> fm() {
            return this.fm;
        }

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

        public WebView ro() {
            if (this.f6685lb.isEmpty()) {
                return null;
            }
            return this.f6685lb.remove(0);
        }

        public boolean fm(WebView webView) {
            if (webView != null && this.f6685lb.size() < this.ro && !this.f6685lb.contains(webView)) {
                Context context = webView.getContext();
                if (context instanceof MutableContextWrapper) {
                    ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
                    if (webView instanceof com.bytedance.sdk.component.ef.ajl) {
                        ((com.bytedance.sdk.component.ef.ajl) webView).setRecycler(true);
                    }
                    ViewParent parent = webView.getParent();
                    if (parent instanceof ViewGroup) {
                        ((ViewGroup) parent).removeView(webView);
                    }
                    this.f6685lb.add(webView);
                    return true;
                }
            }
            return false;
        }
    }

    public static com.bytedance.sdk.component.ef.wsy fm(Context context, AttributeSet attributeSet, int i10, wsy.lb lbVar) {
        WebView webViewFm = fm(context, attributeSet, i10, lbVar, false);
        if (webViewFm == null) {
            return null;
        }
        com.bytedance.sdk.component.ef.wsy wsyVar = new com.bytedance.sdk.component.ef.wsy(context, true, lbVar);
        wsyVar.setWebView(webViewFm);
        wsyVar.jnr();
        return wsyVar;
    }

    public static void lb(com.bytedance.sdk.component.ef.wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        try {
            wsyVar.removeAllViews();
            wsyVar.wsy();
            wsyVar.setWebChromeClient(null);
            wsyVar.setWebViewClient(null);
            wsyVar.setDownloadListener(null);
            wsyVar.setJavaScriptEnabled(true);
            wsyVar.setCacheMode(-1);
            wsyVar.setSupportZoom(false);
            wsyVar.setUseWideViewPort(true);
            wsyVar.setJavaScriptCanOpenWindowsAutomatically(true);
            wsyVar.setDomStorageEnabled(true);
            wsyVar.setBuiltInZoomControls(false);
            wsyVar.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
            wsyVar.setLoadWithOverviewMode(false);
            wsyVar.setDefaultTextEncodingName("UTF-8");
            wsyVar.setDefaultFontSize(16);
            WebView webView = wsyVar.getWebView();
            if (webView instanceof com.bytedance.sdk.component.ef.ajl) {
                ((com.bytedance.sdk.component.ef.ajl) webView).fm();
            }
        } catch (Throwable unused) {
        }
    }

    public static WebView ro(Context context, AttributeSet attributeSet, int i10, wsy.lb lbVar) {
        return fm(context, attributeSet, i10, lbVar, true);
    }

    public static void yz(com.bytedance.sdk.component.ef.wsy wsyVar) {
        if (wsyVar == null) {
            return;
        }
        try {
            wsyVar.removeAllViews();
            wsyVar.wsy();
            wsyVar.setWebChromeClient(null);
            wsyVar.setWebViewClient(null);
            wsyVar.setDownloadListener(null);
            wsyVar.setJavaScriptEnabled(true);
            wsyVar.setCacheMode(-1);
            wsyVar.setSupportZoom(false);
            wsyVar.setUseWideViewPort(true);
            wsyVar.setJavaScriptCanOpenWindowsAutomatically(true);
            wsyVar.setDomStorageEnabled(true);
            wsyVar.setBuiltInZoomControls(false);
            wsyVar.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
            wsyVar.setLoadWithOverviewMode(false);
            wsyVar.setDefaultTextEncodingName("UTF-8");
            wsyVar.setDefaultFontSize(16);
            WebView webView = wsyVar.getWebView();
            if (webView instanceof com.bytedance.sdk.component.ef.ajl) {
                ((com.bytedance.sdk.component.ef.ajl) webView).ro();
            }
        } catch (Throwable unused) {
        }
    }

    public static void ro(com.bytedance.sdk.component.ef.wsy wsyVar) {
        WebView webView;
        if (wsyVar == null || (webView = wsyVar.getWebView()) == null) {
            return;
        }
        fm fmVar = fm.get(wsyVar.getScene().onz);
        if (fmVar == null || !fmVar.fm(webView)) {
            fm(webView);
        } else {
            yz(wsyVar);
        }
    }

    private static WebView fm(Context context, AttributeSet attributeSet, int i10, wsy.lb lbVar, boolean z10) {
        WebView webViewRo;
        fm fmVar;
        if (lbVar == null || attributeSet != null || i10 != 0 || (fmVar = fm.get(lbVar.onz)) == null) {
            webViewRo = null;
        } else {
            webViewRo = fmVar.ro();
            if (webViewRo != null) {
                if (webViewRo instanceof com.bytedance.sdk.component.ef.ajl) {
                    ((com.bytedance.sdk.component.ef.ajl) webViewRo).setRecycler(false);
                }
                fm(lbVar, false);
            }
        }
        if (webViewRo != null || !z10) {
            return webViewRo;
        }
        if (!(context instanceof MutableContextWrapper)) {
            context = new MutableContextWrapper(context);
        }
        fm(lbVar, true);
        return i10 != 0 ? new com.bytedance.sdk.component.ef.ajl(context, attributeSet, i10) : new com.bytedance.sdk.component.ef.ajl(context, attributeSet);
    }

    public static boolean ro(wsy.lb lbVar) {
        return (lbVar == null || fm.get(lbVar.onz) == null) ? false : true;
    }

    private static void fm(final wsy.lb lbVar, final boolean z10) {
        lse.fm("webview_allocate", new lse.fm() { // from class: com.bytedance.sdk.component.utils.gzf.1
            @Override // com.bytedance.sdk.component.utils.lse.fm
            public JSONObject fm() {
                JSONObject jSONObject = new JSONObject();
                try {
                    jSONObject.put("is_new", z10 ? 1 : 0);
                    jSONObject.put("scene", lbVar.onz);
                } catch (JSONException unused) {
                }
                return jSONObject;
            }
        });
    }

    public static void fm(com.bytedance.sdk.component.ef.wsy wsyVar) {
        WebView webView;
        if (wsyVar == null || (webView = wsyVar.getWebView()) == null) {
            return;
        }
        fm fmVar = fm.get(wsyVar.getScene().onz);
        if (fmVar != null && fmVar.fm(webView)) {
            lb(wsyVar);
        } else {
            fm(webView);
        }
    }

    private static void fm(WebView webView) {
        if (webView == null) {
            return;
        }
        try {
            Context context = webView.getContext();
            if (context instanceof MutableContextWrapper) {
                ((MutableContextWrapper) context).setBaseContext(context.getApplicationContext());
            }
            webView.setWebChromeClient(null);
            webView.setWebViewClient(null);
            ViewParent parent = webView.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(webView);
            }
            webView.removeAllViews();
            webView.destroy();
        } catch (Exception unused) {
        }
    }

    public static void fm(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        try {
            JSONObject jSONObject = new JSONObject(str);
            Iterator<String> itKeys = jSONObject.keys();
            while (itKeys.hasNext()) {
                String next = itKeys.next();
                if (!TextUtils.isEmpty(next)) {
                    fm fmVar = new fm(next, jSONObject.getJSONObject(next));
                    Iterator<String> it = fmVar.fm().iterator();
                    while (it.hasNext()) {
                        fm.put(it.next(), fmVar);
                    }
                }
            }
        } catch (Exception unused) {
        }
    }

    public static int fm(wsy.lb lbVar) {
        fm fmVar;
        if (lbVar == null || (fmVar = fm.get(lbVar.onz)) == null) {
            return 0;
        }
        return fmVar.lb();
    }
}
