package com.bytedance.sdk.openadsdk.core.vt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bytedance.sdk.component.ef.wsy;
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGNativeAd;
import com.bytedance.sdk.openadsdk.core.vt.lb;
import com.bytedance.sdk.openadsdk.core.xgn;
import com.bytedance.sdk.openadsdk.utils.bx;
import com.iab.omid.library.bytedance2.adsession.FriendlyObstructionPurpose;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* JADX INFO: loaded from: classes2.dex */
public class ajl extends com.bytedance.sdk.component.ef.wsy implements lb.InterfaceC0085lb {
    AtomicBoolean ajl;
    private List<String> dsz;
    private int duv;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private lb f7290ef;
    private int fhx;
    private long irt;
    protected boolean jnr;
    private String ku;
    private ro onz;
    private com.bytedance.sdk.openadsdk.core.model.lse vt;
    AtomicBoolean wsy;
    private nt wu;
    protected boolean yz;

    public static class fm extends wsy.fm {
        public static final Set<String> fm = new HashSet<String>() { // from class: com.bytedance.sdk.openadsdk.core.vt.ajl.fm.1
            {
                add(".jpeg");
                add(".png");
                add(".bmp");
                add(".gif");
                add(".jpg");
                add(".webp");
            }
        };
        lb.InterfaceC0085lb ro;

        public fm(lb.InterfaceC0085lb interfaceC0085lb) {
            this.ro = interfaceC0085lb;
        }

        private void fm(String str) {
            int iLastIndexOf;
            lb.InterfaceC0085lb interfaceC0085lb;
            if (!TextUtils.isEmpty(str) && (iLastIndexOf = str.lastIndexOf(".")) > 0) {
                if (!fm.contains(str.substring(iLastIndexOf).toLowerCase()) || (interfaceC0085lb = this.ro) == null) {
                    return;
                }
                interfaceC0085lb.ro(str);
            }
        }

        @Override // android.webkit.WebViewClient
        public void onPageFinished(WebView webView, String str) {
            super.onPageFinished(webView, str);
            lb.InterfaceC0085lb interfaceC0085lb = this.ro;
            if (interfaceC0085lb != null) {
                interfaceC0085lb.fm();
            }
        }

        @Override // android.webkit.WebViewClient
        public void onReceivedError(WebView webView, int i10, String str, String str2) {
            super.onReceivedError(webView, i10, str, str2);
        }

        @Override // android.webkit.WebViewClient
        public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
            super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
            if (webResourceRequest == null || webResourceResponse == null || webResourceRequest.getUrl() == null) {
                return;
            }
            if (webResourceRequest.isForMainFrame()) {
                fm(webResourceRequest.getUrl().toString(), webResourceResponse.getStatusCode(), "");
            }
            fm(webResourceRequest.getUrl().toString());
        }

        @Override // android.webkit.WebViewClient
        public boolean shouldOverrideUrlLoading(WebView webView, String str) {
            this.ro.fm(str);
            return true;
        }

