package com.bytedance.sdk.openadsdk.fm.ro;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.bytedance.sdk.openadsdk.ApmHelper;
import com.bytedance.sdk.openadsdk.activity.single.IABLandingPageActivity;
import com.bytedance.sdk.openadsdk.activity.single.TTWebsiteActivity;
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGImageItem;
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGMediaView;
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGNativeAd;
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGVideoAdListener;
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGVideoMediaView;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.lb.lb;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.core.settings.ku;
import com.bytedance.sdk.openadsdk.core.vt.ef;
import com.bytedance.sdk.openadsdk.core.vt.sds;
import com.bytedance.sdk.openadsdk.core.vt.vt;
import com.bytedance.sdk.openadsdk.core.vt.wey;
import com.bytedance.sdk.openadsdk.core.widget.PAGLogoView;
import com.bytedance.sdk.openadsdk.utils.zan;
import java.lang.ref.WeakReference;
import java.util.List;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    private ro ajl;
    private boolean dsz = false;
    private com.bytedance.sdk.openadsdk.core.lb.ro duv;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private sds f7396ef;
    protected final lse fm;
    private WeakReference<com.bytedance.sdk.openadsdk.core.fhx.ro.ajl> jnr;
    private com.bytedance.sdk.openadsdk.core.lb.fm ku;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final String f7397lb;
    private WeakReference<com.bytedance.sdk.openadsdk.core.onz.ajl> onz;
    private final Context ro;
    private PAGMediaView vt;
    private com.bytedance.sdk.openadsdk.fm.ro.fm.lb wsy;
    private PAGMediaView wu;
    private boolean yz;

    public fm(Context context, lse lseVar, String str) {
        this.ro = context;
        this.fm = lseVar;
        this.f7397lb = str;
    }

    private PAGMediaView onz() {
        if (!lse.jnr(this.fm)) {
            com.bytedance.sdk.openadsdk.fm.ro.fm.lb lbVar = this.wsy;
            if (lbVar == null) {
                return null;
            }
            sds sdsVarYz = lbVar.yz();
            sdsVarYz.setTag(520093762, Boolean.TRUE);
            if (!this.dsz) {
                this.wsy.jnr();
            }
            this.dsz = true;
            return fm(sdsVarYz);
        }
        com.bytedance.sdk.openadsdk.fm.ro.fm.lb lbVar2 = this.wsy;
        if (lbVar2 == null || !(lbVar2 instanceof com.bytedance.sdk.openadsdk.fm.ro.fm.ro)) {
            return null;
        }
        wey weyVar = (wey) lbVar2.yz();
        weyVar.setTag(520093762, Boolean.TRUE);
        if (!this.dsz) {
            this.wsy.jnr();
        }
        this.dsz = true;
        return fm(weyVar);
    }

    public String ajl() {
        lse lseVar = this.fm;
        if (lseVar != null) {
            return lseVar.eys();
        }
        return null;
    }

    public void dsz() {
        com.bytedance.sdk.openadsdk.core.onz.ajl ajlVar;
        WeakReference<com.bytedance.sdk.openadsdk.core.onz.ajl> weakReference = this.onz;
        if (weakReference == null || (ajlVar = weakReference.get()) == null) {
            return;
        }
        ajlVar.fm(13);
    }

    public void duv() {
        if (this.ro != null) {
            if (com.bytedance.sdk.openadsdk.utils.ro.jnr() && gof.yz().ai()) {
                IABLandingPageActivity.fm(this.ro, this.fm, this.f7397lb);
            } else {
                TTWebsiteActivity.fm(this.ro, this.fm, this.f7397lb);
            }
        }
    }

    public PAGMediaView ef() {
        if (!lse.jnr(this.fm)) {
            List<com.bytedance.sdk.openadsdk.core.model.gof> listPyj = this.fm.pyj();
            if (listPyj == null || listPyj.isEmpty()) {
                ApmHelper.reportCustomError("images empty", "getMediaView return null", new RuntimeException());
                return null;
            }
            ImageView imageView = new ImageView(this.ro);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            com.bytedance.sdk.openadsdk.core.model.gof gofVar = listPyj.get(0);
            if (gofVar != null) {
                com.bytedance.sdk.openadsdk.vt.yz.fm(gofVar).lb(1).fm(com.bytedance.sdk.openadsdk.vt.lb.fm(this.fm, gofVar.fm(), imageView));
            }
            PAGMediaView pAGMediaViewFm = fm(imageView);
            if (this.ku == null || !ku.ro().yz(String.valueOf(this.fm.pg()))) {
                pAGMediaViewFm.setOnClickListener(null);
                pAGMediaViewFm.setOnTouchListener(null);
            } else {
                pAGMediaViewFm.setOnClickListener(this.ku);
                pAGMediaViewFm.setOnTouchListener(this.ku);
            }
            pAGMediaViewFm.setTag(520093762, Boolean.TRUE);
            PAGMediaView pAGMediaView = this.wu;
            if (pAGMediaView != null) {
                pAGMediaView.setOnClickListener(null);
                this.wu.setOnTouchListener(null);
            }
            this.wu = pAGMediaViewFm;
            return pAGMediaViewFm;
        }
        ro roVar = this.ajl;
        if (roVar == null) {
            ApmHelper.reportCustomError("mPAGFeedVideoAdImpl null", "getMediaView return null", new RuntimeException());
            return null;
        }
        View viewJnr = roVar.jnr();
        if (viewJnr == null) {
            ApmHelper.reportCustomError("adVideoView null", "getMediaView return null", new RuntimeException());
            return null;
        }
        if (viewJnr.getParent() instanceof ViewGroup) {
            ((ViewGroup) viewJnr.getParent()).removeView(viewJnr);
        }
        PAGMediaView pAGMediaView2 = this.wu;
        if (pAGMediaView2 != null) {
            pAGMediaView2.setOnClickListener(null);
            this.wu.setOnTouchListener(null);
        }
        PAGVideoMediaView pAGVideoMediaView = new PAGVideoMediaView(this.ro, viewJnr, this);
        pAGVideoMediaView.setTag(520093762, Boolean.TRUE);
        if (this.ku == null || !ku.ro().yz(String.valueOf(this.fm.pg()))) {
            com.bytedance.sdk.openadsdk.core.lb.lb lbVar = new com.bytedance.sdk.openadsdk.core.lb.lb() { // from class: com.bytedance.sdk.openadsdk.fm.ro.fm.2
                @Override // com.bytedance.sdk.openadsdk.core.lb.lb
                public void fm(View view, float f10, float f11, float f12, float f13, SparseArray<lb.fm> sparseArray, boolean z10) {
                    try {
                        ((PAGVideoMediaView) view).handleInterruptVideo();
                    } catch (Exception unused) {
                    }
                }
            };
            pAGVideoMediaView.setOnClickListener(lbVar);
            pAGVideoMediaView.setOnTouchListener(lbVar);
        } else {
            pAGVideoMediaView.setOnClickListener(this.ku);
            pAGVideoMediaView.setOnTouchListener(this.ku);
        }
        this.wu = pAGVideoMediaView;
        pAGVideoMediaView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        return pAGVideoMediaView;
    }

    public PAGMediaView fm() {
        return this.wu;
    }

    public String jnr() {
        lse lseVar = this.fm;
        if (lseVar != null) {
            return ro(lseVar);
        }
        return null;
    }

    public View ku() {
        lse lseVar;
        if (gof.fm() == null || (lseVar = this.fm) == null || !lseVar.zn() || !this.fm.lse()) {
            return null;
        }
        ImageView imageView = new ImageView(gof.fm());
        com.bytedance.sdk.openadsdk.wey.ro.ro().fm((int) zan.fm(gof.fm(), 14.0f, true), imageView, this.fm);
        return imageView;
    }

    public PAGImageItem lb() {
        lse lseVar = this.fm;
        if (lseVar == null || lseVar.ff() == null) {
            return null;
        }
        return new PAGImageItem(this.fm.ff().lb(), this.fm.ff().ro(), this.fm.ff().fm(), (float) this.fm.ff().yz());
    }

    public sds ro() {
        return this.f7396ef;
    }

    public View vt() {
        if (gof.fm() == null) {
            return null;
        }
        PAGLogoView pAGLogoView = new PAGLogoView(gof.fm()) { // from class: com.bytedance.sdk.openadsdk.fm.ro.fm.4
            int fm = 0;
            int ro = 0;

            @Override // android.widget.LinearLayout, android.view.ViewGroup, android.view.View
            public void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
                super.onLayout(z10, i10, i11, i12, i13);
                try {
                    Object parent = getParent();
                    if (parent == null || this.fm == 0 || this.ro == 0) {
                        return;
                    }
                    int width = ((View) parent).getWidth();
                    int height = ((View) parent).getHeight();
                    int i14 = this.fm;
                    if (width < i14 || height < this.ro) {
                        float fMin = Math.min(height / this.ro, width / i14);
                        int i15 = (width - this.fm) / 2;
                        if (fMin != 1.0f) {
                            setScaleX(fMin);
                            setScaleY(fMin);
                            setTranslationX(i15);
                        }
                    }
                } catch (Throwable th) {
                    th.getMessage();
                }
            }

            @Override // com.bytedance.sdk.openadsdk.core.widget.PAGLogoView, android.widget.LinearLayout, android.view.View
            public void onMeasure(int i10, int i11) {
                if (this.fm == 0) {
                    super.onMeasure(View.MeasureSpec.makeMeasureSpec(1073741823, Integer.MIN_VALUE), i11);
                    this.fm = getMeasuredWidth();
                    this.ro = this.containerHeight;
                }
                setMeasuredDimension(this.fm, this.ro);
            }
        };
        pAGLogoView.initData(this.fm);
        pAGLogoView.setOnClickListener(new View.OnClickListener() { // from class: com.bytedance.sdk.openadsdk.fm.ro.fm.5
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                fm.this.duv();
            }
        });
        return pAGLogoView;
    }

    @SuppressLint({"ClickableViewAccessibility"})
    public PAGMediaView wsy() {
        PAGMediaView pAGMediaViewEf;
        com.bytedance.sdk.openadsdk.utils.ro.fm(this.fm);
        if (this.fm.no() == 2) {
            pAGMediaViewEf = onz();
            fm(pAGMediaViewEf);
        } else {
            pAGMediaViewEf = ef();
        }
        if (pAGMediaViewEf != null) {
            pAGMediaViewEf.setMrcTrackerKey(com.bytedance.sdk.openadsdk.hi.ro.jnr.ro(this.fm));
        } else {
            pAGMediaViewEf = new PAGMediaView(this.ro) { // from class: com.bytedance.sdk.openadsdk.fm.ro.fm.1
                @Override // android.view.ViewGroup, android.view.View
                public void onAttachedToWindow() {
                    super.onAttachedToWindow();
                    com.bytedance.sdk.openadsdk.utils.yz.fm(this, fm.this.fm);
                }
            };
        }
        if (pAGMediaViewEf instanceof PAGVideoMediaView) {
            ((PAGVideoMediaView) pAGMediaViewEf).setMaterialMeta(this.fm);
        }
        this.vt = pAGMediaViewEf;
        return pAGMediaViewEf;
    }

    public PAGMediaView wu() {
        return this.vt;
    }

    public String yz() {
        lse lseVar = this.fm;
        if (lseVar != null) {
            return fm(lseVar);
        }
        return null;
    }

    private String ro(lse lseVar) {
        return !TextUtils.isEmpty(lseVar.qlg()) ? lseVar.qlg() : !TextUtils.isEmpty(lseVar.nn()) ? lseVar.nn() : "";
    }

    public void fm(com.bytedance.sdk.openadsdk.core.lb.fm fmVar) {
        this.ku = fmVar;
    }

    public void fm(com.bytedance.sdk.openadsdk.core.lb.ro roVar) {
        this.duv = roVar;
    }

    public void fm(com.bytedance.sdk.openadsdk.fm.ro.fm.lb lbVar) {
        this.wsy = lbVar;
    }

    public void fm(ro roVar) {
        this.ajl = roVar;
    }

    private lb ro(final PAGVideoAdListener pAGVideoAdListener) {
        return new lb() { // from class: com.bytedance.sdk.openadsdk.fm.ro.fm.6
            @Override // com.bytedance.sdk.openadsdk.fm.ro.lb
            public void fm(int i10, int i11) {
                PAGVideoAdListener pAGVideoAdListener2 = pAGVideoAdListener;
                if (pAGVideoAdListener2 != null) {
                    pAGVideoAdListener2.onVideoError();
                }
            }

            @Override // com.bytedance.sdk.openadsdk.fm.ro.lb
            public void lb(PAGNativeAd pAGNativeAd) {
                PAGVideoAdListener pAGVideoAdListener2 = pAGVideoAdListener;
                if (pAGVideoAdListener2 != null) {
                    pAGVideoAdListener2.onVideoAdComplete();
                }
            }

            @Override // com.bytedance.sdk.openadsdk.fm.ro.lb
            public void ro(PAGNativeAd pAGNativeAd) {
                PAGVideoAdListener pAGVideoAdListener2 = pAGVideoAdListener;
                if (pAGVideoAdListener2 != null) {
                    pAGVideoAdListener2.onVideoAdPaused();
                }
            }

            @Override // com.bytedance.sdk.openadsdk.fm.ro.lb
            public void fm(PAGNativeAd pAGNativeAd) {
                PAGVideoAdListener pAGVideoAdListener2 = pAGVideoAdListener;
                if (pAGVideoAdListener2 != null) {
                    pAGVideoAdListener2.onVideoAdPlay();
                }
            }
        };
    }

    public void fm(boolean z10) {
        this.yz = z10;
    }

    public void fm(com.bytedance.sdk.openadsdk.core.fhx.ro.ajl ajlVar) {
        this.jnr = new WeakReference<>(ajlVar);
    }

    private String fm(lse lseVar) {
        if (lseVar.iat() != null && !TextUtils.isEmpty(lseVar.iat().ro())) {
            return lseVar.iat().ro();
        }
        if (TextUtils.isEmpty(lseVar.wjb())) {
            return !TextUtils.isEmpty(lseVar.qlg()) ? lseVar.qlg() : "";
        }
        return lseVar.wjb();
    }

    private void fm(PAGMediaView pAGMediaView) {
        if (pAGMediaView == null) {
            return;
        }
        try {
            pAGMediaView.setBackgroundColor(-16777216);
        } catch (Exception unused) {
        }
    }

    private PAGMediaView fm(final View view) {
        int i10;
        if (view == null) {
            return null;
        }
        if (view.getParent() instanceof ViewGroup) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        sds sdsVar = this.f7396ef;
        if (sdsVar != null) {
            sdsVar.setClickListener(null);
            this.f7396ef.setClickCreativeListener(null);
        }
        com.bytedance.sdk.openadsdk.core.lb.ro roVar = this.duv;
        if (roVar != null && (roVar instanceof vt) && (view instanceof sds)) {
            ((sds) view).setClickListener((vt) roVar);
        }
        com.bytedance.sdk.openadsdk.core.lb.fm fmVar = this.ku;
        if (fmVar != null && (fmVar instanceof ef) && (view instanceof sds)) {
            ((sds) view).setClickCreativeListener((ef) fmVar);
        }
        PAGMediaView pAGMediaView = new PAGMediaView(this.ro) { // from class: com.bytedance.sdk.openadsdk.fm.ro.fm.3
            private void fm(boolean z10) {
                Integer num = this.fm;
                if (num != null) {
                    com.bytedance.sdk.openadsdk.hi.ro.jnr.fm(com.bytedance.sdk.openadsdk.hi.ro.jnr.fm(num), z10 ? 4 : 8);
                }
                lse lseVar = fm.this.fm;
                if (lseVar != null) {
                    com.bytedance.sdk.openadsdk.hi.fm.lb.fm(lseVar, z10);
                }
            }

            @Override // android.view.ViewGroup, android.view.View
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                com.bytedance.sdk.openadsdk.utils.yz.fm(this, fm.this.fm);
            }

            @Override // android.view.View
            public void onWindowFocusChanged(boolean z10) {
                super.onWindowFocusChanged(z10);
                if (view instanceof sds) {
                    return;
                }
                fm(z10);
            }

            @Override // com.bytedance.sdk.openadsdk.api.nativeAd.PAGMediaView
            public void setVideoAdListener(PAGVideoAdListener pAGVideoAdListener) {
                super.setVideoAdListener(pAGVideoAdListener);
                fm.this.fm(pAGVideoAdListener);
            }
        };
        int i11 = -1;
        pAGMediaView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null) {
            i11 = layoutParams.width;
            i10 = layoutParams.height;
        } else {
            i10 = -1;
        }
        FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(i11, i10);
        layoutParams2.gravity = 17;
        pAGMediaView.addView(view, layoutParams2);
        if (view instanceof sds) {
            this.f7396ef = (sds) view;
        }
        return pAGMediaView;
    }

    public void fm(PAGVideoAdListener pAGVideoAdListener) {
        com.bytedance.sdk.openadsdk.fm.ro.fm.lb lbVar;
        if (this.fm.no() == 2 && lse.jnr(this.fm) && (lbVar = this.wsy) != null && (lbVar instanceof com.bytedance.sdk.openadsdk.fm.ro.fm.ro)) {
            wey weyVar = (wey) lbVar.yz();
            if (weyVar != null) {
                weyVar.setVideoAdListener(ro(pAGVideoAdListener));
                return;
            }
            return;
        }
        ro roVar = this.ajl;
        if (roVar != null) {
            roVar.fm(ro(pAGVideoAdListener));
        }
    }

    public void fm(com.bytedance.sdk.openadsdk.core.onz.ajl ajlVar) {
        this.onz = new WeakReference<>(ajlVar);
    }
}
