package q9;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Typeface;
import android.os.Build;
import android.text.TextPaint;
import android.util.Xml;
import androidx.annotation.FontRes;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import d.j;
import i0.r;
import x8.m;

/* JADX INFO: loaded from: classes2.dex */
public final class g {

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final boolean f32260f;

    @Nullable
    public final String fontFamily;

    @Nullable
    public String fontVariationSettings;

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f32264j = false;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f32265k = false;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public Typeface f32266l;

    @Nullable
    public final ColorStateList shadowColor;

    @Nullable
    private ColorStateList textColor;

    @Nullable
    public final ColorStateList textColorHint;

    @Nullable
    public final ColorStateList textColorLink;

    public g(Context context, int i10) {
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(i10, j.TextAppearance);
        this.f32262h = typedArrayObtainStyledAttributes.getDimension(j.TextAppearance_android_textSize, 0.0f);
        setTextColor(d.getColorStateList(context, typedArrayObtainStyledAttributes, j.TextAppearance_android_textColor));
        this.textColorHint = d.getColorStateList(context, typedArrayObtainStyledAttributes, j.TextAppearance_android_textColorHint);
        this.textColorLink = d.getColorStateList(context, typedArrayObtainStyledAttributes, j.TextAppearance_android_textColorLink);
        this.f32255a = typedArrayObtainStyledAttributes.getInt(j.TextAppearance_android_textStyle, 0);
        this.f32256b = typedArrayObtainStyledAttributes.getInt(j.TextAppearance_android_typeface, 1);
        int i11 = j.TextAppearance_fontFamily;
        i11 = typedArrayObtainStyledAttributes.hasValue(i11) ? i11 : j.TextAppearance_android_fontFamily;
        this.f32263i = typedArrayObtainStyledAttributes.getResourceId(i11, 0);
        this.fontFamily = typedArrayObtainStyledAttributes.getString(i11);
        typedArrayObtainStyledAttributes.getBoolean(j.TextAppearance_textAllCaps, false);
        this.shadowColor = d.getColorStateList(context, typedArrayObtainStyledAttributes, j.TextAppearance_android_shadowColor);
        this.f32257c = typedArrayObtainStyledAttributes.getFloat(j.TextAppearance_android_shadowDx, 0.0f);
        this.f32258d = typedArrayObtainStyledAttributes.getFloat(j.TextAppearance_android_shadowDy, 0.0f);
        this.f32259e = typedArrayObtainStyledAttributes.getFloat(j.TextAppearance_android_shadowRadius, 0.0f);
        typedArrayObtainStyledAttributes.recycle();
        TypedArray typedArrayObtainStyledAttributes2 = context.obtainStyledAttributes(i10, m.MaterialTextAppearance);
        int i12 = m.MaterialTextAppearance_android_letterSpacing;
        this.f32260f = typedArrayObtainStyledAttributes2.hasValue(i12);
        this.f32261g = typedArrayObtainStyledAttributes2.getFloat(i12, 0.0f);
        if (Build.VERSION.SDK_INT >= 26) {
            int i13 = m.MaterialTextAppearance_fontVariationSettings;
            this.fontVariationSettings = typedArrayObtainStyledAttributes2.getString(typedArrayObtainStyledAttributes2.hasValue(i13) ? i13 : m.MaterialTextAppearance_android_fontVariationSettings);
        }
        typedArrayObtainStyledAttributes2.recycle();
    }

    @Nullable
    private Typeface getSystemTypeface(Context context) {
        Typeface typefaceCreate;
        if (this.f32265k) {
            return null;
        }
        this.f32265k = true;
        String fontProviderSystemFontFamily = readFontProviderSystemFontFamily(context, this.f32263i);
        if (fontProviderSystemFontFamily == null || (typefaceCreate = Typeface.create(fontProviderSystemFontFamily, 0)) == Typeface.DEFAULT) {
            return null;
        }
        return Typeface.create(typefaceCreate, this.f32255a);
    }

    @Nullable
    @SuppressLint({"ResourceType"})
    private static String readFontProviderSystemFontFamily(Context context, @FontRes int i10) {
        Resources resources = context.getResources();
        if (i10 != 0 && resources.getResourceTypeName(i10).equals("font")) {
            try {
                XmlResourceParser xml = resources.getXml(i10);
                while (xml.getEventType() != 1) {
                    if (xml.getEventType() == 2 && xml.getName().equals("font-family")) {
                        TypedArray typedArrayObtainAttributes = resources.obtainAttributes(Xml.asAttributeSet(xml), e0.h.FontFamily);
                        String string = typedArrayObtainAttributes.getString(e0.h.FontFamily_fontProviderSystemFontFamily);
                        typedArrayObtainAttributes.recycle();
                        return string;
                    }
                    xml.next();
                }
            } catch (Throwable unused) {
            }
        }
        return null;
    }

