package com.google.android.material.timepicker;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import androidx.core.view.s0;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import vi.d;
import vi.k;
import vi.l;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes3.dex */
public class ClockHandView extends View {
    private final int A;
    private float B;
    private boolean C;
    private double D;
    private int E;

    /* renamed from: p, reason: collision with root package name */
    private ValueAnimator f13757p;

    /* renamed from: q, reason: collision with root package name */
    private boolean f13758q;

    /* renamed from: r, reason: collision with root package name */
    private float f13759r;

    /* renamed from: s, reason: collision with root package name */
    private float f13760s;

    /* renamed from: t, reason: collision with root package name */
    private boolean f13761t;

    /* renamed from: u, reason: collision with root package name */
    private int f13762u;

    /* renamed from: v, reason: collision with root package name */
    private final List f13763v;

    /* renamed from: w, reason: collision with root package name */
    private final int f13764w;

    /* renamed from: x, reason: collision with root package name */
    private final float f13765x;

    /* renamed from: y, reason: collision with root package name */
    private final Paint f13766y;

    /* renamed from: z, reason: collision with root package name */
    private final RectF f13767z;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public class a implements ValueAnimator.AnimatorUpdateListener {
        a() {
        }

        @Override // android.animation.ValueAnimator.AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            ClockHandView.this.m(((Float) valueAnimator.getAnimatedValue()).floatValue(), true);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public class b extends AnimatorListenerAdapter {
        b() {
        }

        @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
        public void onAnimationCancel(Animator animator) {
            animator.end();
        }
    }

    /* loaded from: classes3.dex */
    public interface c {
        void a(float f10, boolean z10);
    }

    public ClockHandView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, vi.b.D);
    }

    private void c(Canvas canvas) {
        int height = getHeight() / 2;
        float width = getWidth() / 2;
        float cos = (this.E * ((float) Math.cos(this.D))) + width;
        float f10 = height;
        float sin = (this.E * ((float) Math.sin(this.D))) + f10;
        this.f13766y.setStrokeWidth(BitmapDescriptorFactory.HUE_RED);
        canvas.drawCircle(cos, sin, this.f13764w, this.f13766y);
        double sin2 = Math.sin(this.D);
        double cos2 = Math.cos(this.D);
        this.f13766y.setStrokeWidth(this.A);
        canvas.drawLine(width, f10, r1 + ((int) (cos2 * r6)), height + ((int) (r6 * sin2)), this.f13766y);
        canvas.drawCircle(width, f10, this.f13765x, this.f13766y);
    }

    private int e(float f10, float f11) {
        int degrees = ((int) Math.toDegrees(Math.atan2(f11 - (getHeight() / 2), f10 - (getWidth() / 2)))) + 90;
        return degrees < 0 ? degrees + 360 : degrees;
    }

    private Pair h(float f10) {
        float f11 = f();
        if (Math.abs(f11 - f10) > 180.0f) {
            if (f11 > 180.0f && f10 < 180.0f) {
                f10 += 360.0f;
            }
            if (f11 < 180.0f && f10 > 180.0f) {
                f11 += 360.0f;
            }
        }
        return new Pair(Float.valueOf(f11), Float.valueOf(f10));
    }

    private boolean i(float f10, float f11, boolean z10, boolean z11, boolean z12) {
        float e10 = e(f10, f11);
        boolean z13 = false;
        boolean z14 = f() != e10;
        if (z11 && z14) {
            return true;
        }
        if (!z14 && !z10) {
            return false;
        }
        if (z12 && this.f13758q) {
            z13 = true;
        }
        l(e10, z13);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void m(float f10, boolean z10) {
        float f11 = f10 % 360.0f;
        this.B = f11;
        this.D = Math.toRadians(f11 - 90.0f);
        int height = getHeight() / 2;
        float width = (getWidth() / 2) + (this.E * ((float) Math.cos(this.D)));
        float sin = height + (this.E * ((float) Math.sin(this.D)));
        RectF rectF = this.f13767z;
        int i10 = this.f13764w;
        rectF.set(width - i10, sin - i10, width + i10, sin + i10);
        Iterator it = this.f13763v.iterator();
        while (it.hasNext()) {
            ((c) it.next()).a(f11, z10);
        }
        invalidate();
    }

    public void b(c cVar) {
        this.f13763v.add(cVar);
    }

    public RectF d() {
        return this.f13767z;
    }

    public float f() {
        return this.B;
    }

    public int g() {
        return this.f13764w;
    }

    public void j(int i10) {
        this.E = i10;
        invalidate();
    }

    public void k(float f10) {
        l(f10, false);
    }

    public void l(float f10, boolean z10) {
        ValueAnimator valueAnimator = this.f13757p;
        if (valueAnimator != null) {
            valueAnimator.cancel();
        }
        if (!z10) {
            m(f10, false);
            return;
        }
        Pair h10 = h(f10);
        ValueAnimator ofFloat = ValueAnimator.ofFloat(((Float) h10.first).floatValue(), ((Float) h10.second).floatValue());
        this.f13757p = ofFloat;
        ofFloat.setDuration(200L);
        this.f13757p.addUpdateListener(new a());
        this.f13757p.addListener(new b());
        this.f13757p.start();
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        c(canvas);
    }

    @Override // android.view.View
    protected void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        super.onLayout(z10, i10, i11, i12, i13);
        k(f());
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean z10;
        boolean z11;
        boolean z12;
        int actionMasked = motionEvent.getActionMasked();
        float x10 = motionEvent.getX();
        float y10 = motionEvent.getY();
        if (actionMasked == 0) {
            this.f13759r = x10;
            this.f13760s = y10;
            this.f13761t = true;
            this.C = false;
            z10 = true;
            z11 = false;
            z12 = false;
        } else if (actionMasked == 1 || actionMasked == 2) {
            int i10 = (int) (x10 - this.f13759r);
            int i11 = (int) (y10 - this.f13760s);
            this.f13761t = (i10 * i10) + (i11 * i11) > this.f13762u;
            z11 = this.C;
            z12 = actionMasked == 1;
            z10 = false;
        } else {
            z11 = false;
            z10 = false;
            z12 = false;
        }
        this.C |= i(x10, y10, z11, z10, z12);
        return true;
    }

    public ClockHandView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f13763v = new ArrayList();
        Paint paint = new Paint();
        this.f13766y = paint;
        this.f13767z = new RectF();
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, l.f40114u1, i10, k.A);
        this.E = obtainStyledAttributes.getDimensionPixelSize(l.f40134w1, 0);
        this.f13764w = obtainStyledAttributes.getDimensionPixelSize(l.f40144x1, 0);
        this.A = getResources().getDimensionPixelSize(d.f39787s);
        this.f13765x = r6.getDimensionPixelSize(d.f39785q);
        int color = obtainStyledAttributes.getColor(l.f40124v1, 0);
        paint.setAntiAlias(true);
        paint.setColor(color);
        k(BitmapDescriptorFactory.HUE_RED);
        this.f13762u = ViewConfiguration.get(context).getScaledTouchSlop();
        s0.x0(this, 2);
        obtainStyledAttributes.recycle();
    }
}
