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

import a3.r;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public class ku extends ajl {
    private String fm;

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

        public fm(Context context) {
            this.fm = new WeakReference<>(context);
        }

        @Override // com.bytedance.sdk.component.jnr.wu
        public Bitmap fm(Bitmap bitmap) {
            Context context = this.fm.get();
            if (context != null) {
                return com.bytedance.sdk.component.adexpress.yz.fm.fm(context, bitmap, 25);
            }
            return null;
        }
    }

    public static class ro implements com.bytedance.sdk.component.jnr.irt {
        private WeakReference<View> fm;
        private Resources ro;

        public ro(View view, Resources resources) {
            this.fm = new WeakReference<>(view);
            this.ro = resources;
        }

        @Override // com.bytedance.sdk.component.jnr.irt
        public void fm(int i10, String str, @Nullable Throwable th) {
        }

        @Override // com.bytedance.sdk.component.jnr.irt
        public void fm(com.bytedance.sdk.component.jnr.ku kuVar) {
            Object objRo;
            View view = this.fm.get();
            if (view == null || (objRo = kuVar.ro()) == null || kuVar.lb() == null) {
                return;
            }
            if (objRo instanceof Bitmap) {
                view.setBackground(new BitmapDrawable(this.ro, (Bitmap) objRo));
            } else if (objRo instanceof Drawable) {
                if (Build.VERSION.SDK_INT >= 28 && r.D(objRo)) {
                    a.h(objRo).start();
                }
                view.setBackground((Drawable) objRo);
            }
        }
    }

    public ku(Context context, @NonNull DynamicRootView dynamicRootView, @NonNull com.bytedance.sdk.component.adexpress.dynamic.yz.wu wuVar) {
        super(context, dynamicRootView, wuVar);
        if (!TextUtils.isEmpty(this.duv.jm()) && wuVar.gqi()) {
            com.bytedance.sdk.component.adexpress.ajl.vt vtVar = new com.bytedance.sdk.component.adexpress.ajl.vt(context);
            vtVar.setAnimationsLoop(this.duv.no());
            vtVar.setImageLottieTosPath(this.duv.jm());
            vtVar.setLottieAppNameMaxLength(this.duv.qb());
            vtVar.setLottieAdTitleMaxLength(this.duv.zow());
            vtVar.setLottieAdDescMaxLength(this.duv.lcp());
            vtVar.setData(wuVar.wey());
            this.fhx = vtVar;
        } else if (this.duv.fhx() > 0.0f) {
            com.bytedance.sdk.component.adexpress.ajl.hlt hltVar = new com.bytedance.sdk.component.adexpress.ajl.hlt(context);
            this.fhx = hltVar;
            hltVar.setXRound((int) com.bytedance.sdk.component.adexpress.yz.wsy.fm(context, this.duv.fhx()));
            ((com.bytedance.sdk.component.adexpress.ajl.hlt) this.fhx).setYRound((int) com.bytedance.sdk.component.adexpress.yz.wsy.fm(context, this.duv.fhx()));
        } else if (wu() || !"arrowButton".equals(wuVar.vt().ro())) {
            this.fhx = new ImageView(context);
        } else {
            com.bytedance.sdk.component.adexpress.dynamic.animation.view.ro roVar = new com.bytedance.sdk.component.adexpress.dynamic.animation.view.ro(context);
            roVar.setBrickNativeValue(this.duv);
            this.fhx = roVar;
        }
        this.fm = getImageKey();
        this.fhx.setTag(Integer.valueOf(getClickArea()));
        if ("arrowButton".equals(wuVar.vt().ro())) {
            if (this.duv.ro() > 0 || this.duv.fm() > 0) {
                int iMin = Math.min(this.wsy, this.wu);
                this.wsy = iMin;
                this.wu = Math.min(iMin, this.wu);
                this.f6471ef = (int) (com.bytedance.sdk.component.adexpress.yz.wsy.fm(context, (this.duv.fm() / 2) + this.duv.ro() + 0.5f) + this.f6471ef);
            } else {
                int iMax = Math.max(this.wsy, this.wu);
                this.wsy = iMax;
                this.wu = Math.max(iMax, this.wu);
            }
            this.duv.fm(this.wsy / 2);
        }
        addView(this.fhx, new FrameLayout.LayoutParams(this.wsy, this.wu));
    }

    private boolean fm() {
        String strDuv = this.duv.duv();
        if (this.duv.wey()) {
            return true;
        }
        if (TextUtils.isEmpty(strDuv)) {
            return false;
        }
        try {
            JSONObject jSONObject = new JSONObject(strDuv);
            return Math.abs((((float) this.wsy) / (((float) this.wu) * 1.0f)) - (((float) jSONObject.optInt("width")) / (((float) jSONObject.optInt("height")) * 1.0f))) > 0.01f;
        } catch (JSONException unused) {
            return false;
        }
    }

    private String getImageKey() {
        Map<String, String> mapKu = this.onz.getRenderRequest().ku();
        if (mapKu == null || mapKu.size() <= 0) {
            return null;
        }
        return mapKu.get(this.duv.ku());
    }

    @Override // com.bytedance.sdk.component.adexpress.dynamic.dynamicview.ajl, com.bytedance.sdk.component.adexpress.dynamic.dynamicview.pkk
    public boolean ef() {
        int iQhl;
        super.ef();
        if (!TextUtils.isEmpty(this.duv.jm())) {
            ((ImageView) this.fhx).setScaleType(ImageView.ScaleType.CENTER_CROP);
            return true;
        }
        int iIrt = 0;
        if ("arrowButton".equals(this.dsz.vt().ro())) {
            ((ImageView) this.fhx).setImageResource(com.bytedance.sdk.component.utils.maa.yz(this.ku, "tt_white_righterbackicon_titlebar"));
            if (((ImageView) this.fhx).getDrawable() != null) {
                ((ImageView) this.fhx).getDrawable().setAutoMirrored(true);
            }
            this.fhx.setPadding(0, 0, 0, 0);
            ((ImageView) this.fhx).setScaleType(ImageView.ScaleType.FIT_XY);
            return true;
        }
        this.fhx.setBackgroundColor(this.duv.gzf());
        String strLb = this.dsz.vt().lb();
        if ("user".equals(strLb)) {
            ((ImageView) this.fhx).setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            ((ImageView) this.fhx).setColorFilter(this.duv.wsy());
            ((ImageView) this.fhx).setImageDrawable(com.bytedance.sdk.component.utils.maa.lb(getContext(), "tt_user"));
            ImageView imageView = (ImageView) this.fhx;
            int i10 = this.wsy;
            imageView.setPadding(i10 / 10, this.wu / 5, i10 / 10, 0);
        } else if (strLb != null && strLb.startsWith("@")) {
            try {
                ((ImageView) this.fhx).setImageResource(Integer.parseInt(strLb.substring(1)));
            } catch (Exception unused) {
            }
        }
        com.bytedance.sdk.component.jnr.fhx fhxVarJnr = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().jnr();
        String strKu = this.duv.ku();
        if (!TextUtils.isEmpty(strKu) && !strKu.startsWith("http:") && !strKu.startsWith("https:")) {
            DynamicRootView dynamicRootView = this.onz;
            strKu = com.bytedance.sdk.component.adexpress.dynamic.jnr.ef.ro(strKu, (dynamicRootView == null || dynamicRootView.getRenderRequest() == null) ? null : this.onz.getRenderRequest().hlt());
        }
        com.bytedance.sdk.component.adexpress.fm.fm.lb lbVarLb = com.bytedance.sdk.component.adexpress.fm.fm.fm.fm().lb();
        if (lbVarLb != null) {
            iIrt = lbVarLb.irt();
            iQhl = lbVarLb.qhl();
        } else {
            iQhl = 0;
        }
        com.bytedance.sdk.component.jnr.vt vtVarJnr = fhxVarJnr.fm(strKu).fm(this.fm).fm(this.wsy).ro(this.wu).yz(iIrt).jnr(iQhl);
        String strOnz = this.onz.getRenderRequest().onz();
        if (!TextUtils.isEmpty(strOnz)) {
            vtVarJnr.ro(strOnz);
        }
        if (fm()) {
            ((ImageView) this.fhx).setScaleType(ImageView.ScaleType.FIT_CENTER);
            vtVarJnr.fm(Bitmap.Config.ARGB_4444).lb(2).fm(new fm(this.ku)).fm(new ro(this.fhx, getResources()));
        } else {
            if (com.bytedance.sdk.component.adexpress.yz.ro()) {
                vtVarJnr.lb(1).fm((ImageView) this.fhx);
            }
            ((ImageView) this.fhx).setScaleType(ImageView.ScaleType.FIT_XY);
        }
        if ((this.fhx instanceof ImageView) && "cover".equals(getImageObjectFit())) {
            ((ImageView) this.fhx).setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
        return true;
    }

    @Override // com.bytedance.sdk.component.adexpress.dynamic.dynamicview.ajl, com.bytedance.sdk.component.adexpress.dynamic.dynamicview.jnr, android.view.ViewGroup, android.view.View
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Drawable drawable = ((ImageView) this.fhx).getDrawable();
        if (Build.VERSION.SDK_INT < 28 || !j4.a.x(drawable)) {
            return;
        }
        j4.a.f(drawable).start();
    }

    @Override // com.bytedance.sdk.component.adexpress.dynamic.dynamicview.ajl, com.bytedance.sdk.component.adexpress.dynamic.dynamicview.jnr, android.view.ViewGroup, android.view.View
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Drawable drawable = ((ImageView) this.fhx).getDrawable();
        if (Build.VERSION.SDK_INT < 28 || !j4.a.x(drawable)) {
            return;
        }
        j4.a.f(drawable).stop();
    }
}
