package com.secspace.lib.common.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.secspace.lib.common.R;


/**
 * Created by cyw on 17-11-15.
 */

public class DotTrailingProgress extends View {
    private Context mContext;

    private Paint mCirclePaint;

    // 刻度画笔
    private Paint mScalePaint;

    // 小原点画笔
    private Paint mDotPaint;

    //遮罩画笔
    private Paint mShadePaint;

    // 文字画笔
    private Paint mTextPaint;

    // 当前进度
    private int progress = 0;

    /**
     * 小圆点的当前进度
     */
    public float mDotProgress;

    // View宽
    private int mWidth;

    // View高
    private int mHeight;

    private int indexColor;

    private int baseColor;

    private int dotColor;

    private int textSize;

    private int textColor;

    private ValueAnimator animator;

    private int[] shadeColors = new int[]{0x00009bd6, 0x00009bd6, 0x00009bd6, 0x19009bd6, 0x66009bd6};
    private int[] shadeLineColors = new int[]{0x00009bd6, 0xFF009bd6, 0xFF009bd6};
    private RectF oval;
    private Shader mShader;
    private Shader mShaderLine;

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

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

    public DotTrailingProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取用户配置属性
        TypedArray tya = context.obtainStyledAttributes(attrs, R.styleable.DotRotateProgress);
        baseColor = tya.getColor(R.styleable.DotRotateProgress_baseColor, Color.LTGRAY);
        indexColor = tya.getColor(R.styleable.DotRotateProgress_indexColor, Color.BLUE);
        textColor = tya.getColor(R.styleable.DotRotateProgress_textColor, Color.BLUE);
        dotColor = tya.getColor(R.styleable.DotRotateProgress_dotColor, Color.RED);
        textSize = tya.getDimensionPixelSize(R.styleable.DotRotateProgress_textSize, 36);
        tya.recycle();
        initUI();
    }

    private void initUI() {
        mContext = getContext();

        // 外圆画笔
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStrokeWidth(dip2px(mContext, 2));
        mCirclePaint.setStrokeCap(Paint.Cap.ROUND);
        mCirclePaint.setColor(baseColor);
        mCirclePaint.setStyle(Paint.Style.STROKE);

        // 刻度画笔
        mScalePaint = new Paint();
        mScalePaint.setAntiAlias(true);
        mScalePaint.setStrokeWidth(dip2px(mContext, 1));
        mScalePaint.setStrokeCap(Paint.Cap.ROUND);
        mScalePaint.setColor(baseColor);
        mScalePaint.setStyle(Paint.Style.STROKE);

        // 小圆点画笔
        mDotPaint = new Paint();
        mDotPaint.setAntiAlias(true);
        mDotPaint.setColor(dotColor);
        mDotPaint.setStrokeWidth(dip2px(mContext, 1));
        mDotPaint.setStyle(Paint.Style.FILL);

        // 遮罩画笔
        mShadePaint = new Paint();
        mShadePaint.setAntiAlias(true);
        mShadePaint.setColor(dotColor);
        mShadePaint.setStrokeWidth(dip2px(mContext, 1));
        mShadePaint.setStyle(Paint.Style.FILL);

        // 文字画笔
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(textColor);
        mTextPaint.setTextSize(textSize);
        mTextPaint.setStrokeWidth(dip2px(mContext, 1));
        mTextPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//      drawArcScale(canvas);
//        drawCircle(canvas);
        drawTextValue(canvas);
        drawRotateDot(canvas);
        drawShadeCircle(canvas);
    }

    private void drawCircle(Canvas canvas) {
        canvas.save();
        canvas.drawCircle(mWidth / 2
                , mHeight / 2
                , mHeight / 2 - mCirclePaint.getStrokeWidth() - dip2px(mContext, 8)
                , mCirclePaint);
        canvas.restore();
    }

    private void drawShadeCircle(Canvas canvas) {
        canvas.save();
        canvas.drawCircle(mWidth / 2
                , mHeight / 2
                , mHeight / 2 - mCirclePaint.getStrokeWidth() - dip2px(mContext, 12)
                , mShadePaint);
        canvas.restore();
    }

    /**
     * 画刻度
     */
    private void drawArcScale(Canvas canvas) {
        canvas.save();

        for (int i = 0; i < 100; i++) {
            if (progress > i) {
                mScalePaint.setColor(indexColor);
            } else {
                mScalePaint.setColor(baseColor);
            }
            canvas.drawLine(mWidth / 2, 0, mHeight / 2, dip2px(mContext, 10), mScalePaint);
            // 旋转的度数 = 100 / 360
            canvas.rotate(3.6f, mWidth / 2, mHeight / 2);
        }

        canvas.restore();
    }

    /**
     * 画内部数值
     */
    private void drawTextValue(Canvas canvas) {
        canvas.save();

        String showValue = String.valueOf(progress) + "%";
        Rect textBound = new Rect();
        mTextPaint.getTextBounds(showValue, 0, showValue.length(), textBound);    // 获取文字的矩形范围
        float textWidth = textBound.right - textBound.left;  // 获得文字宽
        float textHeight = textBound.bottom - textBound.top; // 获得文字高
        canvas.drawText(showValue, mWidth / 2 - textWidth / 2, mHeight / 2 + textHeight / 2, mTextPaint);

        canvas.restore();
    }

    /**
     * 画旋转小圆点
     */
    private void drawRotateDot(final Canvas canvas) {
        canvas.save();

        canvas.rotate(mDotProgress * 3.6f, mWidth / 2, mHeight / 2);
        canvas.drawCircle(mWidth / 2
                , dip2px(mContext, 4)
                , dip2px(mContext, 4)
                , mDotPaint);
        canvas.drawArc(oval, 2, 268, false, mCirclePaint);
        canvas.restore();
    }

    /**
     * 启动小圆点旋转动画
     */
    public void startDotAnimator() {
        animator = ValueAnimator.ofFloat(0, 100);
        animator.setDuration(1500);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 设置小圆点的进度，并通知界面重绘
                mDotProgress = (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }

    /**
     * 设置进度
     */
    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        oval = new RectF(dip2px(mContext, 4), dip2px(mContext, 4),
                mWidth - dip2px(mContext, 4), mHeight - dip2px(mContext, 4));

        mShader = new RadialGradient(mWidth / 2
                , mHeight / 2
                , mHeight / 2 - mCirclePaint.getStrokeWidth() - dip2px(mContext, 12), shadeColors, null, Shader.TileMode.CLAMP);
        mShaderLine = new SweepGradient(mWidth / 2
                , mHeight / 2
                , shadeLineColors, null);
        mShadePaint.setShader(mShader);
        mCirclePaint.setShader(mShaderLine);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int myWidthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int myWidthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int myHeightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int myHeightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        // 获取宽
        if (myWidthSpecMode == MeasureSpec.EXACTLY) {
            // match_parent/精确值
            mWidth = myWidthSpecSize;
        } else {
            // wrap_content
            mWidth = dip2px(mContext, 120);
        }

        // 获取高
        if (myHeightSpecMode == MeasureSpec.EXACTLY) {
            // match_parent/精确值
            mHeight = myHeightSpecSize;
        } else {
            // wrap_content
            mHeight = dip2px(mContext, 120);
        }

        // 设置该view的宽高
        setMeasuredDimension(mWidth, mHeight);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
