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.AppCompatImageButton;
import androidx.constraintlayout.widget.s;
import b0.a;
import b0.c;

/* JADX INFO: loaded from: classes.dex */
public class ImageFilterButton extends AppCompatImageButton {
    public Drawable Q;
    public Drawable 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 f1179d;

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

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final Drawable[] f1186k;

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

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

    public ImageFilterButton(Context context) {
        super(context);
        this.f1179d = new c();
        this.f1180e = 0.0f;
        this.f1181f = 0.0f;
        this.f1182g = Float.NaN;
        this.f1186k = new Drawable[2];
        this.f1188m = true;
        this.Q = null;
        this.R = null;
        this.S = Float.NaN;
        this.T = Float.NaN;
        this.U = Float.NaN;
        this.V = Float.NaN;
        a(context, null);
    }

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

    public final void a(Context context, AttributeSet attributeSet) {
        setPadding(0, 0, 0, 0);
        if (attributeSet != null) {
            TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, s.ImageFilterView);
            int indexCount = typedArrayObtainStyledAttributes.getIndexCount();
            this.Q = typedArrayObtainStyledAttributes.getDrawable(s.ImageFilterView_altSrc);
            for (int i10 = 0; i10 < indexCount; i10++) {
                int index = typedArrayObtainStyledAttributes.getIndex(i10);
                if (index == s.ImageFilterView_crossfade) {
                    this.f1180e = 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_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.f1188m));
                } 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.R = drawable;
            Drawable drawable2 = this.Q;
            Drawable[] drawableArr = this.f1186k;
            if (drawable2 == null || drawable == null) {
                Drawable drawable3 = getDrawable();
                this.R = drawable3;
                if (drawable3 != null) {
                    Drawable drawableMutate = drawable3.mutate();
                    this.R = drawableMutate;
                    drawableArr[0] = drawableMutate;
                    return;
                }
                return;
            }
            Drawable drawableMutate2 = getDrawable().mutate();
            this.R = drawableMutate2;
            drawableArr[0] = drawableMutate2;
            drawableArr[1] = this.Q.mutate();
            LayerDrawable layerDrawable = new LayerDrawable(drawableArr);
            this.f1187l = layerDrawable;
            layerDrawable.getDrawable(1).setAlpha((int) (this.f1180e * 255.0f));
            if (!this.f1188m) {
                this.f1187l.getDrawable(0).setAlpha((int) ((1.0f - this.f1180e) * 255.0f));
            }
            super.setImageDrawable(this.f1187l);
        }
    }

    public final void b() {
        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);
    }

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

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

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

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

    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.f1182g;
    }

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

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

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

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

    public void setAltImageResource(int i10) {
        Drawable drawableMutate = f.a.getDrawable(getContext(), i10).mutate();
        this.Q = drawableMutate;
        Drawable drawable = this.R;
        Drawable[] drawableArr = this.f1186k;
        drawableArr[0] = drawable;
        drawableArr[1] = drawableMutate;
        LayerDrawable layerDrawable = new LayerDrawable(drawableArr);
        this.f1187l = layerDrawable;
        super.setImageDrawable(layerDrawable);
        setCrossfade(this.f1180e);
    }

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

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

    public void setCrossfade(float f10) {
        this.f1180e = f10;
        if (this.f1186k != null) {
            if (!this.f1188m) {
                this.f1187l.getDrawable(0).setAlpha((int) ((1.0f - this.f1180e) * 255.0f));
            }
            this.f1187l.getDrawable(1).setAlpha((int) (this.f1180e * 255.0f));
            super.setImageDrawable(this.f1187l);
        }
    }

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

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

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

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

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

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

    @RequiresApi(21)
    public void setRound(float f10) {
        if (Float.isNaN(f10)) {
            this.f1182g = f10;
            float f11 = this.f1181f;
            this.f1181f = -1.0f;
            setRoundPercent(f11);
            return;
        }
        boolean z10 = this.f1182g != f10;
        this.f1182g = f10;
        if (f10 != 0.0f) {
            if (this.f1183h == null) {
                this.f1183h = new Path();
            }
            if (this.f1185j == null) {
                this.f1185j = new RectF();
            }
            if (this.f1184i == null) {
                a aVar = new a(this, 1);
                this.f1184i = aVar;
                setOutlineProvider(aVar);
            }
            setClipToOutline(true);
            this.f1185j.set(0.0f, 0.0f, getWidth(), getHeight());
            this.f1183h.reset();
            Path path = this.f1183h;
            RectF rectF = this.f1185j;
            float f12 = this.f1182g;
            path.addRoundRect(rectF, f12, f12, Path.Direction.CW);
        } else {
            setClipToOutline(false);
        }
        if (z10) {
            invalidateOutline();
        }
    }

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

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

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

    public ImageFilterButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f1179d = new c();
        this.f1180e = 0.0f;
        this.f1181f = 0.0f;
        this.f1182g = Float.NaN;
        this.f1186k = new Drawable[2];
        this.f1188m = true;
        this.Q = null;
        this.R = null;
        this.S = Float.NaN;
        this.T = Float.NaN;
        this.U = Float.NaN;
        this.V = Float.NaN;
        a(context, attributeSet);
    }

    public ImageFilterButton(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f1179d = new c();
        this.f1180e = 0.0f;
        this.f1181f = 0.0f;
        this.f1182g = Float.NaN;
        this.f1186k = new Drawable[2];
        this.f1188m = true;
        this.Q = null;
        this.R = null;
        this.S = Float.NaN;
        this.T = Float.NaN;
        this.U = Float.NaN;
        this.V = Float.NaN;
        a(context, attributeSet);
    }
}