    public final void a() {
        String str;
        Typeface typeface = this.f32266l;
        int i10 = this.f32255a;
        if (typeface == null && (str = this.fontFamily) != null) {
            this.f32266l = Typeface.create(str, i10);
        }
        if (this.f32266l == null) {
            int i11 = this.f32256b;
            if (i11 == 1) {
                this.f32266l = Typeface.SANS_SERIF;
            } else if (i11 == 2) {
                this.f32266l = Typeface.SERIF;
            } else if (i11 != 3) {
                this.f32266l = Typeface.DEFAULT;
            } else {
                this.f32266l = Typeface.MONOSPACE;
            }
            this.f32266l = Typeface.create(this.f32266l, i10);
        }
    }

    public final void b(Context context, h hVar) {
        if (!c(context)) {
            a();
        }
        int i10 = this.f32263i;
        if (i10 == 0) {
            this.f32264j = true;
        }
        if (this.f32264j) {
            hVar.b(this.f32266l, true);
            return;
        }
        try {
            r.getFont(context, i10, new e(this, hVar), null);
        } catch (Resources.NotFoundException unused) {
            this.f32264j = true;
            hVar.a(1);
        } catch (Exception unused2) {
            this.f32264j = true;
            hVar.a(-3);
        }
    }

    public final boolean c(Context context) {
        if (this.f32264j) {
            return true;
        }
        int i10 = this.f32263i;
        if (i10 == 0) {
            return false;
        }
        Typeface cachedFont = r.getCachedFont(context, i10);
        if (cachedFont != null) {
            this.f32266l = cachedFont;
            this.f32264j = true;
            return true;
        }
        Typeface systemTypeface = getSystemTypeface(context);
        if (systemTypeface == null) {
            return false;
        }
        this.f32266l = systemTypeface;
        this.f32264j = true;
        return true;
    }

    public final void d(Context context, TextPaint textPaint, h hVar) {
        e(context, textPaint, hVar);
        ColorStateList colorStateList = this.textColor;
        textPaint.setColor(colorStateList != null ? colorStateList.getColorForState(textPaint.drawableState, colorStateList.getDefaultColor()) : -16777216);
        ColorStateList colorStateList2 = this.shadowColor;
        textPaint.setShadowLayer(this.f32259e, this.f32257c, this.f32258d, colorStateList2 != null ? colorStateList2.getColorForState(textPaint.drawableState, colorStateList2.getDefaultColor()) : 0);
    }

    public final void e(Context context, TextPaint textPaint, h hVar) {
        Typeface typeface;
        if (c(context) && this.f32264j && (typeface = this.f32266l) != null) {
            f(context, textPaint, typeface);
            return;
        }
        a();
        f(context, textPaint, this.f32266l);
        b(context, new f(this, context, textPaint, hVar));
    }

    public final void f(Context context, TextPaint textPaint, Typeface typeface) {
        Typeface typefaceMaybeCopyWithFontWeightAdjustment = i.maybeCopyWithFontWeightAdjustment(context, typeface);
        if (typefaceMaybeCopyWithFontWeightAdjustment != null) {
            typeface = typefaceMaybeCopyWithFontWeightAdjustment;
        }
        textPaint.setTypeface(typeface);
        int i10 = (~typeface.getStyle()) & this.f32255a;
        textPaint.setFakeBoldText((i10 & 1) != 0);
        textPaint.setTextSkewX((i10 & 2) != 0 ? -0.25f : 0.0f);
        textPaint.setTextSize(this.f32262h);
        if (Build.VERSION.SDK_INT >= 26) {
            textPaint.setFontVariationSettings(this.fontVariationSettings);
        }
        if (this.f32260f) {
            textPaint.setLetterSpacing(this.f32261g);
        }
    }

    @Nullable
    @RequiresApi(26)
    public String getFontVariationSettings() {
        return this.fontVariationSettings;
    }

    @Nullable
    public ColorStateList getTextColor() {
        return this.textColor;
    }

    @RequiresApi(26)
    public void setFontVariationSettings(@Nullable String str) {
        this.fontVariationSettings = str;
    }

    public void setTextColor(@Nullable ColorStateList colorStateList) {
        this.textColor = colorStateList;
    }
}
