package in.myinnos.alphabetsindexfastscrollrecycler;

/**
 * Created by MyInnos on 31-01-2017.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class IndexFastScrollRecyclerView extends RecyclerView {

    private IndexFastScrollRecyclerSection mScroller = null;
    private GestureDetector mGestureDetector = null;

    public static int setIndexTextSize = 12;
    public static float mIndexbarWidth = 20;
    public static float mIndexbarMargin = 5;
    public static int mPreviewPadding = 5;
    public static int mIndexBarCornerRadius = 5;
    public static float mIndexBarTransparentValue = (float) 0.6;
    public static String mIndexbarBackgroudColor = "#000000";
    public static String mIndexbarTextColor = "#FFFFFF";

    public IndexFastScrollRecyclerView(Context context) {
        super(context);
    }

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

    public IndexFastScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        mScroller = new IndexFastScrollRecyclerSection(context, this);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndexFastScrollRecyclerView, 0, 0);

            if (typedArray != null) {
                try {
                    setIndexTextSize = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setIndexTextSize, setIndexTextSize);
                    mIndexbarWidth = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarWidth, mIndexbarWidth);
                    mIndexbarMargin = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexbarMargin, mIndexbarMargin);
                    mPreviewPadding = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setPreviewPadding, mPreviewPadding);
                    mIndexBarCornerRadius = typedArray.getInt(R.styleable.IndexFastScrollRecyclerView_setIndexBarCornerRadius, mIndexBarCornerRadius);
                    mIndexBarTransparentValue = typedArray.getFloat(R.styleable.IndexFastScrollRecyclerView_setIndexBarTransparentValue, mIndexBarTransparentValue);

                    if (typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor) != null) {
                        mIndexbarBackgroudColor = typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarColor);
                    }

                    if (typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor) != null) {
                        mIndexbarTextColor = typedArray.getString(R.styleable.IndexFastScrollRecyclerView_setIndexBarTextColor);
                    }

                } finally {
                    typedArray.recycle();
                }
            }
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        // Overlay index bar
        if (mScroller != null)
            mScroller.draw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Intercept ListView's touch event
        if (mScroller != null && mScroller.onTouchEvent(ev))
            return true;

        if (mGestureDetector == null) {
            mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2,
                                       float velocityX, float velocityY) {
                    return super.onFling(e1, e2, velocityX, velocityY);
                }

            });
        }
        mGestureDetector.onTouchEvent(ev);

        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mScroller.contains(ev.getX(), ev.getY()))
            return true;

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        if (mScroller != null)
            mScroller.setAdapter(adapter);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mScroller != null)
            mScroller.onSizeChanged(w, h, oldw, oldh);
    }

    /**
     * @param value int to set size of index text
     */
    public void setIndexTextSize(int value) {
        mScroller.setIndexTextSize(value);
    }

    /**
     * @param value float to set size of index text
     */
    public void setIndexbarWidth(float value) {
        mScroller.setIndexbarWidth(value);
    }

    /**
     * @param value float to set size of index text
     */
    public void setIndexbarMargin(float value) {
        mScroller.setIndexbarMargin(value);
    }


    /**
     * @param value int to set size of index text
     */
    public void setPreviewPadding(int value) {
        mScroller.setPreviewPadding(value);
    }

    /**
     * @param value int to set size of index text
     */
    public void setIndexBarCornerRadius(int value) {
        mScroller.setIndexBarCornerRadius(value);
    }

    /**
     * @param value float to set size of index text
     */
    public void setIndexBarTransparentValue(float value) {
        mScroller.setIndexBarTransparentValue(value);
    }


    /**
     * @param color The color for the scroll track
     */
    public void setIndexBarColor(String color) {
        mScroller.setIndexBarColor(color);
    }

    /**
     * @param color The color for the text in scroll track
     */
    public void setIndexBarTextColor(String color) {
        mScroller.setIndexBarTextColor(color);
    }

}
@@ -4752,32 +4752,41 @@
  int to set 
+the text 
 size of 
 index text%0A 
@@ -4769,34 +4769,37 @@
 ext size of 
+the 
 index 
-text
+bar
 %0A     */%0A   
@@ -4932,34 +4932,42 @@
  to set 
-size of
+the width of the
  index 
-text
+bar
 %0A     */
@@ -5106,34 +5106,43 @@
  to set 
-size of
+the margin of the
  index 
-text
+bar
 %0A     */
@@ -5234,33 +5234,32 @@
 (value);%0A    %7D%0A%0A
-%0A
     /**%0A     * @
@@ -5281,34 +5281,35 @@
  to set 
-size of index text
+the preview padding
 %0A     */
@@ -5446,34 +5446,50 @@
  to set 
-size of
+the corner radius of the
  index 
-text
+bar
 %0A     */
@@ -5644,26 +5644,47 @@
 set 
-siz
+the transparency valu
 e of 
+the 
 index 
-text
+bar
 %0A   
@@ -5804,25 +5804,24 @@
 ue);%0A    %7D%0A%0A
-%0A
     /**%0A    
@@ -5854,28 +5854,25 @@
 for the 
-scroll track
+index bar
 %0A     */
@@ -5995,32 +5995,37 @@
 param color The 
+text 
 color for the te
@@ -6026,28 +6026,17 @@
 the 
-text in scroll track
+index bar
 %0A   

fix java docs v2