package z8;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.annotation.XmlRes;
import com.google.android.material.internal.m0;
import com.google.android.material.internal.n0;
import com.google.android.material.internal.q0;
import com.google.android.material.shape.k;
import com.google.android.material.shape.q;
import com.google.android.material.shape.s;
import java.lang.ref.WeakReference;
import java.text.NumberFormat;
import q9.g;
import x8.l;

/* JADX INFO: loaded from: classes2.dex */
public final class a extends Drawable implements m0 {

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final int f37070l = l.Widget_MaterialComponents_Badge;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final int f37071m = x8.c.badgeStyle;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final WeakReference f37072a;

    @Nullable
    private WeakReference<View> anchorViewRef;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final k f37073b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final n0 f37074c;

    @Nullable
    private WeakReference<FrameLayout> customBadgeParentRef;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Rect f37075d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final c f37076e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public float f37077f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public float f37078g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f37079h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public float f37080i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public float f37081j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public float f37082k;

    private a(@NonNull Context context, @XmlRes int i10, @AttrRes int i11, @StyleRes int i12, @Nullable b bVar) {
        g gVar;
        WeakReference weakReference = new WeakReference(context);
        this.f37072a = weakReference;
        q0.b(context, q0.f22800b, "Theme.MaterialComponents");
        this.f37075d = new Rect();
        n0 n0Var = new n0(this);
        this.f37074c = n0Var;
        TextPaint textPaint = n0Var.f22792a;
        textPaint.setTextAlign(Paint.Align.CENTER);
        c cVar = new c(context, i10, i11, i12, bVar);
        this.f37076e = cVar;
        boolean zE = e();
        b bVar2 = cVar.f37102b;
        k kVar = new k(s.a(context, zE ? bVar2.f37094g.intValue() : bVar2.f37091e.intValue(), e() ? bVar2.f37095h.intValue() : bVar2.f37093f.intValue()).a());
        this.f37073b = kVar;
        g();
        Context context2 = (Context) weakReference.get();
        if (context2 != null && n0Var.getTextAppearance() != (gVar = new g(context2, bVar2.f37089d.intValue()))) {
            n0Var.setTextAppearance(gVar, context2);
            textPaint.setColor(bVar2.f37087c.intValue());
            invalidateSelf();
            h();
            invalidateSelf();
        }
        int i13 = bVar2.f37098k;
        if (i13 != -2) {
            this.f37079h = ((int) Math.pow(10.0d, ((double) i13) - 1.0d)) - 1;
        } else {
            this.f37079h = bVar2.f37099l;
        }
        n0Var.f22796e = true;
        h();
        invalidateSelf();
        n0Var.f22796e = true;
        g();
        h();
        invalidateSelf();
        textPaint.setAlpha(getAlpha());
        invalidateSelf();
        ColorStateList colorStateListValueOf = ColorStateList.valueOf(bVar2.f37085b.intValue());
        if (kVar.getFillColor() != colorStateListValueOf) {
            kVar.setFillColor(colorStateListValueOf);
            invalidateSelf();
        }
        textPaint.setColor(bVar2.f37087c.intValue());
        invalidateSelf();
        WeakReference<View> weakReference2 = this.anchorViewRef;
        if (weakReference2 != null && weakReference2.get() != null) {
            View view = this.anchorViewRef.get();
            WeakReference<FrameLayout> weakReference3 = this.customBadgeParentRef;
            updateBadgeCoordinates(view, weakReference3 != null ? weakReference3.get() : null);
        }
        h();
        setVisible(bVar2.T.booleanValue(), false);
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void autoAdjustWithinViewBounds(@NonNull View view, @Nullable View view2) {
        float y10;
        float x10;
        ViewParent parent;
        boolean z10;
        FrameLayout customBadgeParent = getCustomBadgeParent();
        if (customBadgeParent == null) {
            float y11 = view.getY();
            x10 = view.getX();
            parent = view.getParent();
            y10 = y11;
        } else {
            y10 = 0.0f;
            x10 = 0.0f;
            parent = customBadgeParent;
        }
        while (true) {
            z10 = parent instanceof View;
            if (!z10 || parent == view2) {
                break;
            }
            ViewParent parent2 = parent.getParent();
            if (!(parent2 instanceof ViewGroup) || ((ViewGroup) parent2).getClipChildren()) {
                break;
            }
            View view3 = (View) parent;
            y10 += view3.getY();
            x10 += view3.getX();
            parent = parent.getParent();
        }
        if (z10) {
            float f10 = (this.f37078g - this.f37082k) + y10;
            float f11 = (this.f37077f - this.f37081j) + x10;
            View view4 = (View) parent;
            float height = ((this.f37078g + this.f37082k) - view4.getHeight()) + y10;
            float width = ((this.f37077f + this.f37081j) - view4.getWidth()) + x10;
            if (f10 < 0.0f) {
                this.f37078g = Math.abs(f10) + this.f37078g;
            }
            if (f11 < 0.0f) {
                this.f37077f = Math.abs(f11) + this.f37077f;
            }
            if (height > 0.0f) {
                this.f37078g -= Math.abs(height);
            }
            if (width > 0.0f) {
                this.f37077f -= Math.abs(width);
            }
        }
    }

    public static a b(Context context) {
        return new a(context, 0, f37071m, f37070l, null);
    }

    public static a c(Context context, b bVar) {
        return new a(context, 0, f37071m, f37070l, bVar);
    }

    @Nullable
    private String getBadgeContent() {
        c cVar = this.f37076e;
        if (cVar.a()) {
            return getTextBadgeText();
        }
        if (!f()) {
            return null;
        }
        if (this.f37079h == -2 || d() <= this.f37079h) {
            return NumberFormat.getInstance(cVar.f37102b.f37100m).format(d());
        }
        Context context = (Context) this.f37072a.get();
        return context == null ? "" : String.format(cVar.f37102b.f37100m, context.getString(x8.k.mtrl_exceed_max_badge_number_suffix), Integer.valueOf(this.f37079h), "+");
    }

    @Nullable
    private String getNumberContentDescription() {
        Context context;
        c cVar = this.f37076e;
        if (cVar.f37102b.Q == 0 || (context = (Context) this.f37072a.get()) == null) {
            return null;
        }
        if (this.f37079h != -2) {
            int iD = d();
            int i10 = this.f37079h;
            if (iD > i10) {
                return context.getString(cVar.f37102b.R, Integer.valueOf(i10));
            }
        }
        return context.getResources().getQuantityString(cVar.f37102b.Q, d(), Integer.valueOf(d()));
    }

    @Nullable
    private String getTextBadgeText() {
        String text = getText();
        int i10 = this.f37076e.f37102b.f37098k;
        if (i10 == -2 || text == null || text.length() <= i10) {
            return text;
        }
        Context context = (Context) this.f37072a.get();
        if (context == null) {
            return "";
        }
        return String.format(context.getString(x8.k.m3_exceed_max_badge_text_suffix), text.substring(0, i10 - 1), "…");
    }

    @Nullable
    private CharSequence getTextContentDescription() {
        CharSequence charSequence = this.f37076e.f37102b.contentDescriptionForText;
        return charSequence != null ? charSequence : getText();
    }

    @Override // com.google.android.material.internal.m0
    public final void a() {
        invalidateSelf();
    }

    public final int d() {
        int i10 = this.f37076e.f37102b.f37097j;
        if (i10 != -1) {
            return i10;
        }
        return 0;
    }

    @Override // android.graphics.drawable.Drawable
    public final void draw(Canvas canvas) {
        String badgeContent;
        if (getBounds().isEmpty() || getAlpha() == 0 || !isVisible()) {
            return;
        }
        this.f37073b.draw(canvas);
        if (!e() || (badgeContent = getBadgeContent()) == null) {
            return;
        }
        Rect rect = new Rect();
        n0 n0Var = this.f37074c;
        n0Var.f22792a.getTextBounds(badgeContent, 0, badgeContent.length(), rect);
        float fExactCenterY = this.f37078g - rect.exactCenterY();
        canvas.drawText(badgeContent, this.f37077f, rect.bottom <= 0 ? (int) fExactCenterY : Math.round(fExactCenterY), n0Var.f22792a);
    }

    public final boolean e() {
        return this.f37076e.a() || f();
    }

    public final boolean f() {
        c cVar = this.f37076e;
        return (cVar.a() || cVar.f37102b.f37097j == -1) ? false : true;
    }

    public final void g() {
        Context context = (Context) this.f37072a.get();
        if (context == null) {
            return;
        }
        boolean zE = e();
        c cVar = this.f37076e;
        this.f37073b.setShapeAppearanceModel(s.a(context, zE ? cVar.f37102b.f37094g.intValue() : cVar.f37102b.f37091e.intValue(), e() ? cVar.f37102b.f37095h.intValue() : cVar.f37102b.f37093f.intValue()).a());
        invalidateSelf();
    }

    @Override // android.graphics.drawable.Drawable
    public final int getAlpha() {
        return this.f37076e.f37102b.f37096i;
    }

    @Nullable
    public CharSequence getContentDescription() {
        if (!isVisible()) {
            return null;
        }
        c cVar = this.f37076e;
        return cVar.a() ? getTextContentDescription() : f() ? getNumberContentDescription() : cVar.f37102b.contentDescriptionNumberless;
    }

    @Nullable
    public FrameLayout getCustomBadgeParent() {
        WeakReference<FrameLayout> weakReference = this.customBadgeParentRef;
        if (weakReference != null) {
            return weakReference.get();
        }
        return null;
    }

    @Override // android.graphics.drawable.Drawable
    public final int getIntrinsicHeight() {
        return this.f37075d.height();
    }

    @Override // android.graphics.drawable.Drawable
    public final int getIntrinsicWidth() {
        return this.f37075d.width();
    }

    @Override // android.graphics.drawable.Drawable
    public final int getOpacity() {
        return -3;
    }

    @Nullable
    public String getText() {
        return this.f37076e.f37102b.text;
    }

    public final void h() {
        WeakReference weakReference = this.f37072a;
        Context context = (Context) weakReference.get();
        WeakReference<View> weakReference2 = this.anchorViewRef;
        View view = weakReference2 != null ? weakReference2.get() : null;
        if (context == null || view == null) {
            return;
        }
        Rect rect = new Rect();
        Rect rect2 = this.f37075d;
        rect.set(rect2);
        Rect rect3 = new Rect();
        view.getDrawingRect(rect3);
        WeakReference<FrameLayout> weakReference3 = this.customBadgeParentRef;
        FrameLayout frameLayout = weakReference3 != null ? weakReference3.get() : null;
        if (frameLayout != null) {
            frameLayout.offsetDescendantRectToMyCoords(view, rect3);
        }
        boolean zE = e();
        c cVar = this.f37076e;
        float f10 = zE ? cVar.f37104d : cVar.f37103c;
        this.f37080i = f10;
        if (f10 != -1.0f) {
            this.f37081j = f10;
            this.f37082k = f10;
        } else {
            this.f37081j = Math.round((e() ? cVar.f37107g : cVar.f37105e) / 2.0f);
            this.f37082k = Math.round((e() ? cVar.f37108h : cVar.f37106f) / 2.0f);
        }
        if (e()) {
            String badgeContent = getBadgeContent();
            float f11 = this.f37081j;
            n0 n0Var = this.f37074c;
            this.f37081j = Math.max(f11, (n0Var.b(badgeContent) / 2.0f) + cVar.f37102b.U.intValue());
            float fMax = Math.max(this.f37082k, (n0Var.getTextHeight(badgeContent) / 2.0f) + cVar.f37102b.V.intValue());
            this.f37082k = fMax;
            this.f37081j = Math.max(this.f37081j, fMax);
        }
        int iIntValue = cVar.f37102b.X.intValue();
        boolean zE2 = e();
        b bVar = cVar.f37102b;
        if (zE2) {
            iIntValue = bVar.Z.intValue();
            Context context2 = (Context) weakReference.get();
            if (context2 != null) {
                iIntValue = y8.a.c(y8.a.b(0.0f, 1.0f, 0.3f, 1.0f, context2.getResources().getConfiguration().fontScale - 1.0f), iIntValue, iIntValue - bVar.f37088c0.intValue());
            }
        }
        int i10 = cVar.f37111k;
        if (i10 == 0) {
            iIntValue -= Math.round(this.f37082k);
        }
        int iIntValue2 = bVar.f37086b0.intValue() + iIntValue;
        int iIntValue3 = bVar.S.intValue();
        if (iIntValue3 == 8388691 || iIntValue3 == 8388693) {
            this.f37078g = rect3.bottom - iIntValue2;
        } else {
            this.f37078g = rect3.top + iIntValue2;
        }
        int iIntValue4 = e() ? bVar.Y.intValue() : bVar.W.intValue();
        if (i10 == 1) {
            iIntValue4 += e() ? cVar.f37110j : cVar.f37109i;
        }
        int iIntValue5 = bVar.f37084a0.intValue() + iIntValue4;
        int iIntValue6 = bVar.S.intValue();
        if (iIntValue6 == 8388659 || iIntValue6 == 8388691) {
            this.f37077f = cVar.f37112l == 0 ? view.getLayoutDirection() == 0 ? (rect3.left + this.f37081j) - ((this.f37082k * 2.0f) - iIntValue5) : (rect3.right - this.f37081j) + ((this.f37082k * 2.0f) - iIntValue5) : view.getLayoutDirection() == 0 ? (rect3.left - this.f37081j) + iIntValue5 : (rect3.right + this.f37081j) - iIntValue5;
        } else {
            this.f37077f = cVar.f37112l == 0 ? view.getLayoutDirection() == 0 ? (rect3.right + this.f37081j) - iIntValue5 : (rect3.left - this.f37081j) + iIntValue5 : view.getLayoutDirection() == 0 ? (rect3.right - this.f37081j) + ((this.f37082k * 2.0f) - iIntValue5) : (rect3.left + this.f37081j) - ((this.f37082k * 2.0f) - iIntValue5);
        }
        if (bVar.f37090d0.booleanValue()) {
            ViewParent customBadgeParent = getCustomBadgeParent();
            if (customBadgeParent == null) {
                customBadgeParent = view.getParent();
            }
            if ((customBadgeParent instanceof View) && (customBadgeParent.getParent() instanceof View)) {
                autoAdjustWithinViewBounds(view, (View) customBadgeParent.getParent());
            }
        } else {
            autoAdjustWithinViewBounds(view, null);
        }
        float f12 = this.f37077f;
        float f13 = this.f37078g;
        float f14 = this.f37081j;
        float f15 = this.f37082k;
        rect2.set((int) (f12 - f14), (int) (f13 - f15), (int) (f12 + f14), (int) (f13 + f15));
        float f16 = this.f37080i;
        k kVar = this.f37073b;
        if (f16 != -1.0f) {
            q qVarH = kVar.f23095b.f23068a.h();
            qVarH.c(f16);
            kVar.setShapeAppearanceModel(qVarH.a());
        }
        if (rect.equals(rect2)) {
            return;
        }
        kVar.setBounds(rect2);
    }

    @Override // android.graphics.drawable.Drawable
    public final boolean isStateful() {
        return false;
    }

    @Override // android.graphics.drawable.Drawable, com.google.android.material.internal.m0
    public final boolean onStateChange(int[] iArr) {
        return super.onStateChange(iArr);
    }

    @Override // android.graphics.drawable.Drawable
    public final void setAlpha(int i10) {
        c cVar = this.f37076e;
        cVar.f37101a.f37096i = i10;
        cVar.f37102b.f37096i = i10;
        this.f37074c.f22792a.setAlpha(getAlpha());
        invalidateSelf();
    }

    @Override // android.graphics.drawable.Drawable
    public final void setColorFilter(ColorFilter colorFilter) {
    }

    public void setContentDescriptionForText(@Nullable CharSequence charSequence) {
        c cVar = this.f37076e;
        cVar.f37101a.contentDescriptionForText = charSequence;
        cVar.f37102b.contentDescriptionForText = charSequence;
    }

    public void setText(@Nullable String str) {
        c cVar = this.f37076e;
        if (TextUtils.equals(cVar.f37102b.text, str)) {
            return;
        }
        cVar.f37101a.text = str;
        cVar.f37102b.text = str;
        this.f37074c.f22796e = true;
        g();
        h();
        invalidateSelf();
    }

    @Deprecated
    public void updateBadgeCoordinates(@NonNull View view, @Nullable ViewGroup viewGroup) {
        if (!(viewGroup instanceof FrameLayout)) {
            throw new IllegalArgumentException("customBadgeParent must be a FrameLayout");
        }
        updateBadgeCoordinates(view, (FrameLayout) viewGroup);
    }

    public void updateBadgeCoordinates(@NonNull View view, @Nullable FrameLayout frameLayout) {
        this.anchorViewRef = new WeakReference<>(view);
        this.customBadgeParentRef = new WeakReference<>(frameLayout);
        ViewGroup viewGroup = (ViewGroup) view.getParent();
        viewGroup.setClipChildren(false);
        viewGroup.setClipToPadding(false);
        h();
        invalidateSelf();
    }
}
