package com.jimind.sportdemo.view;

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.view.View;

import com.jimind.sportdemo.R;

/**
 * 自定义view 圆形进度条--签到
 *
 * @author Any
 */
public class SignProgressBar extends View {

    /**
     * 圆的半径，圆环的宽度,圆环半径
     */
    private float mRadius, mStrokeWidth, mRingRadius;

    /**
     * 圆的颜色，圆环的颜色，圆环的背景色
     */
    private int mCircleColor, mRingColor, mRingBgColor;

    /**
     * 圆的画笔，圆环的画笔，圆环背景的画笔，文字画笔
     */
    private Paint mCirclePaint, mRingPaint, mRingBgPaint, mTextPaint;

    private float mTextHeight;

    private int mProgress = 0, mTotalProgress = 100;

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

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

    public SignProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttribute(context, attrs);
        initPaint();
    }

    /**
     * 初始化自定义属性
     */
    private void initAttribute(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SignProgressStyle, 0, 0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.SignProgressStyle_radius:
                    mRadius = typedArray.getDimension(attr, 80);
                    break;
                case R.styleable.SignProgressStyle_strokeWidth:
                    mStrokeWidth = typedArray.getDimension(attr, 10);
                    break;
                case R.styleable.SignProgressStyle_circleColor:
                    mCircleColor = typedArray.getColor(attr, 0);
                    break;
                case R.styleable.SignProgressStyle_ringColor:
                    mRingColor = typedArray.getColor(attr, 0);
                    break;
                case R.styleable.SignProgressStyle_ringBgColor:
                    mRingBgColor = typedArray.getColor(attr, 0);
                    break;
                default:
            }
        }
        typedArray.recycle();
        mRingRadius = mRadius + mStrokeWidth / 2;
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        //内圆
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        //外圆环背景
        mRingBgPaint = new Paint();
        mRingBgPaint.setAntiAlias(true);
        mRingBgPaint.setColor(mRingBgColor);
        mRingBgPaint.setStyle(Paint.Style.STROKE);
        mRingBgPaint.setStrokeWidth(mStrokeWidth);

        //外圆环
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);
        //设置线冒样式，有圆 有方
        mRingPaint.setStrokeCap(Paint.Cap.ROUND);

        //字体
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mRingColor);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(mRadius / 2);

        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = (float) Math.ceil(fontMetrics.descent - fontMetrics.ascent);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int xCenter = getWidth() / 2;
        int yCenter = getHeight() / 2;

        //内圆
        canvas.drawCircle(xCenter, yCenter, mRadius, mCirclePaint);

        //外圆环背景
        RectF bgOval = new RectF();
        bgOval.left = xCenter - mRingRadius;
        bgOval.top = yCenter - mRingRadius;
        bgOval.right = xCenter + mRingRadius;
        bgOval.bottom = yCenter + mRingRadius;
        canvas.drawArc(bgOval, 0, 360, false, mRingBgPaint);

        //外圆环
        canvas.drawArc(bgOval, -90, 360 * mProgress / mTotalProgress, false, mRingPaint);

        //文字
        String content = String.valueOf(mProgress);
        float textWidth = mTextPaint.measureText(content, 0, content.length());
        canvas.drawText(content, xCenter - textWidth / 2, yCenter + mTextHeight / 4, mTextPaint);
    }

    /**
     * 更新进度
     *
     * @param progress
     */
    public void setProgress(int progress) {
        mProgress = progress;
        invalidate();
    }

    public void setTotalProgress(int progress) {
        mTotalProgress = progress;
    }
}