        @Override // android.webkit.WebViewClient
        public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
            super.onReceivedError(webView, webResourceRequest, webResourceError);
            if (webResourceRequest == null || webResourceRequest.getUrl() == null) {
                return;
            }
            fm(webResourceRequest.getUrl().toString());
        }

        private void fm(String str, int i10, String str2) {
            lb.InterfaceC0085lb interfaceC0085lb = this.ro;
            if (interfaceC0085lb != null) {
                interfaceC0085lb.fm(106, i10);
            }
        }
    }

    public interface ro {
        View fm();

        void fm(int i10, int i11);

        void fm(View view, int i10);

        void ro();
    }

    public ajl(Context context) {
        super(context, wsy.lb.DSP);
        this.yz = false;
        this.jnr = false;
        this.ajl = new AtomicBoolean(false);
        this.wsy = new AtomicBoolean(false);
        this.duv = 0;
    }

    private void wey() {
        if (this.dsz == null) {
            com.bytedance.sdk.openadsdk.yz.lb.ro(this.vt, this.ku, "dsp_html_success_url", (JSONObject) null);
        } else {
            com.bytedance.sdk.openadsdk.yz.lb.fm(new com.bytedance.sdk.component.wu.ro.lb("dsp_html_error_url") { // from class: com.bytedance.sdk.openadsdk.core.vt.ajl.3
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        if (ajl.this.dsz != null && ajl.this.wsy.compareAndSet(false, true)) {
                            JSONObject jSONObject = new JSONObject();
                            JSONArray jSONArray = new JSONArray();
                            Iterator it = ajl.this.dsz.iterator();
                            while (it.hasNext()) {
                                jSONArray.put((String) it.next());
                            }
                            jSONObject.put(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL, jSONArray);
                            com.bytedance.sdk.openadsdk.yz.lb.ro(ajl.this.vt, ajl.this.ku, "dsp_html_error_url", jSONObject);
                            ajl.this.dsz = null;
                        }
                    } catch (Exception unused) {
                    }
                }
            });
        }
    }

    @Override // com.bytedance.sdk.component.ef.wsy
    public void fhx() {
        this.f7290ef.ro();
        super.fhx();
    }

    public void gof() {
        ro roVar = this.onz;
        if (roVar != null) {
            roVar.ro();
        }
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("render_duration", SystemClock.elapsedRealtime() - this.irt);
        } catch (Throwable unused) {
        }
        com.bytedance.sdk.openadsdk.yz.lb.ro(this.vt, this.ku, "render_html_success", jSONObject);
    }

    public void gqi() {
        this.ajl.set(false);
        String strMzr = this.vt.mzr();
        if (TextUtils.isEmpty(strMzr)) {
            return;
        }
        String strFm = com.bytedance.sdk.openadsdk.core.onz.jnr.fm(strMzr);
        String str = TextUtils.isEmpty(strFm) ? strMzr : strFm;
        this.duv = 0;
        fm(null, str, "text/html", "UTF-8", null);
        this.irt = SystemClock.elapsedRealtime();
    }

    @Override // com.bytedance.sdk.component.ef.wsy, android.view.ViewGroup, android.view.View
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.yz) {
            this.f7290ef.fm(getWebView());
        }
    }

    @Override // com.bytedance.sdk.component.ef.wsy, android.view.ViewGroup, android.view.View
    public void onDetachedFromWindow() {
        this.f7290ef.fm();
        super.onDetachedFromWindow();
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("rate", this.fhx / 100.0f);
        } catch (Throwable unused) {
        }
        com.bytedance.sdk.openadsdk.yz.lb.ro(this.vt, this.ku, "load_rate", jSONObject);
    }

    @Override // android.view.View
    public void onVisibilityChanged(@NonNull View view, int i10) {
        super.onVisibilityChanged(view, i10);
        boolean z10 = i10 == 0;
        this.jnr = z10;
        this.f7290ef.fm(z10);
    }

    @Override // com.bytedance.sdk.openadsdk.core.vt.lb.InterfaceC0085lb
    public void ro(String str) {
        if (this.dsz == null) {
            this.dsz = new ArrayList();
        }
        this.dsz.add(str);
    }

    public static class lb {
        protected int fm = 0;
        private com.bytedance.sdk.openadsdk.core.onz.ajl ro = com.bytedance.sdk.openadsdk.core.onz.ajl.fm();

        public void fm(WebView webView) {
            if (webView != null && this.fm == 0) {
                if (this.ro == null) {
                    this.ro = com.bytedance.sdk.openadsdk.core.onz.ajl.fm();
                }
                this.ro.fm(webView);
                this.ro.ro();
                this.fm = 1;
            }
        }

        public void ro() {
            fm();
        }

        public void fm(boolean z10) {
            com.bytedance.sdk.openadsdk.core.onz.ajl ajlVar;
            if (this.fm == 1 && z10 && (ajlVar = this.ro) != null) {
                ajlVar.lb();
                this.fm = 3;
            }
        }

        public void fm(@Nullable View view, @Nullable FriendlyObstructionPurpose friendlyObstructionPurpose) {
            com.bytedance.sdk.openadsdk.core.onz.ajl ajlVar = this.ro;
            if (ajlVar != null) {
                ajlVar.fm(view, friendlyObstructionPurpose);
            }
        }

        public void fm() {
            com.bytedance.sdk.openadsdk.core.onz.ajl ajlVar;
            int i10 = this.fm;
            if (i10 != 0 && i10 != 4 && (ajlVar = this.ro) != null) {
                ajlVar.yz();
            }
            this.fm = 4;
            this.ro = null;
        }
    }

    public void fm(com.bytedance.sdk.openadsdk.core.model.lse lseVar, ro roVar, String str) {
        this.onz = roVar;
        this.vt = lseVar;
        this.ku = str;
        this.f7290ef = new lb();
        this.wu = new nt(getContext());
        setWebViewClient(new fm(this));
        setWebChromeClient(new WebChromeClient() { // from class: com.bytedance.sdk.openadsdk.core.vt.ajl.1
            @Override // android.webkit.WebChromeClient
            public void onProgressChanged(WebView webView, int i10) {
                ajl.this.fhx = i10;
                super.onProgressChanged(webView, i10);
                if (i10 >= 100) {
                    ajl.this.fm();
                }
            }
        });
        com.bytedance.sdk.component.utils.ef.ro().post(new Runnable() { // from class: com.bytedance.sdk.openadsdk.core.vt.ajl.2
            @Override // java.lang.Runnable
            @SuppressLint({"ClickableViewAccessibility"})
            public void run() {
                WebView webView = ajl.this.getWebView();
                if (webView != null) {
                    webView.setOnTouchListener(new View.OnTouchListener() { // from class: com.bytedance.sdk.openadsdk.core.vt.ajl.2.1
                        @Override // android.view.View.OnTouchListener
                        public boolean onTouch(View view, MotionEvent motionEvent) {
                            ajl.this.wu.onTouchEvent(motionEvent);
                            return false;
                        }
                    });
                }
            }
        });
    }

    public void fm(@Nullable View view, @Nullable FriendlyObstructionPurpose friendlyObstructionPurpose) {
        this.f7290ef.fm(view, friendlyObstructionPurpose);
    }

    @Override // com.bytedance.sdk.openadsdk.core.vt.lb.InterfaceC0085lb
    public void fm(String str) {
        boolean zFm;
        if (TextUtils.isEmpty(str) || this.vt == null || !this.wu.ro()) {
            return;
        }
        int iFm = bx.fm(this.ku);
        View viewFm = null;
        if (!com.bytedance.sdk.component.utils.gof.fm(str) && (this.vt.ya() == null || TextUtils.isEmpty(this.vt.ya().fm()))) {
            com.bytedance.sdk.openadsdk.core.model.dsz dszVar = new com.bytedance.sdk.openadsdk.core.model.dsz();
            dszVar.fm(str);
            this.vt.fm(dszVar);
            str = null;
        }
        this.vt.yz(true);
        com.bytedance.sdk.openadsdk.core.model.lse lseVar = this.vt;
        if (lseVar == null || lseVar.ya() == null || TextUtils.isEmpty(this.vt.ya().fm())) {
            zFm = false;
        } else {
            zFm = xgn.fm(getContext(), this.vt, iFm, this.ku, true, (Map<String, Object>) null);
            if (!zFm && !TextUtils.isEmpty(this.vt.ya().ro())) {
                str = this.vt.ya().ro();
                com.bytedance.sdk.openadsdk.yz.lb.fm(this.vt, this.ku, "open_fallback_url", (Map<String, Object>) null);
            }
        }
        String str2 = str;
        if (!zFm) {
            if (TextUtils.isEmpty(str2)) {
                return;
            } else {
                xgn.fm(getContext(), this.vt, iFm, (PAGNativeAd) null, (com.bytedance.sdk.openadsdk.core.yz.fm) null, this.ku, true, str2);
            }
        }
        if (this.wu != null) {
            ro roVar = this.onz;
            if (roVar != null) {
                viewFm = roVar.fm();
                this.onz.fm(this, 2);
            }
            com.bytedance.sdk.openadsdk.core.model.duv duvVarFm = this.wu.fm(getContext(), viewFm);
            HashMap map = new HashMap();
            map.put("click_scence", 1);
            com.bytedance.sdk.openadsdk.yz.lb.fm("click", this.vt, duvVarFm, this.ku, true, (Map<String, Object>) map, this.wu.ro() ? 1 : 2);
        }
        nt ntVar = this.wu;
        if (ntVar != null) {
            ntVar.fm();
        }
    }

    @Override // com.bytedance.sdk.openadsdk.core.vt.lb.InterfaceC0085lb
    public void fm(int i10, int i11) {
        ro roVar = this.onz;
        if (roVar != null) {
            roVar.fm(i10, i11);
        }
        this.duv = i11;
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("error_code", i11);
            jSONObject.put("render_duration", SystemClock.elapsedRealtime() - this.irt);
        } catch (Throwable unused) {
        }
        com.bytedance.sdk.openadsdk.yz.lb.ro(this.vt, this.ku, "render_html_fail", jSONObject);
    }

    @Override // com.bytedance.sdk.openadsdk.core.vt.lb.InterfaceC0085lb
    public void fm() {
        if (this.ajl.compareAndSet(false, true)) {
            this.yz = true;
            this.f7290ef.fm(getWebView());
            this.f7290ef.fm(this.jnr);
            gof();
            wey();
        }
    }
}
