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

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import com.bytedance.sdk.component.utils.maa;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.activity.single.IABLandingPageActivity;
import com.bytedance.sdk.openadsdk.activity.single.TTWebsiteActivity;
import com.bytedance.sdk.openadsdk.api.banner.PAGBannerAdInteractionCallback;
import com.bytedance.sdk.openadsdk.api.banner.PAGBannerAdInteractionListener;
import com.bytedance.sdk.openadsdk.api.banner.PAGBannerAdWrapperListener;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.lb.ro;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.core.vt;
import com.bytedance.sdk.openadsdk.core.vt.sds;
import com.bytedance.sdk.openadsdk.core.widget.PAGLogoView;
import com.bytedance.sdk.openadsdk.core.wu;
import com.bytedance.sdk.openadsdk.core.yz.wsy;
import com.bytedance.sdk.openadsdk.oem.IPBroadcastReceiver;
import com.bytedance.sdk.openadsdk.utils.bx;
import com.bytedance.sdk.openadsdk.utils.mq;
import com.bytedance.sdk.openadsdk.utils.tzk;
import com.bytedance.sdk.openadsdk.utils.xgn;
import com.bytedance.sdk.openadsdk.utils.zan;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    private final Context ajl;
    private wsy duv;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private boolean f7367ef;
    protected yz fm;
    private long gqi;
    private PAGBannerAdWrapperListener jnr;
    private sds ku;
    private boolean onz;
    protected AdSlot ro;
    private com.bytedance.sdk.openadsdk.sds.fm.fm.lb vt;
    private final boolean wu;
    private lse yz;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private String f7368lb = "banner_ad";
    private long wsy = 0;
    private final AtomicBoolean dsz = new AtomicBoolean(false);
    private final int fhx = 1;
    private final int irt = 0;
    private int qhl = -1;
    private final View.OnAttachStateChangeListener gof = new View.OnAttachStateChangeListener() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.1
        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewAttachedToWindow(View view) {
            if (fm.this.qhl == 0 || fm.this.f7367ef) {
                return;
            }
            fm fmVar = fm.this;
            fmVar.fm(fmVar.fm.getCurView(), fm.this.yz);
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewDetachedFromWindow(View view) {
            if (fm.this.onz) {
                fm.this.qhl = 0;
            } else {
                fm.this.ajl();
            }
        }
    };

    /* JADX INFO: renamed from: com.bytedance.sdk.openadsdk.core.yz.fm$fm, reason: collision with other inner class name */
    public static class C0089fm extends com.bytedance.sdk.component.wu.ro.lb {
        boolean fm;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        WeakReference<fm> f7372lb;
        lse ro;

        public C0089fm(boolean z10, lse lseVar, fm fmVar) {
            super("ReportWindowFocusChangedAdShow");
            this.fm = z10;
            this.ro = lseVar;
            this.f7372lb = new WeakReference<>(fmVar);
        }

        @Override // java.lang.Runnable
        public void run() {
            WeakReference<fm> weakReference = this.f7372lb;
            if (weakReference == null || weakReference.get() == null) {
                return;
            }
            this.f7372lb.get().ro(this.fm, this.ro);
        }
    }

    public fm(Context context, lse lseVar, AdSlot adSlot, wsy wsyVar, boolean z10) {
        this.onz = false;
        this.ajl = context;
        this.yz = lseVar;
        this.ro = adSlot;
        this.duv = wsyVar;
        this.onz = z10;
        fm(context, lseVar, adSlot);
        this.wu = false;
        this.f7367ef = false;
    }

    private void duv() {
        this.yz.fm(SystemClock.elapsedRealtime());
        this.fm.jnr();
    }

    private wsy.fm ku() {
        return new wsy.fm() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.7
            @Override // com.bytedance.sdk.openadsdk.core.yz.wsy.fm
            public void fm() {
                int width = fm.this.ku.getWidth();
                int height = fm.this.ku.getHeight();
                View viewFm = fm(((double) height) >= Math.floor((((double) width) * 450.0d) / 600.0d));
                fm.this.ku.hlt();
                fm.this.ku.removeAllViews();
                fm.this.ku.addView(viewFm, new ViewGroup.LayoutParams(width, height));
                fm.this.ku.setClickCreativeListener(null);
                fm.this.ku.setClickListener(null);
                if (fm.this.jnr != null) {
                    fm.this.jnr.onAdDismissed();
                }
                fm.this.f7367ef = true;
            }

            private View fm(boolean z10) {
                com.bytedance.sdk.openadsdk.core.ajl.lb lbVar = new com.bytedance.sdk.openadsdk.core.ajl.lb(fm.this.ajl);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-1, -1);
                lbVar.setBackgroundColor(-1);
                lbVar.setLayoutParams(layoutParams);
                View view = new View(fm.this.ajl);
                ViewGroup.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(-1, -1);
                view.setAlpha(0.3f);
                view.setBackgroundColor(Color.parseColor("#F3F7F8"));
                lbVar.addView(view, layoutParams2);
                com.bytedance.sdk.openadsdk.core.ajl.lb lbVar2 = new com.bytedance.sdk.openadsdk.core.ajl.lb(fm.this.ajl);
                ViewGroup.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(-1, -1);
                if (z10) {
                    lbVar2.setBackground(maa.lb(fm.this.ajl, "tt_ad_closed_background_300_250"));
                } else {
                    lbVar2.setBackground(maa.lb(fm.this.ajl, "tt_ad_closed_background_320_50"));
                }
                lbVar.addView(lbVar2, layoutParams3);
                PAGLogoView pAGLogoViewCreatePAGLogoViewByMaterial = PAGLogoView.createPAGLogoViewByMaterial(fm.this.ajl, fm.this.yz);
                pAGLogoViewCreatePAGLogoViewByMaterial.setId(520093739);
                FrameLayout.LayoutParams layoutParams4 = new FrameLayout.LayoutParams(-2, -2);
                if (z10) {
                    int iRo = zan.ro(fm.this.ajl, 16.0f);
                    layoutParams4.width = zan.ro(fm.this.ajl, 77.0f);
                    layoutParams4.height = zan.ro(fm.this.ajl, 14.0f);
                    layoutParams4.leftMargin = iRo;
                    layoutParams4.topMargin = iRo;
                } else {
                    int iRo2 = zan.ro(fm.this.ajl, 8.0f);
                    layoutParams4.width = zan.ro(fm.this.ajl, 45.0f);
                    layoutParams4.height = zan.ro(fm.this.ajl, 8.18f);
                    layoutParams4.leftMargin = iRo2;
                    layoutParams4.topMargin = iRo2;
                }
                lbVar2.addView(pAGLogoViewCreatePAGLogoViewByMaterial, layoutParams4);
                com.bytedance.sdk.openadsdk.core.ajl.wu wuVar = new com.bytedance.sdk.openadsdk.core.ajl.wu(fm.this.ajl);
                FrameLayout.LayoutParams layoutParams5 = new FrameLayout.LayoutParams(-2, -2);
                layoutParams5.gravity = 17;
                wuVar.setAlpha(0.5f);
                wuVar.setLines(1);
                wuVar.setText(maa.fm(fm.this.ajl, "tt_ad_is_closed"));
                if (z10) {
                    wuVar.setTextSize(18.0f);
                } else {
                    wuVar.setTextSize(12.0f);
                }
                lbVar2.addView(wuVar, layoutParams5);
                View.OnClickListener onClickListener = new View.OnClickListener() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.7.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view2) {
                        if (com.bytedance.sdk.openadsdk.utils.ro.jnr() && gof.yz().ai()) {
                            IABLandingPageActivity.fm(fm.this.ajl, fm.this.yz, fm.this.f7368lb);
                        } else {
                            TTWebsiteActivity.fm(fm.this.ajl, fm.this.yz, fm.this.f7368lb);
                        }
                    }
                };
                pAGLogoViewCreatePAGLogoViewByMaterial.setOnClickListener(onClickListener);
                wuVar.setOnClickListener(onClickListener);
                return lbVar;
            }
        };
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void vt() {
        duv();
    }

    public void ajl() {
        this.qhl = 1;
        this.fm.yz();
    }

    public void ef() {
        yz yzVar = this.fm;
        if (yzVar instanceof lb) {
            ((lb) yzVar).fm();
        }
    }

    public void jnr() {
        this.wsy = System.currentTimeMillis();
    }

    public com.bytedance.sdk.openadsdk.gqi.lb.fm lb() {
        yz yzVar = this.fm;
        if (yzVar instanceof lb) {
            return ((lb) yzVar).getVideoModel();
        }
        return null;
    }

    public void wsy() {
        if (this.fm != null) {
            try {
                ajl();
                this.fm.removeOnAttachStateChangeListener(this.gof);
            } catch (Throwable unused) {
            }
        }
    }

    public void wu() {
        yz yzVar = this.fm;
        if (yzVar instanceof lb) {
            ((lb) yzVar).ro();
        }
    }

    public void yz() {
        ro(this.yz);
        this.wsy = 0L;
    }

    public boolean ro() {
        return this.fm instanceof lb;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void ro(boolean z10, lse lseVar) {
        try {
            if (z10) {
                this.wsy = System.currentTimeMillis();
            } else {
                if (this.wsy <= 0 || this.ku == null) {
                    return;
                }
                String strValueOf = String.valueOf(System.currentTimeMillis() - this.wsy);
                this.wsy = 0L;
                com.bytedance.sdk.openadsdk.yz.lb.fm(strValueOf, lseVar, this.f7368lb, this.ku.getAdShowTime());
            }
        } catch (Exception e10) {
            wey.lb("PAGBannerAdImpl", e10.getMessage());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void ro(lse lseVar) {
        if (this.wsy <= 0 || lseVar == null) {
            return;
        }
        try {
            if (this.ku != null) {
                String strValueOf = String.valueOf(System.currentTimeMillis() - this.wsy);
                this.wsy = 0L;
                com.bytedance.sdk.openadsdk.yz.lb.fm(strValueOf, lseVar, this.f7368lb, this.ku.getAdShowTime());
            }
        } catch (Exception e10) {
            wey.lb("PAGBannerAdImpl", e10.getMessage());
        }
    }

    public void fm(int i10) {
        yz yzVar = this.fm;
        if (yzVar != null) {
            yzVar.setCurrentIndex(i10);
        }
    }

    public void fm(PAGBannerAdInteractionCallback pAGBannerAdInteractionCallback) {
        wu wuVar = new wu(pAGBannerAdInteractionCallback);
        this.jnr = wuVar;
        this.fm.setExpressInteractionListener(wuVar);
    }

    public void fm(PAGBannerAdInteractionListener pAGBannerAdInteractionListener) {
        wu wuVar = new wu(pAGBannerAdInteractionListener);
        this.jnr = wuVar;
        this.fm.setExpressInteractionListener(wuVar);
    }

    public View fm() {
        com.bytedance.sdk.openadsdk.utils.ro.fm(this.yz);
        IPBroadcastReceiver.ro(this.ajl, this.yz);
        return this.fm;
    }

    private void fm(Context context, lse lseVar, AdSlot adSlot) {
        if (lseVar.ex() != null) {
            lb lbVar = new lb(context, lseVar, adSlot, this.onz);
            this.fm = lbVar;
            lbVar.addOnAttachStateChangeListener(this.gof);
        } else {
            yz yzVar = new yz(context, lseVar, adSlot, this.onz);
            this.fm = yzVar;
            yzVar.addOnAttachStateChangeListener(this.gof);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(boolean z10, lse lseVar) {
        if (z10 && this.yz.vu() && !this.yz.mq()) {
            this.yz.wsy(true);
            lse lseVar2 = this.yz;
            com.bytedance.sdk.openadsdk.yz.lb.fm(lseVar2, this.f7368lb, lseVar2.nqr());
        }
        xgn.ro(new C0089fm(z10, lseVar, this), 10);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(View view, sds sdsVar, lse lseVar, String str, wsy.fm fmVar) {
        vt.ro().fm(str, fmVar);
        this.wsy = System.currentTimeMillis();
        try {
            JSONObject jSONObject = new JSONObject();
            if (sdsVar != null) {
                jSONObject.put("dynamic_show_type", sdsVar.getDynamicShowType());
                sdsVar.fm(jSONObject, lseVar);
            }
            if (view != null) {
                JSONObject jSONObject2 = new JSONObject();
                try {
                    jSONObject2.put("width", view.getWidth());
                    jSONObject2.put("height", view.getHeight());
                    jSONObject2.put("alpha", view.getAlpha());
                } catch (Throwable unused) {
                }
                jSONObject.put("root_view", jSONObject2.toString());
            }
            com.bytedance.sdk.openadsdk.yz.lb.fm(lseVar, this.f7368lb, jSONObject, (JSONObject) null);
            com.bytedance.sdk.openadsdk.hi.fm.lb.fm(lseVar);
        } catch (JSONException unused2) {
            wey.lb("PAGBannerAdImpl", "onShowFun json error");
        }
        PAGBannerAdWrapperListener pAGBannerAdWrapperListener = this.jnr;
        if (pAGBannerAdWrapperListener != null) {
            pAGBannerAdWrapperListener.onAdShow(view, lseVar.oy());
        }
        if (lseVar.mu()) {
            bx.fm(lseVar, view);
        }
    }

    private com.bytedance.sdk.openadsdk.sds.fm.fm.lb fm(lse lseVar) {
        if (lseVar.oy() == 4) {
            return com.bytedance.sdk.openadsdk.sds.fm.fm.yz.fm(this.ajl, this.f7368lb);
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    @SuppressLint({"ClickableViewAccessibility"})
    public void fm(@NonNull final sds sdsVar, @NonNull final lse lseVar) {
        com.bytedance.sdk.openadsdk.core.wu wuVar;
        if (sdsVar == null || lseVar == null) {
            return;
        }
        this.yz = lseVar;
        this.vt = fm(lseVar);
        this.ku = sdsVar;
        final String strFm = mq.fm();
        final wsy.fm fmVarKu = ku();
        sdsVar.setClosedListenerKey(strFm);
        sdsVar.setBannerClickClosedListener(fmVarKu);
        sdsVar.setBackupListener(new com.bytedance.sdk.component.adexpress.ro.lb() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.2
            @Override // com.bytedance.sdk.component.adexpress.ro.lb
            public boolean fm(ViewGroup viewGroup, int i10) {
                try {
                    sdsVar.maa();
                    if (!fm.this.yz.zn()) {
                        ro roVar = new ro(sdsVar.getContext());
                        roVar.setClosedListenerKey(strFm);
                        roVar.fm(fm.this.yz, sdsVar, fm.this.vt);
                        roVar.setAdInteractionListener(fm.this.jnr);
                        return true;
                    }
                    ef efVar = new ef(sdsVar.getContext());
                    efVar.setClosedListenerKey(strFm);
                    efVar.fm(fm.this.yz, sdsVar, fm.this.vt);
                    efVar.setAdInteractionListener(fm.this.jnr);
                    sdsVar.setVastVideoHelper(efVar);
                    return true;
                } catch (Exception unused) {
                    return false;
                }
            }
        });
        if (!this.wu) {
            com.bytedance.sdk.openadsdk.core.wu wuVarFm = fm(sdsVar);
            if (wuVarFm == null) {
                wuVarFm = new com.bytedance.sdk.openadsdk.core.wu(this.ajl, sdsVar, this.duv.fm());
                sdsVar.addView(wuVarFm);
            }
            wuVar = wuVarFm;
            wuVar.setAdType(1);
            wuVar.setCallback(new wu.fm() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.3
                @Override // com.bytedance.sdk.openadsdk.core.wu.fm
                public void fm(boolean z10) {
                    fm.this.fm(z10, lseVar);
                }

                @Override // com.bytedance.sdk.openadsdk.core.wu.fm
                public void ro() {
                    fm fmVar = fm.this;
                    fmVar.ro(fmVar.yz);
                }

                @Override // com.bytedance.sdk.openadsdk.core.wu.fm
                public void fm() {
                    if (fm.this.qhl != 0) {
                        fm.this.vt();
                    }
                }

                @Override // com.bytedance.sdk.openadsdk.core.wu.fm
                public void fm(View view) {
                    if (fm.this.dsz.compareAndSet(false, true)) {
                        fm.this.fm(view, sdsVar, lseVar, strFm, fmVarKu);
                    }
                    yz yzVar = fm.this.fm;
                    if (yzVar == null || yzVar.getCurView() == null) {
                        return;
                    }
                    fm.this.fm.getCurView().ku();
                    fm.this.fm.getCurView().gqi();
                    fm.this.fm.setIsShow(true);
                }
            });
        } else {
            tzk.fm(sdsVar, true, 1, this.duv.fm(), new tzk.ro() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.4
                @Override // com.bytedance.sdk.openadsdk.utils.tzk.ro
                public void fm(boolean z10) {
                    fm.this.fm(z10, lseVar);
                }

                @Override // com.bytedance.sdk.openadsdk.utils.tzk.ro
                public void ro() {
                    fm fmVar = fm.this;
                    fmVar.ro(fmVar.yz);
                }

                @Override // com.bytedance.sdk.openadsdk.utils.tzk.ro
                public void fm() {
                    if (fm.this.qhl != 0) {
                        fm.this.vt();
                    }
                }

                @Override // com.bytedance.sdk.openadsdk.utils.tzk.ro
                public void fm(View view, boolean z10) {
                    if (!z10) {
                        com.bytedance.sdk.openadsdk.hi.fm.lb.fm(fm.this.yz, 8);
                        return;
                    }
                    com.bytedance.sdk.openadsdk.hi.fm.lb.fm(fm.this.yz, 4);
                    if (fm.this.dsz.compareAndSet(false, true)) {
                        fm.this.fm(view, sdsVar, lseVar, strFm, fmVarKu);
                    }
                    yz yzVar = fm.this.fm;
                    if (yzVar == null || yzVar.getCurView() == null) {
                        return;
                    }
                    fm.this.fm.getCurView().ku();
                    fm.this.fm.getCurView().gqi();
                    fm.this.fm.setIsShow(true);
                }
            }, null);
            wuVar = null;
        }
        Context contextFm = com.bytedance.sdk.component.utils.ro.fm(sdsVar);
        if (contextFm == null) {
            contextFm = this.ajl;
        }
        com.bytedance.sdk.openadsdk.core.vt.vt vtVar = new com.bytedance.sdk.openadsdk.core.vt.vt(contextFm, lseVar, this.f7368lb, 2);
        vtVar.ro(sdsVar);
        vtVar.fm(this);
        vtVar.fm(this.vt);
        vtVar.fm(new ro.fm() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.5
            @Override // com.bytedance.sdk.openadsdk.core.lb.ro.fm
            public void fm(View view, int i10) {
                if (fm.this.jnr != null) {
                    fm.this.jnr.onAdClicked();
                }
            }
        });
        sdsVar.setClickListener(vtVar);
        com.bytedance.sdk.openadsdk.core.vt.ef efVar = new com.bytedance.sdk.openadsdk.core.vt.ef(this.ajl, lseVar, this.f7368lb, 2);
        efVar.ro(sdsVar);
        efVar.fm(this);
        efVar.fm(new ro.fm() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.6
            @Override // com.bytedance.sdk.openadsdk.core.lb.ro.fm
            public void fm(View view, int i10) {
                if (fm.this.jnr != null) {
                    fm.this.jnr.onAdClicked();
                }
            }
        });
        sds sdsVar2 = this.ku;
        if (sdsVar2 instanceof com.bytedance.sdk.openadsdk.core.vt.wey) {
            efVar.fm(((com.bytedance.sdk.openadsdk.core.vt.wey) sdsVar2).getVideoController());
        }
        efVar.fm(this.vt);
        sdsVar.setClickCreativeListener(efVar);
        if (this.wu) {
            return;
        }
        wuVar.setNeedCheckingShow(true);
    }

    private com.bytedance.sdk.openadsdk.core.wu fm(ViewGroup viewGroup) {
        if (viewGroup == null) {
            return null;
        }
        for (int i10 = 0; i10 < viewGroup.getChildCount(); i10++) {
            try {
                View childAt = viewGroup.getChildAt(i10);
                if (childAt instanceof com.bytedance.sdk.openadsdk.core.wu) {
                    return (com.bytedance.sdk.openadsdk.core.wu) childAt;
                }
            } catch (Exception unused) {
            }
            return null;
        }
        return null;
    }

    public void fm(final com.bytedance.sdk.openadsdk.fm.fm.ro roVar) {
        long jCurrentTimeMillis = System.currentTimeMillis();
        if (jCurrentTimeMillis - this.gqi > 500) {
            this.gqi = jCurrentTimeMillis;
            yz yzVar = this.fm;
            if (yzVar instanceof lb) {
                yzVar.postDelayed(new Runnable() { // from class: com.bytedance.sdk.openadsdk.core.yz.fm.8
                    @Override // java.lang.Runnable
                    public void run() {
                        ((lb) fm.this.fm).fm(roVar);
                    }
                }, 500L);
            }
        }
    }
}
