package com.mugen.attachers;

import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import static android.support.v7.widget.RecyclerView.NO_POSITION;

/**
 * ${CLASS_NAME} com.mugen.attachers.
 */
class RecyclerViewPositionHelper {

    final RecyclerView recyclerView;
    final RecyclerView.LayoutManager layoutManager;

    RecyclerViewPositionHelper(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        this.layoutManager = recyclerView.getLayoutManager();
    }

    public static RecyclerViewPositionHelper createHelper(RecyclerView recyclerView) {
        if (recyclerView == null) {
            throw new NullPointerException("Recycler View is null");
        }
        return new RecyclerViewPositionHelper(recyclerView);
    }

    public int getItemCount() {
        return layoutManager == null ? 0 : layoutManager.getItemCount();
    }

    public int findFirstVisibleItemPosition() {
        final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true);
        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    public int findFirstCompletelyVisibleItemPosition() {
        final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false);
        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    public int findLastVisibleItemPosition() {
        final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);
        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    public int findLastCompletelyVisibleItemPosition() {
        final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, true, false);
        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
                             boolean acceptPartiallyVisible) {
        OrientationHelper helper;
        if (layoutManager.canScrollVertically()) {
            helper = OrientationHelper.createVerticalHelper(layoutManager);
        } else {
            helper = OrientationHelper.createHorizontalHelper(layoutManager);
        }

        final int start = helper.getStartAfterPadding();
        final int end = helper.getEndAfterPadding();
        final int next = toIndex > fromIndex ? 1 : -1;
        View partiallyVisible = null;
        for (int i = fromIndex; i != toIndex; i += next) {
            final View child = layoutManager.getChildAt(i);
            final int childStart = helper.getDecoratedStart(child);
            final int childEnd = helper.getDecoratedEnd(child);
            if (childStart < end && childEnd > start) {
                if (completelyVisible) {
                    if (childStart >= start && childEnd <= end) {
                        return child;
                    } else if (acceptPartiallyVisible && partiallyVisible == null) {
                        partiallyVisible = child;
                    }
                } else {
                    return child;
                }
            }
        }
        return partiallyVisible;
    }
}

@@ -837,849 +837,2307 @@
     
-public int getItemCount() %7B%0A        return layoutManager == null ? 0 : layoutManager.getItemCount();%0A    %7D%0A%0A    public int findFirstVisibleItemPosition() %7B%0A        final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true);%0A        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A%0A    public int findFirstCompletelyVisibleItemPosition() %7B%0A        final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false);%0A        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A%0A    public int findLastVisibleItemPosition() %7B%0A        final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);%0A        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A
+/**%0A     * Returns the adapter item count.%0A     *%0A     * @return The total number on items in a layout manager%0A     */%0A    public int getItemCount() %7B%0A        return layoutManager == null ? 0 : layoutManager.getItemCount();%0A    %7D%0A%0A    /**%0A     * Returns the adapter position of the first visible view. This position does not include%0A     * adapter changes that were dispatched after the last layout pass.%0A     *%0A     * @return The adapter position of the first visible item or %7B@link RecyclerView#NO_POSITION%7D if%0A     * there aren't any visible items.%0A     */%0A    public int findFirstVisibleItemPosition() %7B%0A        final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true);%0A        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A%0A    /**%0A     * Returns the adapter position of the first fully visible view. This position does not include%0A     * adapter changes that were dispatched after the last layout pass.%0A     *%0A     * @return The adapter position of the first fully visible item or%0A     * %7B@link RecyclerView#NO_POSITION%7D if there aren't any visible items.%0A     */%0A    public int findFirstCompletelyVisibleItemPosition() %7B%0A        final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false);%0A        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A%0A    /**%0A     * Returns the adapter position of the last visible view. This position does not include%0A     * adapter changes that were dispatched after the last layout pass.%0A     *%0A     * @return The adapter position of the last visible view or %7B@link RecyclerView#NO_POSITION%7D if%0A     * there aren't any visible items%0A     */%0A    public int findLastVisibleItemPosition() %7B%0A        final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);%0A        return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);%0A    %7D%0A%0A    /**%0A     * Returns the adapter position of the last fully visible view. This position does not include%0A     * adapter changes that were dispatched after the last layout pass.%0A     *%0A     * @return The adapter position of the last fully visible view or%0A     * %7B@link RecyclerView#NO_POSITION%7D if there aren't any visible items.%0A     */
 %0A   

add docs