package uk.co.barbuzz.beerprogressview;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.nineoldandroids.animation.ObjectAnimator;
/* loaded from: classes.dex */
public class BeerProgressView extends View {
    private static final int BEER_DEFAULT_COLOR = Color.parseColor("#EFA601");
    private static final int BUBBLE_DEFAULT_COLOR = Color.parseColor("#B67200");
    private static final int BUBBLE_DEFAULT_COUNT = 20;
    private static final int BUBBLE_FPS = 30;
    private static final String STATE_BUBBLE_COLOR = "state_bubble_color";
    private static final String STATE_INSTANCE = "state_instance";
    private static final String STATE_MAX = "state_max";
    private static final String STATE_PROGRESS = "state_progress";
    private static final String STATE_WAVE_COLOR = "state_wave_color";
    private static final String TAG = "BeerProgressView";
    private Handler handler;
    private int mAmplitude;
    private int mAngle;
    private ObjectAnimator mAngleAnim;
    private float mAngularVelocity;
    private int mBeerColor;
    private int mBeerHeight;
    private int mBeerProgress;
    private float mBeerProgressHeight;
    private int mBeerWidth;
    private Paint mBorderPaint;
    private float mBorderRadius;
    private RectF mBorderRectF;
    private int mBorderWidth;
    private int mBubbleColor;
    private int mBubbleHeight;
    private int mBubbleTopMargin;
    private int mBubbleWidth;
    private Bubble[] mBubbles;
    private Runnable mDrawBubblesRunnable;
    private float mHaftBorderRadius;
    private int mMax;
    private Paint mPaint;
    private Path mPath;
    private long mStartMilli;

    public BeerProgressView(Context context) {
        this(context, null);
    }

