package com.bytedance.sdk.openadsdk.component.reward.fm;

import a3.r;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bytedance.sdk.component.adexpress.dynamic.dynamicview.a;
import com.bytedance.sdk.component.ef.wsy;
import com.bytedance.sdk.openadsdk.core.bx;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.utils.DeviceUtils;
import com.bytedance.sdk.openadsdk.utils.zan;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* JADX INFO: loaded from: classes2.dex */
public class gqi implements com.bytedance.sdk.openadsdk.dsz.ef {
    private com.bytedance.sdk.openadsdk.core.lb.wsy ajl;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private final ro f6924ef;
    private final Activity fm;
    private boolean jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private com.bytedance.sdk.component.ef.wsy f6925lb;
    private final lse ro;
    private int vt;
    private final AtomicBoolean wsy = new AtomicBoolean(false);
    private volatile boolean wu;
    private ImageView yz;

    public gqi(ro roVar) {
        this.f6924ef = roVar;
        this.ro = roVar.ro;
        this.fm = roVar.xgn;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void lb() {
        fm(Integer.MAX_VALUE, (String) null);
    }

    private void yz() {
        this.f6925lb.jnr();
        fm(this.f6925lb);
        this.f6925lb.setDisplayZoomControls(false);
        this.f6925lb.setWebChromeClient(new WebChromeClient() { // from class: com.bytedance.sdk.openadsdk.component.reward.fm.gqi.3
            @Override // android.webkit.WebChromeClient
            public void onProgressChanged(WebView webView, int i10) {
                super.onProgressChanged(webView, i10);
                if (i10 == 100) {
                    gqi.this.lb();
                }
            }
        });
        this.f6925lb.setWebViewClient(new wsy.fm() { // from class: com.bytedance.sdk.openadsdk.component.reward.fm.gqi.4
            @Override // android.webkit.WebViewClient
            public void onPageFinished(WebView webView, String str) {
                super.onPageFinished(webView, str);
                gqi.this.lb();
            }

            @Override // android.webkit.WebViewClient
            public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
                super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
                if (webResourceRequest.isForMainFrame()) {
                    gqi.this.fm(webResourceResponse.getStatusCode(), webResourceRequest.getUrl() != null ? webResourceRequest.getUrl().toString() : null);
                }
            }

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

    @Override // com.bytedance.sdk.openadsdk.dsz.ef
    public void lb(int i10) {
        int i11 = this.vt;
        if (i11 == 0 && i10 > 0) {
            this.ro.mt().fm().ef(this.f6924ef.sds.dsz());
        } else if (i11 > 0 && i10 == 0) {
            this.ro.mt().fm().wu(this.f6924ef.sds.dsz());
        }
        this.vt = i10;
    }

    public void ro() {
        DeviceUtils.AudioInfoReceiver.ro(this);
        com.bytedance.sdk.component.ef.wsy wsyVar = this.f6925lb;
        if (wsyVar != null) {
            bx.fm(wsyVar.getWebView());
        }
    }

    public void fm() {
        DeviceUtils.AudioInfoReceiver.fm(this);
        this.vt = DeviceUtils.wsy();
        if (this.ro.mt() != null) {
            this.ajl = new com.bytedance.sdk.openadsdk.core.lb.wsy("VAST_END_CARD", this.ro.mt()) { // from class: com.bytedance.sdk.openadsdk.component.reward.fm.gqi.1
            };
            com.bytedance.sdk.openadsdk.core.onz.lb lbVarLb = this.ro.mt().lb();
            if (lbVarLb != null) {
                String strYz = lbVarLb.yz();
                if (!TextUtils.isEmpty(strYz)) {
                    this.jnr = true;
                    this.yz = (ImageView) this.f6924ef.f6937gc.findViewById(com.bytedance.sdk.openadsdk.utils.qhl.vt);
                    fm(lbVarLb.fm(), lbVarLb.ro());
                    com.bytedance.sdk.openadsdk.vt.yz.fm(strYz).fm(lbVarLb.fm()).ro(lbVarLb.ro()).jnr(zan.jnr(com.bytedance.sdk.openadsdk.core.gof.fm())).yz(zan.lb(com.bytedance.sdk.openadsdk.core.gof.fm())).lb(1).fm(new com.bytedance.sdk.openadsdk.vt.ro(this.ro, strYz, new fm(this.yz, strYz, this, this.wsy)));
                    return;
                }
                com.bytedance.sdk.component.ef.wsy wsyVar = (com.bytedance.sdk.component.ef.wsy) this.f6924ef.f6937gc.findViewById(com.bytedance.sdk.openadsdk.utils.qhl.ku);
                this.f6925lb = wsyVar;
                if (wsyVar == null) {
                    return;
                }
                yz();
                String strLb = lbVarLb.lb();
                if (strLb != null) {
                    this.jnr = true;
                    if (strLb.startsWith("http")) {
                        this.f6925lb.a_(strLb);
                        return;
                    }
                    String strFm = com.bytedance.sdk.openadsdk.core.onz.jnr.fm(strLb);
                    String str = TextUtils.isEmpty(strFm) ? strLb : strFm;
                    this.f6925lb.setDefaultTextEncodingName("UTF -8");
                    this.f6925lb.fm(null, str, "text/html", "UTF-8", null);
                }
            }
        }
    }

    public static class fm implements com.bytedance.sdk.component.jnr.irt {
        private final WeakReference<ImageView> fm;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private final WeakReference<gqi> f6927lb;
        private final String ro;
        private final AtomicBoolean yz;

        public fm(ImageView imageView, String str, gqi gqiVar, AtomicBoolean atomicBoolean) {
            this.fm = new WeakReference<>(imageView);
            this.ro = str;
            this.f6927lb = new WeakReference<>(gqiVar);
            this.yz = atomicBoolean;
        }

        @Override // com.bytedance.sdk.component.jnr.irt
        public void fm(com.bytedance.sdk.component.jnr.ku kuVar) {
            gqi gqiVar;
            ImageView imageView = this.fm.get();
            if (imageView == null || (gqiVar = this.f6927lb.get()) == null || kuVar == null) {
                return;
            }
            Object objRo = kuVar.ro();
            if (objRo == null) {
                gqiVar.fm(-1, this.ro);
                return;
            }
            if (objRo instanceof Bitmap) {
                imageView.setImageBitmap((Bitmap) objRo);
            } else if (objRo instanceof Drawable) {
                if (Build.VERSION.SDK_INT >= 28 && r.D(objRo)) {
                    a.h(objRo).start();
                }
                imageView.setImageDrawable((Drawable) objRo);
            }
            this.yz.set(true);
            gqiVar.lb();
        }

        @Override // com.bytedance.sdk.component.jnr.irt
        public void fm(int i10, String str, @Nullable Throwable th) {
            gqi gqiVar;
            ImageView imageView = this.fm.get();
            if (imageView == null || (gqiVar = this.f6927lb.get()) == null) {
                return;
            }
            imageView.setVisibility(8);
            gqiVar.fm(-2, this.ro);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(final int i10, final String str) {
        if (this.wu) {
            return;
        }
        this.wu = true;
        if (i10 == Integer.MAX_VALUE) {
            lse lseVar = this.ro;
            com.bytedance.sdk.openadsdk.yz.lb.ro(lseVar, lseVar.jnr(), "load_vast_endcard_success", (JSONObject) null);
        } else {
            com.bytedance.sdk.openadsdk.yz.lb.fm(new com.bytedance.sdk.component.wu.ro.lb("load_vast_endcard_fail") { // from class: com.bytedance.sdk.openadsdk.component.reward.fm.gqi.2
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        JSONObject jSONObject = new JSONObject();
                        jSONObject.put("reason_code", i10);
                        jSONObject.put("error_code", i10);
                        String str2 = str;
                        if (str2 != null) {
                            jSONObject.put(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL, str2);
                        }
                        com.bytedance.sdk.openadsdk.yz.lb.ro(gqi.this.ro, gqi.this.ro.jnr(), "load_vast_endcard_fail", jSONObject);
                    } catch (Exception unused) {
                    }
                }
            });
        }
    }

    private void fm(com.bytedance.sdk.component.ef.wsy wsyVar) {
        wsyVar.setVerticalScrollBarEnabled(false);
        wsyVar.setHorizontalScrollBarEnabled(false);
        wsyVar.setMixedContentMode(0);
        wsyVar.setJavaScriptEnabled(true);
        wsyVar.setJavaScriptCanOpenWindowsAutomatically(true);
        wsyVar.setDomStorageEnabled(true);
        wsyVar.setDatabaseEnabled(true);
        wsyVar.setCacheMode(-1);
        wsyVar.setAllowFileAccess(false);
        wsyVar.setSupportZoom(true);
        wsyVar.setBuiltInZoomControls(true);
        wsyVar.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        wsyVar.setUseWideViewPort(true);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean fm(String str) {
        lse lseVar;
        if (str == null || (lseVar = this.ro) == null || lseVar.mt() == null || this.ajl == null) {
            return false;
        }
        this.ro.mt().ajl(str);
        this.ajl.onClick(this.f6925lb);
        return true;
    }

    @SuppressLint({"ClickableViewAccessibility"})
    private void fm(int i10, int i11) {
        if (i10 == 0 || i11 == 0 || this.yz == null) {
            return;
        }
        int iLb = zan.lb((Context) this.fm);
        int iJnr = zan.jnr(this.fm);
        if (i10 / i11 <= iLb / iJnr) {
            iLb = (int) Math.ceil(r5 * r4);
        } else {
            iJnr = (int) Math.ceil(r5 / r4);
        }
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) this.yz.getLayoutParams();
        layoutParams.width = iLb;
        layoutParams.height = iJnr;
        layoutParams.gravity = 17;
        this.yz.setLayoutParams(layoutParams);
        this.yz.setOnClickListener(this.ajl);
        this.yz.setOnTouchListener(this.ajl);
    }

    @SuppressLint({"ClickableViewAccessibility"})
    public boolean fm(qhl qhlVar) {
        com.bytedance.sdk.openadsdk.core.onz.lb lbVarLb;
        if (!this.jnr) {
            return false;
        }
        if (this.yz == null || !this.wsy.get()) {
            com.bytedance.sdk.component.ef.wsy wsyVar = this.f6925lb;
            if (wsyVar != null) {
                wsyVar.setVisibility(0);
                if (this.f6925lb.getWebView() != null) {
                    this.f6925lb.getWebView().setOnTouchListener(this.ajl);
                }
            }
        } else {
            this.yz.setVisibility(0);
        }
        lse lseVar = this.ro;
        if (lseVar == null || lseVar.mt() == null || (lbVarLb = this.ro.mt().lb()) == null) {
            return true;
        }
        lbVarLb.ro(qhlVar != null ? qhlVar.dsz() : -1L);
        return true;
    }

    public void fm(com.bytedance.sdk.openadsdk.core.lb.jnr jnrVar) {
        com.bytedance.sdk.openadsdk.core.lb.wsy wsyVar = this.ajl;
        if (wsyVar != null) {
            wsyVar.fm(jnrVar);
        }
    }
}
