package com.za.ui.camera;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.za.common.log.LogUtil;


/**
 *
 */
public class TakePhotoButtonView extends View {
    private Paint innerPaint;
    private Paint outPaint;
    private Paint animationPaint;
    private float angle;
    private ValueAnimator angleAnimation;
    private int mWidth;
    private int mHeight;
    private RectF rectF;

    public TakePhotoButtonView(Context context) {
        super(context);
    }

    public TakePhotoButtonView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        innerPaint = new Paint();
        innerPaint.setAntiAlias(true);
        innerPaint.setColor(Color.WHITE);

        outPaint = new Paint();
        outPaint.setAntiAlias(true);
        outPaint.setColor(Color.GRAY);

        animationPaint = new Paint();
        animationPaint.setAntiAlias(true);
        animationPaint.setColor(Color.GREEN);
        angleAnimation = ValueAnimator.ofFloat(0f, 360f);
        angleAnimation.setDuration(1000)
                .setRepeatCount(-1);
        angleAnimation.addUpdateListener(animation -> {
            angle = (Float) animation.getAnimatedValue();
            invalidate();
        });

        rectF = new RectF(0f, 0f, 0f, 0f);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getWidth();
        mHeight = getHeight();

        rectF.left = -mHeight / 2f;
        rectF.top = -mHeight / 2f;
        rectF.right = mWidth / 2f;
        rectF.bottom = mHeight / 2f;
    }


    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2f, mHeight / 2f);
        canvas.drawCircle(0f, 0f, mWidth / 2f, outPaint);
        canvas.drawArc(rectF, 270f, angle, true, animationPaint);
        canvas.drawCircle(0f, 0f, mWidth / 2f - 40f, innerPaint);
    }

    /**
     * 开始动画
     */
    public void startAnimation() {
        angleAnimation.start();
    }

    /**
     * 暂停动画
     */
    public void cancelAnimation() {
        try {
            if (angleAnimation == null) {
                return;
            }
            angleAnimation.setCurrentFraction(0f);
            angleAnimation.pause();
        } catch (Exception e) {
            LogUtil.INSTANCE.print("cancelAnimation", e);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        angleAnimation.cancel();
        angleAnimation.removeAllUpdateListeners();
        angleAnimation = null;
    }
}
