package com.sobot.screencapturesdk.widget;

import android.content.Context;
import android.content.res.TypedArray;
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 com.sobot.screencapturesdk.R;


public class RoundProgressBar extends View {

    private Paint mPaint;

    /**
     * 圆环的颜色
     */
    private int roundColor;

    /**
     * 圆环进度的颜色
     */
    private int roundProgressColor;

    /**
     * 圆环的宽度
     */
    private float roundWidth;

    /**
     * 最大进度
     */
    private long mMax;

    /**
     * 当前进度
     */
    private long mProgress;

    /**
     * 进度的风格，实心或者空心
     */
    private int style;
    public static final int STROKE = 0;
    public static final int FILL = 1;

    private RectF mOval = new RectF();
    private boolean mEnableProgress = false;

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

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

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint = new Paint();

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

        // 获取自定义属性和默认值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.RED);
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_width, 5);
        mMax = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
        mTypedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!mEnableProgress) {
            return;
        }
        // 准备画笔
        mPaint.setColor(roundColor);
        mPaint.setStrokeWidth(roundWidth);
        mPaint.setAntiAlias(true);

        // 绘制外层圆环
        int centre = getWidth() / 2;
        int radius = (int) (centre - roundWidth / 2);
//        canvas.drawCircle(centre, centre, radius, mPaint);

        // 画圆弧 ，画圆环的进度
        mPaint.setColor(roundProgressColor);
        mOval.set(centre - radius, centre - radius, centre + radius, centre + radius);
        float progress = (float) mProgress / mMax;
        switch (style) {
//                LogUtils.i("STROKE>>> progress" + 360 * progress + "currentProgress>>>" + progress);
            case STROKE:
                mPaint.setStyle(Paint.Style.STROKE);
                canvas.drawArc(mOval, -90, 360 * progress, false, mPaint); // 根据进度画圆弧
                break;
            case FILL:
                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                if (mProgress != 0)
                    canvas.drawArc(mOval, -90, 360 * progress, true, mPaint); // 根据进度画圆弧
                break;
        }
    }

    public long getMax() {
        return mMax;
    }

    /**
     * 设置进度的最大值
     *
     * @param max
     */
    public void setMax(long max) {
        if (max < 0) {
            throw new IllegalArgumentException("max not less than 0");
        }
        this.mMax = max;
    }

    /**
     * 获取进度.需要同步
     *
     * @return 当前进度([0 - max])
     */
    public long getProgress() {
        return mProgress;
    }

    /**
     * 设置进度, 进度应该为[0,max]
     *
     * @param progress
     */
    public void setProgress(long progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (progress > mMax) {
            progress = mMax;
            invalidate();
        } else {
            this.mProgress = progress;
            invalidate();
        }
    }

    public int getRoundColor() {
        return roundColor;
    }

    public void setRoundColor(int roundColor) {
        this.roundColor = roundColor;
    }

    public int getRoundProgressColor() {
        return roundProgressColor;
    }

    public void setRoundProgressColor(int roundProgressColor) {
        this.roundProgressColor = roundProgressColor;
    }

    /**
     * 设置是否绘制进度
     *
     * @param enable true 绘制进度
     * @return always true.
     */
    public boolean enableProgress(boolean enable) {
        mEnableProgress = enable;
        invalidate();
        return true;
    }
}
