package com.tenlionsoft.baselib.core.widget.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 作者 : Adam on 2018/10/15.
 * 邮箱 : itgaojian@163.com
 * 描述 : 闪屏页圆形进度倒计时
 */
public class CircleProgressView extends View {


    private RectF mRectF;
    private Paint mPaint;
    /**
     * 画笔颜色 默认蓝色
     */
    private int paintColor = Color.BLUE;
    /**
     * 进度条圆弧宽度
     */
    private int mStrokeWidth = 8;
    /**
     * 最大进度
     */
    private float mMaxProgress = 100;
    /**
     * 当前进度
     */
    private float mProgress = 0;


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

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        //位置
        mRectF.left = mStrokeWidth / 2;
        mRectF.top = mStrokeWidth / 2;
        mRectF.right = width - mStrokeWidth / 2;
        mRectF.bottom = height - mStrokeWidth / 2;

        //设置画布为透明
        canvas.drawColor(Color.TRANSPARENT);

        //画个半透明的圆当背景
        mPaint.setColor(Color.parseColor("#33000000"));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawOval(mRectF, mPaint);


        //设置画笔
        mPaint.setAntiAlias(true);
        mPaint.setColor(paintColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);


        canvas.drawArc(mRectF, -90, -(mProgress / mMaxProgress) * 360, false, mPaint);
    }

    /**
     * 设置画笔颜色
     *
     * @param color
     */
    public void setPaintColor(String color) {
        paintColor = Color.parseColor(color);
    }

    /**
     * 设置到最大进度的时间
     *
     * @param time 倒计时时间 毫秒值
     */
    public void startDownTime(final long time, final OnFinishListener listener) {
        mMaxProgress = 100;
        new Thread(() -> {
            for (int i = (int) mMaxProgress; i >= 0; i--) {
                try {
                    Thread.sleep((long) (time / mMaxProgress));
                    //当倒计时结束时通知
                    if (i == 0 && listener != null) {
                        post(() -> listener.onFinish());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mProgress = (float) i;
                CircleProgressView.this.postInvalidate();
            }
        }).start();
    }


}
