
package net.qiujuer.genius.ui.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.os.SystemClock;

/**
 * <h1>SeekBarDrawable</h1>
 * <p>
 * Special {@link net.qiujuer.genius.ui.drawable.SeekBarStatusDrawable} implementation
 * To draw the Track, Scrubber and Thumb circle.
 * </p>
 * <p>
 * It's special because it will stop drawing once the state is pressed/focused BUT only after a small delay.
 * </p>
 * <p>
 * This special delay is meant to help avoiding frame glitches while the {@link BalloonMarkerDrawable} is added to the Window
 * </p>
 *
 * @hide
 */
public class SeekBarDrawable extends SeekBarStatusDrawable implements Animatable {
    private Point mPoint;
    private int mContentWidth;
    private float mTickDistance;
    private float mHotScale;

    private int mNumSegments;
    private int mTrackStroke;
    private int mScrubberStroke;
    private int mThumbRadius;
    private int mTickRadius;
    private int mTouchRadius;

    private boolean isRtl;
    private boolean isOpen;
    private boolean isRunning;
    private Runnable mOpener = new Runnable() {
        @Override
        public void run() {
            isOpen = true;
            invalidateSelf();
            isRunning = false;
        }
    };

    public SeekBarDrawable(ColorStateList trackStateList, ColorStateList scrubberStateList, ColorStateList thumbStateList) {
        super(trackStateList, scrubberStateList, thumbStateList);
        mPoint = new Point();
    }

    public void setRtl(boolean isRtl) {
        this.isRtl = isRtl;
    }

    public void setNumSegments(int numSegments) {
        this.mNumSegments = numSegments;
    }

    public int getTickRadius() {
        return mTickRadius;
    }

    public void setTickRadius(int tickRadius) {
        this.mTickRadius = tickRadius;
    }

    public int getThumbRadius() {
        return mThumbRadius;
    }

    public void setThumbRadius(int thumbRadius) {
        this.mThumbRadius = thumbRadius;
    }

    public int getTouchRadius() {
        return mTouchRadius;
    }

    public void setTouchRadius(int touchRadius) {
        this.mTouchRadius = touchRadius;
    }

    public int getScrubberStroke() {
        return mScrubberStroke;
    }

    public void setScrubberStroke(int scrubberStroke) {
        if (scrubberStroke < 0)
            scrubberStroke = 0;
        this.mScrubberStroke = scrubberStroke;
    }

    public int getTrackStroke() {
        return mTrackStroke;
    }

    public void setTrackStroke(int trackStroke) {
        if (trackStroke < 0)
            trackStroke = 0;
        this.mTrackStroke = trackStroke;
    }

    public float getHotScale() {
        return mHotScale;
    }

    public void setHotScale(float scale) {
        mHotScale = scale;
        int hotWidth = getHotWidth();
        Rect bounds = getBounds();
        int x;
        if (isRtl) {
            x = bounds.right - mTouchRadius - hotWidth;
        } else {
            x = bounds.left + mTouchRadius + hotWidth;
        }
        mPoint.set(x, bounds.centerY());
    }

    public boolean isHaveTick() {
        return mTickRadius != 0;
    }

    public Point getPosPoint() {
        return mPoint;
    }

    public void copyTouchBounds(Rect rect) {
        Rect bounds = getBounds();
        int hotWidth = getHotWidth();
        int x;
        if (isRtl) {
            x = bounds.right - mTouchRadius - hotWidth;
        } else {
            x = bounds.left + mTouchRadius + hotWidth;
        }
        rect.set(x - mTouchRadius, bounds.top,
                x + mTouchRadius, bounds.bottom);
    }

    public void animateToPressed() {
        // Delay 100'''
        scheduleSelf(mOpener, SystemClock.uptimeMillis() + 100);
        isRunning = true;
    }

    public void animateToNormal() {
        isOpen = false;
        isRunning = false;
        unscheduleSelf(mOpener);
        invalidateSelf();
    }

    @Override
    public void start() {
    }

    @Override
    public void stop() {
        animateToNormal();
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        mContentWidth = bounds.right - bounds.left - mTouchRadius - mTouchRadius;
        mTickDistance = (float) mContentWidth / (float) mNumSegments;
        setHotScale(mHotScale);
    }

    @Override
    public int getIntrinsicHeight() {
        int maxHeight = Math.max(mTrackStroke, mScrubberStroke);
        maxHeight = Math.max(maxHeight, mThumbRadius * 2);
        maxHeight = Math.max(maxHeight, mTickRadius * 2);
        maxHeight = Math.max(maxHeight, mTouchRadius * 2);
        return maxHeight;
    }

    private int getHotWidth() {
        return (int) (mContentWidth * mHotScale);
    }

    @Override
    public void draw(Canvas canvas, Paint paint, int trackColor, int trackAlpha, int scrubberColor, int scrubberAlpha, int thumbColor, int thumbAlpha) {
        float halfTrackStroke = mTrackStroke / 2;
        float halfScrubberStroke = mScrubberStroke / 2;

        if (isRtl) {
            draw(canvas, paint, thumbColor, thumbAlpha, trackColor, scrubberColor, trackAlpha, scrubberAlpha, halfTrackStroke, halfScrubberStroke);
        } else {
            draw(canvas, paint, thumbColor, thumbAlpha, scrubberColor, trackColor, scrubberAlpha, trackAlpha, halfScrubberStroke, halfTrackStroke);
        }
    }


    private void draw(Canvas canvas, Paint paint, int thumbColor, int thumbAlpha, int colorLeft, int colorRight, int alphaLeft, int alphaRight, float halfLeft, float halfRight) {
        Rect bounds = getBounds();
        int thumbX = mPoint.x;
        int thumbY = mPoint.y;
        int startLeft = bounds.left + mTouchRadius;
        int startRight = bounds.right - mTouchRadius;

        // Track
        if (halfLeft > 0) {
            paint.setColor(colorLeft);
            paint.setAlpha(alphaLeft);
            canvas.drawRect(startLeft, thumbY - halfLeft, thumbX, thumbY + halfLeft, paint);
        }

        // Scrubber
        if (halfRight > 0) {
            paint.setColor(colorRight);
            paint.setAlpha(alphaRight);
            canvas.drawRect(thumbX, thumbY - halfRight, startRight, thumbY + halfRight, paint);
        }

        // Ticks Right
        if (mTickRadius > halfRight) {
            for (int i = 0; i <= mNumSegments; i++) {
                float x = startRight - i * mTickDistance;
                if (x <= thumbX)
                    break;
                canvas.drawCircle(x, thumbY, mTickRadius, paint);
            }
        }

        // Ticks Left
        if (mThumbRadius > halfLeft) {
            paint.setColor(colorLeft);
            paint.setAlpha(alphaLeft);
            for (int i = 0; i <= mNumSegments; i++) {
                float x = i * mTickDistance + startLeft;
                if (x > thumbX)
                    break;
                canvas.drawCircle(x, thumbY, mTickRadius, paint);
            }
        }

        // Thumb
        if (!isOpen && mThumbRadius > 0) {
            paint.setColor(thumbColor);
            paint.setAlpha(thumbAlpha);
            canvas.drawCircle(thumbX, thumbY, mThumbRadius, paint);
        }
    }
}