    public BeerProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BeerProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mHaftBorderRadius = this.mBorderRadius / 2.0f;
        this.mAngularVelocity = 2.0f;
        this.mBeerProgressHeight = 50.0f;
        this.mAmplitude = dp2px(3);
        this.mAngle = 0;
        this.mMax = 100;
        this.mBeerProgress = 0;
        this.mBeerColor = BEER_DEFAULT_COLOR;
        this.handler = new Handler();
        this.mBubbleColor = BUBBLE_DEFAULT_COLOR;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BeerProgressView);
        this.mBorderWidth = a.getDimensionPixelSize(R.styleable.BeerProgressView_waveBorderWidth, this.mBorderWidth);
        this.mAmplitude = a.getDimensionPixelSize(R.styleable.BeerProgressView_waveAmplitude, this.mAmplitude);
        this.mBorderRadius = a.getDimensionPixelSize(R.styleable.BeerProgressView_waveBorderRadius, (int) this.mBorderRadius);
        this.mHaftBorderRadius = this.mBorderRadius / 2.0f;
        this.mBeerColor = a.getColor(R.styleable.BeerProgressView_beerColor, this.mBeerColor);
        this.mMax = a.getInt(R.styleable.BeerProgressView_waveMax, 100);
        this.mBeerProgress = a.getInteger(R.styleable.BeerProgressView_beerProgress, 0);
        this.mBubbleColor = a.getColor(R.styleable.BeerProgressView_bubbleColor, this.mBubbleColor);
        a.recycle();
        init();
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.mBeerProgressHeight > 0.0f) {
            updatePath();
            canvas.drawPath(this.mPath, this.mPaint);
            if (this.mBorderWidth != 0) {
                canvas.drawRoundRect(this.mBorderRectF, this.mBorderRadius, this.mBorderRadius, this.mBorderPaint);
            }
        }
        if ((this.mBeerProgressHeight > 0.0f) & (this.mBeerProgress > 10)) {
            drawBubbles(canvas);
        } else {
            this.handler.removeCallbacks(this.mDrawBubblesRunnable);
        }
    }

    @Override // android.view.View
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.mBeerWidth = (int) (getWidth() - this.mHaftBorderRadius);
        this.mBeerHeight = (int) (getHeight() - this.mHaftBorderRadius);
        this.mBorderRectF.set(this.mHaftBorderRadius, this.mHaftBorderRadius, this.mBeerWidth, this.mBeerHeight);
    }

    @Override // android.view.View
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(STATE_INSTANCE, super.onSaveInstanceState());
        bundle.putInt(STATE_MAX, this.mMax);
        bundle.putInt(STATE_PROGRESS, this.mBeerProgress);
        bundle.putInt(STATE_WAVE_COLOR, this.mBeerColor);
        bundle.putInt(STATE_BUBBLE_COLOR, this.mBubbleColor);
        return bundle;
    }

    @Override // android.view.View
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            this.mMax = bundle.getInt(STATE_MAX, 100);
            this.mBeerProgress = bundle.getInt(STATE_PROGRESS, 0);
            this.mBeerColor = bundle.getInt(STATE_WAVE_COLOR, BEER_DEFAULT_COLOR);
            this.mBubbleColor = bundle.getInt(STATE_BUBBLE_COLOR, BUBBLE_DEFAULT_COLOR);
            super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE));
            return;
        }
        super.onRestoreInstanceState(state);
    }

    @Override // android.view.View
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setupAngleAnim();
    }

    @Override // android.view.View
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        cancelAngleAnim();
    }

    @Override // android.view.View
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        startOrCancelAngleAnim();
    }

    @Override // android.view.View
    public void setAlpha(float alpha) {
        super.setAlpha(alpha);
        startOrCancelAngleAnim();
    }

    private void init() {
        this.mPaint = new Paint(1);
        this.mPaint.setColor(this.mBeerColor);
        this.mBorderPaint = new Paint(1);
        this.mBorderPaint.setColor(this.mBeerColor);
        this.mBorderPaint.setStrokeWidth(this.mBorderWidth);
        this.mBorderPaint.setStyle(Paint.Style.STROKE);
        this.mBorderRectF = new RectF();
        this.mPath = new Path();
        setupAngleAnim();
        this.mDrawBubblesRunnable = new Runnable() { // from class: uk.co.barbuzz.beerprogressview.BeerProgressView.1
            @Override // java.lang.Runnable
            public void run() {
                BeerProgressView.this.invalidate();
            }
        };
    }

    private void drawBubbles(Canvas canvas) {
        this.mStartMilli = SystemClock.uptimeMillis();
        this.mBubbleHeight = this.mBeerHeight;
        this.mBubbleTopMargin = (this.mBubbleHeight - ((int) this.mBeerProgressHeight)) + 20;
        this.mBubbleWidth = canvas.getWidth();
        if (this.mBubbles == null || this.mBubbles.length != 20) {
            createBubbles(this.mBubbleWidth, this.mBubbleHeight, this.mBubbleTopMargin);
        }
        Bubble[] arr$ = this.mBubbles;
        for (Bubble bubble : arr$) {
            bubble.update(30, 0.0f);
            bubble.draw(canvas);
            if (bubble.popped(this.mBubbleWidth, this.mBubbleHeight, this.mBubbleTopMargin)) {
                bubble.recycle(false, this.mBubbleWidth, this.mBubbleHeight, this.mBubbleTopMargin);
            }
        }
        long duration = SystemClock.uptimeMillis() - this.mStartMilli;
        this.handler.postDelayed(this.mDrawBubblesRunnable, 33 - duration);
    }

    private void createBubbles(int width, int height, int topMargin) {
        this.mBubbles = new Bubble[20];
        for (int i = 0; i < 20; i++) {
            this.mBubbles[i] = new Bubble(width, height, topMargin, this.mBubbleColor);
        }
    }

    private void setupAngleAnim() {
        if (isViewVisiable()) {
            if (this.mAngleAnim == null) {
                this.mAngleAnim = ObjectAnimator.ofInt(this, "angle", 0, 360);
                this.mAngleAnim.setDuration(800L);
                this.mAngleAnim.setRepeatMode(1);
                this.mAngleAnim.setRepeatCount(-1);
                this.mAngleAnim.setInterpolator(new LinearInterpolator());
            }
            if (!this.mAngleAnim.isRunning()) {
                this.mAngleAnim.start();
            }
        }
    }

    private void cancelAngleAnim() {
        if (this.mAngleAnim != null) {
            this.mAngleAnim.cancel();
        }
    }

    private void updatePath() {
        setBeerProgress(this.mBeerProgress);
        this.mPath.reset();
        for (int i = 0; i < this.mBeerWidth; i++) {
            int x = i;
            int y = (int) clamp((this.mAmplitude * Math.sin(((i * this.mAngularVelocity) + (this.mAngle * 3.141592653589793d)) / 180.0d)) + (this.mBeerHeight - this.mBeerProgressHeight), this.mHaftBorderRadius, this.mBeerHeight);
            if (i == 0) {
                this.mPath.moveTo(x, y);
            }
            this.mPath.quadTo(x, y, x + 1, y);
        }
        this.mPath.lineTo(this.mBeerWidth, this.mBeerHeight);
        this.mPath.lineTo(0.0f, this.mBeerHeight);
        this.mPath.close();
    }

    private void startOrCancelAngleAnim() {
        if (isViewVisiable()) {
            setupAngleAnim();
        } else {
            cancelAngleAnim();
        }
    }

    private boolean isViewVisiable() {
        return getVisibility() == 0 && getAlpha() * 255.0f > 0.0f;
    }

    private static double clamp(double value, double max, double min) {
        return Math.max(Math.min(value, min), max);
    }

    private static int dp2px(int dp) {
        return (int) (Resources.getSystem().getDisplayMetrics().density * dp);
    }

    public int getMax() {
        return this.mMax;
    }

    public void setMax(int max) {
        this.mMax = max;
    }

    public int getAmplitude() {
        return this.mAmplitude;
    }

    public void setAmplitude(int amplitude) {
        this.mAmplitude = amplitude;
    }

    public int getBeerProgress() {
        return this.mBeerProgress;
    }

    public void setBeerProgress(int beerProgress) {
        this.mBeerProgress = beerProgress;
        if (this.mBeerProgress > this.mMax) {
            this.mBeerProgress = this.mMax;
        }
        if (this.mBeerProgress < 0) {
            this.mBeerProgress = 0;
        }
        float pecent = (this.mBeerProgress * 1.0f) / this.mMax;
        this.mBeerProgressHeight = this.mBeerHeight * pecent;
        invalidate();
    }

    public int getBeerColor() {
        return this.mBeerColor;
    }

    public void setBeerColor(int beerColor) {
        this.mBeerColor = beerColor;
        this.mPaint.setColor(this.mBeerColor);
        this.mBorderPaint.setColor(this.mBeerColor);
    }

    public int getBubbleColor() {
        return this.mBubbleColor;
    }

    public void setBubbleColor(int bubbleColor) {
        this.mBubbleColor = bubbleColor;
        this.mBubbles = null;
    }

    public void setAngle(int angle) {
        this.mAngle = angle;
        invalidate();
    }
}
