package com.liulishuo.magicprogress.library.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.View;

import com.liulishuo.magicprogress.library.R;


/**
 * Created by Jacksgong on 12/8/15.
 * <p/>
 * 轻量的ProgressBar
 */
public class MagicProgressBar extends View {

    @ColorInt
    private int fillColor;

    @ColorInt
    private int backgroundColor;

    private Paint fillPaint;
    private Paint backgroundPaint;

    private float fillPercent = 0.8f;

    public MagicProgressBar(Context context) {
        super(context);
        init(context, null);
    }

    public MagicProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MagicProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MagicProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(final Context context, AttributeSet attrs) {
        if (context == null || attrs == null) {
            return;
        }


        TypedArray typedArray = null;
        try {
            typedArray = context.obtainStyledAttributes(attrs, R.styleable.magic_progress_bar);
            fillPercent = typedArray.getFloat(R.styleable.magic_progress_bar_mpb_percent, 0);
            fillColor = typedArray.getColor(R.styleable.magic_progress_bar_mpb_fill_color, 0);
            backgroundColor = typedArray.getColor(R.styleable.magic_progress_bar_mpb_default_color, 0);
        } finally {
            if (typedArray != null) {
                typedArray.recycle();
            }
        }

        fillPaint = new Paint();
        fillPaint.setColor(fillColor);
        fillPaint.setAntiAlias(true);

        backgroundPaint = new Paint();
        backgroundPaint.setColor(backgroundColor);
        backgroundPaint.setAntiAlias(true);

    }

    public float getFillPercent() {
        return this.fillPercent;
    }

    public void setFillPercent(final float fillPercent) {
        this.fillPercent = fillPercent;
        invalidate();
    }

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

        float drawPercent = fillPercent;

        canvas.save();
        final int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        final int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();

        final int fillWidth = (int) (drawPercent * width);
        final float radius = height / 2.0f;


        RectF rectF = new RectF(0, 0, width, height);

        // draw background
        if (backgroundColor != 0) {
            canvas.drawRoundRect(rectF, radius, radius, backgroundPaint);
        }


        // draw fill
        if (fillColor != 0) {
            rectF.right = fillWidth;
            canvas.drawRoundRect(rectF, radius, radius, fillPaint);
        }

        canvas.restore();
    }
}

@@ -313,24 +313,70 @@
 n.ColorInt;%0A
+import android.support.annotation.FloatRange;%0A
 import andro
@@ -765,26 +765,15 @@
 oat 
-fillP
+p
 ercent
- = 0.8f
 ;%0A%0A 
@@ -1729,21 +1729,17 @@
         
-fillP
+p
 ercent =
@@ -2400,20 +2400,16 @@
 loat get
-Fill
 Percent(
@@ -2428,29 +2428,25 @@
 return this.
-fillP
+p
 ercent;%0A    
@@ -2471,20 +2471,16 @@
  set
-Fill
 Percent(
 fina
@@ -2475,16 +2475,50 @@
 Percent(
+@FloatRange(from = 0.0, to = 1.0) 
 final fl
@@ -2521,21 +2521,17 @@
 l float 
-fillP
+p
 ercent) 
@@ -2545,21 +2545,17 @@
    this.
-fillP
+p
 ercent =
@@ -2547,37 +2547,33 @@
  this.percent = 
-fillP
+p
 ercent;%0A        
@@ -2709,21 +2709,17 @@
 rcent = 
-fillP
+p
 ercent;%0A

rename MagicProgressBar fillPercent -> percent, & set MagicProgressBar default percent 0.8 -> 0.0