package com.futuremind.recyclerviewfastscroll;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Michal on 05/08/16.
 */
public abstract class ScrollerViewProvider {

    private FastScroller scroller;
    private VisibilityAnimationManager bubbleVisibilityManager;
    private VisibilityAnimationManager handleVisibilityManager;

    private boolean isGrabbing;
    private boolean isScrolling;

    void setFastScroller(FastScroller scroller){
        this.scroller = scroller;
    }

    protected Context getContext(){
        return scroller.getContext();
    }

    protected FastScroller getScroller() {
        return scroller;
    }

    /**
     * @param container The container {@link FastScroller} for the view to inflate properly.
     * @return A view which will be by the {@link FastScroller} used as a handle.
     */
    protected abstract View provideHandleView(ViewGroup container);

    /**
     * @param container The container {@link FastScroller} for the view to inflate properly.
     * @return A view which will be by the {@link FastScroller} used as a bubble.
     */
    protected abstract View provideBubbleView(ViewGroup container);

    /**
     * Bubble view has to provide a {@link TextView} that will show the index title.
     * @return A {@link TextView} that will hold the index title.
     */
    protected abstract TextView provideBubbleTextView();

    /**
     * To offset the position of the bubble relative to the handle. E.g. in {@link DefaultScrollerViewProvider}
     * the sharp corner of the bubble is aligned with the center of the handle.
     * @return the position of the bubble in relation to the handle (according to the orientation).
     */
    protected abstract int getBubbleOffset();

    /**
     * @return {@link VisibilityAnimationManager} responsible for showing and hiding bubble.
     */
    protected abstract VisibilityAnimationManager provideBubbleVisibilityManager();

    /**
     * @return {@link VisibilityAnimationManager} responsible for showing and hiding handle.
     */
    protected abstract VisibilityAnimationManager provideHandleVisibilityManager();

    private VisibilityAnimationManager getBubbleVisibilityManager(){
        if(bubbleVisibilityManager==null) bubbleVisibilityManager = provideBubbleVisibilityManager();
        return bubbleVisibilityManager;
    }

    private VisibilityAnimationManager getHandleVisibilityManager(){
        if(handleVisibilityManager==null) handleVisibilityManager = provideHandleVisibilityManager();
        return handleVisibilityManager;
    }

    protected void handleGrabbed(){
        isGrabbing = true;
        if(getBubbleVisibilityManager()!=null) getBubbleVisibilityManager().show();
    }

    protected void handleReleased(){
        isGrabbing = false;
        if(getBubbleVisibilityManager()!=null) getBubbleVisibilityManager().hide();
        if(getHandleVisibilityManager()!=null && !isScrolling) getHandleVisibilityManager().hide();
    }

    protected void onScrollStarted(){
        isScrolling = true;
        if(getHandleVisibilityManager()!=null) getHandleVisibilityManager().show();
    }

    protected void onScrollFinished(){
        isScrolling = false;
        if(getHandleVisibilityManager()!=null && !isGrabbing) getHandleVisibilityManager().hide();
    }

}

@@ -444,74 +444,8 @@
 r;%0A%0A
-    private boolean isGrabbing;%0A    private boolean isScrolling;%0A%0A
     
@@ -2650,35 +2650,8 @@
 ()%7B%0A
-        isGrabbing = true;%0A
     
@@ -2722,32 +2722,116 @@
 nager().show();%0A
+        if(getHandleVisibilityManager()!=null) getHandleVisibilityManager().show();%0A
     %7D%0A%0A    prote
@@ -2862,36 +2862,8 @@
 ()%7B%0A
-        isGrabbing = false;%0A
     
@@ -2991,24 +2991,8 @@
 null
- && !isScrolling
 ) ge
@@ -3075,36 +3075,8 @@
 ()%7B%0A
-        isScrolling = true;%0A
     
@@ -3205,37 +3205,8 @@
 ()%7B%0A
-        isScrolling = false;%0A
     
@@ -3250,23 +3250,8 @@
 null
- && !isGrabbing
 ) ge

fix showing and hiding handle