package androidx.constraintlayout.utils.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.constraintlayout.widget.s;
import b0.b;
import b0.c;
import f.a;

/* JADX INFO: loaded from: classes.dex */
public class ImageFilterView extends AppCompatImageView {
    public final Drawable[] Q;
    public LayerDrawable R;
    public float S;
    public float T;
    public float U;
    public float V;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f1190e;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public RectF f1198m;

    public ImageFilterView(Context context) {
        super(context);
        this.f1189d = new c();
        this.f1190e = true;
        this.f1191f = null;
        this.f1192g = null;
        this.f1193h = 0.0f;
        this.f1194i = 0.0f;
        this.f1195j = Float.NaN;
        this.Q = new Drawable[2];
        this.S = Float.NaN;
        this.T = Float.NaN;
        this.U = Float.NaN;
        this.V = Float.NaN;
    }

    private void setOverlay(boolean z10) {
        this.f1190e = z10;
    }

    public final void c(Context context, AttributeSet attributeSet) {
        if (attributeSet != null) {
            TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, s.ImageFilterView);
            int indexCount = typedArrayObtainStyledAttributes.getIndexCount();
            this.f1191f = typedArrayObtainStyledAttributes.getDrawable(s.ImageFilterView_altSrc);
            for (int i10 = 0; i10 < indexCount; i10++) {
                int index = typedArrayObtainStyledAttributes.getIndex(i10);
                if (index == s.ImageFilterView_crossfade) {
                    this.f1193h = typedArrayObtainStyledAttributes.getFloat(index, 0.0f);
                } else if (index == s.ImageFilterView_warmth) {
                    setWarmth(typedArrayObtainStyledAttributes.getFloat(index, 0.0f));
                } else if (index == s.ImageFilterView_saturation) {
                    setSaturation(typedArrayObtainStyledAttributes.getFloat(index, 0.0f));
                } else if (index == s.ImageFilterView_contrast) {
                    setContrast(typedArrayObtainStyledAttributes.getFloat(index, 0.0f));
                } else if (index == s.ImageFilterView_brightness) {
                    setBrightness(typedArrayObtainStyledAttributes.getFloat(index, 0.0f));
                } else if (index == s.ImageFilterView_round) {
                    setRound(typedArrayObtainStyledAttributes.getDimension(index, 0.0f));
                } else if (index == s.ImageFilterView_roundPercent) {
                    setRoundPercent(typedArrayObtainStyledAttributes.getFloat(index, 0.0f));
                } else if (index == s.ImageFilterView_overlay) {
                    setOverlay(typedArrayObtainStyledAttributes.getBoolean(index, this.f1190e));
                } else if (index == s.ImageFilterView_imagePanX) {
                    setImagePanX(typedArrayObtainStyledAttributes.getFloat(index, this.S));
                } else if (index == s.ImageFilterView_imagePanY) {
                    setImagePanY(typedArrayObtainStyledAttributes.getFloat(index, this.T));
                } else if (index == s.ImageFilterView_imageRotate) {
                    setImageRotate(typedArrayObtainStyledAttributes.getFloat(index, this.V));
                } else if (index == s.ImageFilterView_imageZoom) {
                    setImageZoom(typedArrayObtainStyledAttributes.getFloat(index, this.U));
                }
            }
            typedArrayObtainStyledAttributes.recycle();
            Drawable drawable = getDrawable();
            this.f1192g = drawable;
            Drawable drawable2 = this.f1191f;
            Drawable[] drawableArr = this.Q;
            if (drawable2 == null || drawable == null) {
                Drawable drawable3 = getDrawable();
                this.f1192g = drawable3;
                if (drawable3 != null) {
                    Drawable drawableMutate = drawable3.mutate();
                    this.f1192g = drawableMutate;
                    drawableArr[0] = drawableMutate;
                    return;
                }
                return;
            }
            Drawable drawableMutate2 = getDrawable().mutate();
            this.f1192g = drawableMutate2;
            drawableArr[0] = drawableMutate2;
            drawableArr[1] = this.f1191f.mutate();
            LayerDrawable layerDrawable = new LayerDrawable(drawableArr);
            this.R = layerDrawable;
            layerDrawable.getDrawable(1).setAlpha((int) (this.f1193h * 255.0f));
            if (!this.f1190e) {
                this.R.getDrawable(0).setAlpha((int) ((1.0f - this.f1193h) * 255.0f));
            }
            super.setImageDrawable(this.R);
        }
    }

    public final void d() {
        if (Float.isNaN(this.S) && Float.isNaN(this.T) && Float.isNaN(this.U) && Float.isNaN(this.V)) {
            return;
        }
        float f10 = Float.isNaN(this.S) ? 0.0f : this.S;
        float f11 = Float.isNaN(this.T) ? 0.0f : this.T;
        float f12 = Float.isNaN(this.U) ? 1.0f : this.U;
        float f13 = Float.isNaN(this.V) ? 0.0f : this.V;
        Matrix matrix = new Matrix();
        matrix.reset();
        float intrinsicWidth = getDrawable().getIntrinsicWidth();
        float intrinsicHeight = getDrawable().getIntrinsicHeight();
        float width = getWidth();
        float height = getHeight();
        float f14 = f12 * (intrinsicWidth * height < intrinsicHeight * width ? width / intrinsicWidth : height / intrinsicHeight);
        matrix.postScale(f14, f14);
        float f15 = intrinsicWidth * f14;
        float f16 = f14 * intrinsicHeight;
        matrix.postTranslate(((((width - f15) * f10) + width) - f15) * 0.5f, ((((height - f16) * f11) + height) - f16) * 0.5f);
        matrix.postRotate(f13, width / 2.0f, height / 2.0f);
        setImageMatrix(matrix);
        setScaleType(ImageView.ScaleType.MATRIX);
    }

    @Override // android.view.View
    public final void draw(Canvas canvas) {
        super.draw(canvas);
    }

    public final void e() {
        if (Float.isNaN(this.S) && Float.isNaN(this.T) && Float.isNaN(this.U) && Float.isNaN(this.V)) {
            setScaleType(ImageView.ScaleType.FIT_CENTER);
        } else {
            d();
        }
    }

    public float getBrightness() {
        return this.f1189d.f4704d;
    }

    public float getContrast() {
        return this.f1189d.f4706f;
    }

    public float getCrossfade() {
        return this.f1193h;
    }

    public float getImagePanX() {
        return this.S;
    }

    public float getImagePanY() {
        return this.T;
    }

    public float getImageRotate() {
        return this.V;
    }

    public float getImageZoom() {
        return this.U;
    }

    public float getRound() {
        return this.f1195j;
    }

    public float getRoundPercent() {
        return this.f1194i;
    }

    public float getSaturation() {
        return this.f1189d.f4705e;
    }

    public float getWarmth() {
        return this.f1189d.f4707g;
    }

    @Override // android.view.View
    public final void layout(int i10, int i11, int i12, int i13) {
        super.layout(i10, i11, i12, i13);
        d();
    }

    public void setAltImageDrawable(Drawable drawable) {
        Drawable drawableMutate = drawable.mutate();
        this.f1191f = drawableMutate;
        Drawable drawable2 = this.f1192g;
        Drawable[] drawableArr = this.Q;
        drawableArr[0] = drawable2;
        drawableArr[1] = drawableMutate;
        LayerDrawable layerDrawable = new LayerDrawable(drawableArr);
        this.R = layerDrawable;
        super.setImageDrawable(layerDrawable);
        setCrossfade(this.f1193h);
    }

    public void setAltImageResource(int i10) {
        Drawable drawable = a.getDrawable(getContext(), i10);
        this.f1191f = drawable;
        setAltImageDrawable(drawable);
    }

    public void setBrightness(float f10) {
        c cVar = this.f1189d;
        cVar.f4704d = f10;
        cVar.a(this);
    }

    public void setContrast(float f10) {
        c cVar = this.f1189d;
        cVar.f4706f = f10;
        cVar.a(this);
    }

    public void setCrossfade(float f10) {
        this.f1193h = f10;
        if (this.Q != null) {
            if (!this.f1190e) {
                this.R.getDrawable(0).setAlpha((int) ((1.0f - this.f1193h) * 255.0f));
            }
            this.R.getDrawable(1).setAlpha((int) (this.f1193h * 255.0f));
            super.setImageDrawable(this.R);
        }
    }

    @Override // androidx.appcompat.widget.AppCompatImageView, android.widget.ImageView
    public void setImageDrawable(Drawable drawable) {
        if (this.f1191f == null || drawable == null) {
            super.setImageDrawable(drawable);
            return;
        }
        Drawable drawableMutate = drawable.mutate();
        this.f1192g = drawableMutate;
        Drawable[] drawableArr = this.Q;
        drawableArr[0] = drawableMutate;
        drawableArr[1] = this.f1191f;
        LayerDrawable layerDrawable = new LayerDrawable(drawableArr);
        this.R = layerDrawable;
        super.setImageDrawable(layerDrawable);
        setCrossfade(this.f1193h);
    }

    public void setImagePanX(float f10) {
        this.S = f10;
        e();
    }

    public void setImagePanY(float f10) {
        this.T = f10;
        e();
    }

    @Override // androidx.appcompat.widget.AppCompatImageView, android.widget.ImageView
    public void setImageResource(int i10) {
        if (this.f1191f == null) {
            super.setImageResource(i10);
            return;
        }
        Drawable drawableMutate = a.getDrawable(getContext(), i10).mutate();
        this.f1192g = drawableMutate;
        Drawable[] drawableArr = this.Q;
        drawableArr[0] = drawableMutate;
        drawableArr[1] = this.f1191f;
        LayerDrawable layerDrawable = new LayerDrawable(drawableArr);
        this.R = layerDrawable;
        super.setImageDrawable(layerDrawable);
        setCrossfade(this.f1193h);
    }

    public void setImageRotate(float f10) {
        this.V = f10;
        e();
    }

    public void setImageZoom(float f10) {
        this.U = f10;
        e();
    }

    @RequiresApi(21)
    public void setRound(float f10) {
        if (Float.isNaN(f10)) {
            this.f1195j = f10;
            float f11 = this.f1194i;
            this.f1194i = -1.0f;
            setRoundPercent(f11);
            return;
        }
        boolean z10 = this.f1195j != f10;
        this.f1195j = f10;
        if (f10 != 0.0f) {
            if (this.f1196k == null) {
                this.f1196k = new Path();
            }
            if (this.f1198m == null) {
                this.f1198m = new RectF();
            }
            if (this.f1197l == null) {
                b bVar = new b(this, 1);
                this.f1197l = bVar;
                setOutlineProvider(bVar);
            }
            setClipToOutline(true);
            this.f1198m.set(0.0f, 0.0f, getWidth(), getHeight());
            this.f1196k.reset();
            Path path = this.f1196k;
            RectF rectF = this.f1198m;
            float f12 = this.f1195j;
            path.addRoundRect(rectF, f12, f12, Path.Direction.CW);
        } else {
            setClipToOutline(false);
        }
        if (z10) {
            invalidateOutline();
        }
    }

    @RequiresApi(21)
    public void setRoundPercent(float f10) {
        boolean z10 = this.f1194i != f10;
        this.f1194i = f10;
        if (f10 != 0.0f) {
            if (this.f1196k == null) {
                this.f1196k = new Path();
            }
            if (this.f1198m == null) {
                this.f1198m = new RectF();
            }
            if (this.f1197l == null) {
                b bVar = new b(this, 0);
                this.f1197l = bVar;
                setOutlineProvider(bVar);
            }
            setClipToOutline(true);
            int width = getWidth();
            int height = getHeight();
            float fMin = (Math.min(width, height) * this.f1194i) / 2.0f;
            this.f1198m.set(0.0f, 0.0f, width, height);
            this.f1196k.reset();
            this.f1196k.addRoundRect(this.f1198m, fMin, fMin, Path.Direction.CW);
        } else {
            setClipToOutline(false);
        }
        if (z10) {
            invalidateOutline();
        }
    }

    public void setSaturation(float f10) {
        c cVar = this.f1189d;
        cVar.f4705e = f10;
        cVar.a(this);
    }

    public void setWarmth(float f10) {
        c cVar = this.f1189d;
        cVar.f4707g = f10;
        cVar.a(this);
    }

    public ImageFilterView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f1189d = new c();
        this.f1190e = true;
        this.f1191f = null;
        this.f1192g = null;
        this.f1193h = 0.0f;
        this.f1194i = 0.0f;
        this.f1195j = Float.NaN;
        this.Q = new Drawable[2];
        this.S = Float.NaN;
        this.T = Float.NaN;
        this.U = Float.NaN;
        this.V = Float.NaN;
        c(context, attributeSet);
    }

    public ImageFilterView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f1189d = new c();
        this.f1190e = true;
        this.f1191f = null;
        this.f1192g = null;
        this.f1193h = 0.0f;
        this.f1194i = 0.0f;
        this.f1195j = Float.NaN;
        this.Q = new Drawable[2];
        this.S = Float.NaN;
        this.T = Float.NaN;
        this.U = Float.NaN;
        this.V = Float.NaN;
        c(context, attributeSet);
    }
}
