package com.bytedance.sdk.component.adexpress.dynamic.fm;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.bytedance.sdk.component.adexpress.dynamic.dynamicview.DynamicRootView;
import com.bytedance.sdk.component.adexpress.dynamic.dynamicview.hxv;
import com.bytedance.sdk.component.adexpress.dynamic.yz.ajl;
import com.bytedance.sdk.component.adexpress.lb;
import com.bytedance.sdk.component.adexpress.ro.dsz;
import com.bytedance.sdk.component.adexpress.ro.ku;
import com.bytedance.sdk.component.adexpress.ro.onz;
import com.bytedance.sdk.component.adexpress.ro.wsy;
import com.bytedance.sdk.component.adexpress.ro.wu;
import com.bytedance.sdk.component.adexpress.ro.yz;
import com.bytedance.sdk.component.utils.ef;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public class fm implements ku, yz<DynamicRootView> {
    private dsz ajl;
    private DynamicRootView fm;
    private wu jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private Context f6475lb;
    private com.bytedance.sdk.component.adexpress.dynamic.jnr.wu ro;
    private ScheduledFuture<?> wsy;
    private AtomicBoolean wu = new AtomicBoolean(false);
    private wsy yz;

    /* JADX INFO: renamed from: com.bytedance.sdk.component.adexpress.dynamic.fm.fm$fm, reason: collision with other inner class name */
    public class RunnableC0030fm implements Runnable {
        private int ro;

        public RunnableC0030fm(int i10) {
            this.ro = i10;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.ro == 2) {
                fm.this.fm.callBackRenderFail(fm.this.ro instanceof com.bytedance.sdk.component.adexpress.dynamic.jnr.wsy ? 127 : 117, null);
            }
        }
    }

    public fm(Context context, boolean z10, com.bytedance.sdk.component.adexpress.dynamic.jnr.wu wuVar, dsz dszVar, com.bytedance.sdk.component.adexpress.dynamic.ajl.fm fmVar) {
        this.f6475lb = context;
        DynamicRootView dynamicRootView = new DynamicRootView(context, z10, dszVar, fmVar);
        this.fm = dynamicRootView;
        this.ro = wuVar;
        this.ajl = dszVar;
        dynamicRootView.setRenderListener(this);
        this.ajl = dszVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void ajl() {
        this.ajl.jnr().ro(lb());
        JSONObject jSONObjectLb = this.ajl.lb();
        if (com.bytedance.sdk.component.adexpress.fm.ro.ro.fm(jSONObjectLb)) {
            this.ro.fm(new com.bytedance.sdk.component.adexpress.dynamic.ajl.ro() { // from class: com.bytedance.sdk.component.adexpress.dynamic.fm.fm.2
                @Override // com.bytedance.sdk.component.adexpress.dynamic.ajl.ro
                public void fm(final com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar) {
                    fm.this.wu();
                    fm.this.ajl.jnr().lb(fm.this.lb());
                    fm.this.fm(wuVar);
                    fm.this.ro(wuVar);
                    if (Looper.getMainLooper() == Looper.myLooper()) {
                        fm.this.lb(wuVar);
                    } else {
                        new Handler(Looper.getMainLooper()).post(new Runnable() { // from class: com.bytedance.sdk.component.adexpress.dynamic.fm.fm.2.1
                            @Override // java.lang.Runnable
                            public void run() {
                                fm.this.lb(wuVar);
                            }
                        });
                    }
                    if (fm.this.fm == null || wuVar == null) {
                        return;
                    }
                    fm.this.fm.setBgColor(wuVar.fm());
                    fm.this.fm.setBgMaterialCenterCalcColor(wuVar.ro());
                }
            });
            this.ro.fm(this.ajl);
            return;
        }
        int i10 = this.ro instanceof com.bytedance.sdk.component.adexpress.dynamic.jnr.wsy ? 123 : 113;
        DynamicRootView dynamicRootView = this.fm;
        StringBuilder sb = new StringBuilder("data null is ");
        sb.append(jSONObjectLb == null);
        dynamicRootView.callBackRenderFail(i10, sb.toString());
    }

    private boolean wsy() {
        DynamicRootView dynamicRootView = this.fm;
        return (dynamicRootView == null || dynamicRootView.getChildCount() == 0) ? false : true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void wu() {
        try {
            ScheduledFuture<?> scheduledFuture = this.wsy;
            if (scheduledFuture == null || scheduledFuture.isCancelled()) {
                return;
            }
            this.wsy.cancel(false);
            this.wsy = null;
        } catch (Throwable unused) {
        }
    }

    public void fm(wu wuVar) {
        this.jnr = wuVar;
    }

    public DynamicRootView yz() {
        return this.fm;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void ro(com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar) {
        float fWsy;
        float fAjl;
        List<com.bytedance.sdk.component.adexpress.dynamic.yz.wu> listKu;
        if (wuVar == null) {
            return;
        }
        List<com.bytedance.sdk.component.adexpress.dynamic.yz.wu> listKu2 = wuVar.ku();
        if (listKu2 == null || listKu2.size() <= 0) {
            fWsy = 0.0f;
        } else {
            fWsy = 0.0f;
            for (com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar2 : listKu2) {
                if (wuVar2.wsy() > wuVar.wsy() - wuVar2.ef() || (listKu = wuVar2.ku()) == null || listKu.size() <= 0) {
                    fAjl = 0.0f;
                } else {
                    fAjl = 0.0f;
                    for (com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar3 : listKu) {
                        if (wuVar3.vt().ro().equals("logo-union")) {
                            fAjl = wuVar3.vt().ajl();
                            fWsy = ((wuVar.wsy() + (-fAjl)) - wuVar2.wsy()) + wuVar2.vt().jnr().lsa();
                        }
                    }
                }
                ro(wuVar2);
                if (fAjl <= -15.0f) {
                    wuVar2.ajl(wuVar2.ef() - fAjl);
                    wuVar2.yz(wuVar2.wsy() + fAjl);
                    for (com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar4 : wuVar2.ku()) {
                        wuVar4.yz(wuVar4.wsy() - fAjl);
                    }
                }
            }
        }
        com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVarDuv = wuVar.duv();
        if (wuVarDuv == null) {
            return;
        }
        float fAjl2 = wuVar.ajl() - wuVarDuv.ajl();
        float fWsy2 = wuVar.wsy() - wuVarDuv.wsy();
        wuVar.lb(fAjl2);
        wuVar.yz(fWsy2);
        if (fWsy > 0.0f) {
            wuVar.yz(wuVar.wsy() - fWsy);
            wuVar.ajl(wuVar.ef() + fWsy);
            for (com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar5 : wuVar.ku()) {
                wuVar5.yz(wuVar5.wsy() + fWsy);
            }
        }
    }

    @Override // com.bytedance.sdk.component.adexpress.ro.yz
    public int lb() {
        return this.ro instanceof com.bytedance.sdk.component.adexpress.dynamic.jnr.wsy ? 3 : 2;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void lb(com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar) {
        if (wuVar == null) {
            this.fm.callBackRenderFail(this.ro instanceof com.bytedance.sdk.component.adexpress.dynamic.jnr.wsy ? 123 : 113, "layoutUnit is null");
            return;
        }
        this.ajl.jnr().yz(lb());
        try {
            this.fm.render(wuVar, lb());
        } catch (Exception e10) {
            int i10 = this.ro instanceof com.bytedance.sdk.component.adexpress.dynamic.jnr.wsy ? 128 : 118;
            this.fm.callBackRenderFail(i10, "exception is " + e10.getMessage());
        }
    }

    @Override // com.bytedance.sdk.component.adexpress.ro.yz
    public void fm(wsy wsyVar) {
        this.yz = wsyVar;
        int iAjl = this.ajl.ajl();
        if (iAjl < 0) {
            this.fm.callBackRenderFail(this.ro instanceof com.bytedance.sdk.component.adexpress.dynamic.jnr.wsy ? 127 : 117, "time is ".concat(String.valueOf(iAjl)));
            return;
        }
        this.wsy = com.bytedance.sdk.component.adexpress.yz.yz.fm(new RunnableC0030fm(2), iAjl, TimeUnit.MILLISECONDS);
        if (Looper.getMainLooper() == Looper.myLooper() && this.ajl.ef() <= 0) {
            ajl();
        } else {
            ef.ro().postDelayed(new Runnable() { // from class: com.bytedance.sdk.component.adexpress.dynamic.fm.fm.1
                @Override // java.lang.Runnable
                public void run() {
                    fm.this.ajl();
                }
            }, this.ajl.ef());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar) {
        List<com.bytedance.sdk.component.adexpress.dynamic.yz.wu> listKu;
        if (wuVar == null || (listKu = wuVar.ku()) == null || listKu.size() <= 0) {
            return;
        }
        Collections.sort(listKu, new Comparator<com.bytedance.sdk.component.adexpress.dynamic.yz.wu>() { // from class: com.bytedance.sdk.component.adexpress.dynamic.fm.fm.3
            @Override // java.util.Comparator
            /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
            public int compare(com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar2, com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar3) {
                ajl ajlVarJnr = wuVar2.vt().jnr();
                ajl ajlVarJnr2 = wuVar3.vt().jnr();
                if (ajlVarJnr == null || ajlVarJnr2 == null) {
                    return 0;
                }
                return ajlVarJnr.qph() >= ajlVarJnr2.qph() ? 1 : -1;
            }
        });
        for (com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar2 : listKu) {
            if (wuVar2 != null) {
                fm(wuVar2);
            }
        }
    }

    @Override // com.bytedance.sdk.component.adexpress.ro.yz
    /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
    public DynamicRootView jnr() {
        return yz();
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void fm(View view) {
        if (view == 0) {
            return;
        }
        if (view instanceof ViewGroup) {
            int i10 = 0;
            while (true) {
                ViewGroup viewGroup = (ViewGroup) view;
                if (i10 >= viewGroup.getChildCount()) {
                    break;
                }
                fm(viewGroup.getChildAt(i10));
                i10++;
            }
        }
        if (view instanceof hxv) {
            ((hxv) view).ro();
        }
    }

    @Override // com.bytedance.sdk.component.adexpress.ro.ku
    public void fm(onz onzVar) {
        if (this.wu.get()) {
            return;
        }
        this.wu.set(true);
        if (onzVar.ro() && wsy()) {
            this.fm.setLayoutParams(new FrameLayout.LayoutParams(-1, -1));
            this.yz.fm(jnr(), onzVar);
            return;
        }
        this.yz.fm(onzVar.vt(), onzVar.ef());
    }

    public void ro() {
        fm(jnr());
    }

    @Override // com.bytedance.sdk.component.adexpress.ro.ku
    public void fm(View view, int i10, lb lbVar) {
        wu wuVar = this.jnr;
        if (wuVar != null) {
            wuVar.fm(view, i10, lbVar);
        }
    }
}
