package oppa.paperstyle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

/**
 * Material seekbar.
 * @author DrFailov
 * @author aNNiMON
 */
public class PaperSeekBar extends SeekBar {

    private static final int ANIMATION_DELAY = 20;

    private final Paint mColorPaint = new Paint();
    private final Paint mGrayPaint = new Paint();

    private int mColor;
    private float mCurrentProgress;

    private float mCircleSizeCurrent;
    private float mCircleSizeNormal;
    private float mCircleSizeSelected;

    public PaperSeekBar(Context context) {
        super(context);

        mColor = 0xFFF4B400;

        init();
    }

    public PaperSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        final TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.PaperSeekBar, 0, 0);
        try {
            mColor = a.getColor(R.styleable.PaperSeekBar_pw_color, 0xFFF4B400);
        } finally {
            a.recycle();
        }

        init();
    }

    private void init() {
        mColorPaint.setColor(mColor);
        mColorPaint.setStyle(Paint.Style.FILL);
        mColorPaint.setAntiAlias(true);

        mGrayPaint.setColor(0xFFC8C8C8);
        mGrayPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mColorPaint.setStrokeWidth(h / 10f);
        mGrayPaint.setStrokeWidth(h / 10f);
        mCircleSizeNormal = h / 4f;
        mCircleSizeSelected = h / 2f;
        mCircleSizeCurrent = mCircleSizeNormal;
    }

    public int getColor() {
        return mColor;
    }

    public void setColor(int color) {
        mColor = color;
        mColorPaint.setColor(mColor);
        invalidate();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        final float width = getWidth(), height = getHeight();
        final float cur = mCurrentProgress * width / getMax();
        if (getProgress() != 0) {
            canvas.drawLine(0, height / 2, cur, height / 2, mColorPaint);
        }
        canvas.drawLine(cur + mCircleSizeCurrent, height / 2, width, height / 2, mGrayPaint);
        canvas.drawCircle(cur, height / 2, mCircleSizeCurrent,
                (getProgress() == 0) ? mGrayPaint : mColorPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mCircleSizeCurrent = mCircleSizeSelected;
                return true;
            case MotionEvent.ACTION_UP:
                mCircleSizeCurrent = mCircleSizeNormal;
                mAnimationHandler.sendEmptyMessage(0);
                return true;
            case MotionEvent.ACTION_MOVE:
                mCircleSizeCurrent = mCircleSizeSelected;
                mCurrentProgress = getProgress();
                return true;
        }
        return false;
    }

    protected void onDetachedFromWindow() {
        mAnimationHandler.removeMessages(0);
        super.onDetachedFromWindow();
    }

    private final Handler mAnimationHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            final float max = getProgress() - mCurrentProgress;
            mCurrentProgress += max * 0.2f;
            invalidate();
            if (Math.abs(max) > 1.0f) {
                mAnimationHandler.sendEmptyMessageDelayed(0, ANIMATION_DELAY);
            }
        }
    };
}

@@ -734,30 +734,29 @@
  mCircleSize
-Select
+Touch
 ed;%0A%0A    pub
@@ -1789,22 +1789,21 @@
 rcleSize
-Select
+Touch
 ed = h /
@@ -2115,24 +2115,70 @@
 s canvas) %7B%0A
+        final float gap = mCircleSizeTouched;%0A
         fina
@@ -2203,24 +2203,30 @@
 tWidth()
-, height
+ - 2 * gap, h2
  = getHe
@@ -2231,16 +2231,20 @@
 Height()
+ / 2
 ;%0A      
@@ -2262,16 +2262,22 @@
 at cur =
+ gap +
  mCurren
@@ -2336,9 +2336,9 @@
 s() 
-!
+=
 = 0)
@@ -2356,62 +2356,121 @@
     
-canvas.drawLine(0, height / 2, cur, height / 2, mColor
+// Draw gray circle on left side%0A            canvas.drawLine(cur + mCircleSizeCurrent, h2, gap + width, h2, mGray
 Pain
@@ -2473,30 +2473,24 @@
 Paint);%0A    
-    %7D%0A
         canv
@@ -2492,34 +2492,39 @@
  canvas.draw
-Line(cur +
+Circle(cur, h2,
  mCircleSize
@@ -2534,168 +2534,295 @@
 rent
-, height / 2, width, height / 2, mGrayPaint);%0A        canvas.drawCircle(cur, height / 2, mCircleSizeCurrent,%0A                (getProgress() == 0) ? mGrayPaint :
+ - mGrayPaint.getStrokeWidth() / 2, mGrayPaint);%0A        %7D else %7B%0A            canvas.drawLine(mCircleSizeTouched, h2, cur, h2, mColorPaint);%0A            canvas.drawLine(cur + mCircleSizeCurrent, h2, gap + width, h2, mGrayPaint);%0A            canvas.drawCircle(cur, h2, mCircleSizeCurrent,
  mCo
@@ -2828,24 +2828,34 @@
 olorPaint);%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -3070,38 +3070,37 @@
 nt = mCircleSize
-Select
+Touch
 ed;%0A            
@@ -3390,14 +3390,13 @@
 Size
-Select
+Touch
 ed;%0A

Fix #1. Improved SeekBar view