/*
 * Copyright 2016 Philip Cohn-Cort
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fuzz.emptyhusk.prefab;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;

import com.fuzz.indicator.CutoutViewIndicator;

/**
 * {@link RecyclerView} analog to {@link com.fuzz.indicator.OnViewPagerChangeListener}.
 *
 * @author Philip Cohn-Cort (Fuzz)
 */
class CVIScrollListener extends RecyclerView.OnScrollListener {
    private CutoutViewIndicator cvi;

    protected int cumulativeScrollDx;
    protected int cumulativeScrollDy;

    CVIScrollListener(CutoutViewIndicator cvi, int initialDx, int initialDy) {
        this.cvi = cvi;
        cumulativeScrollDx = initialDx;
        cumulativeScrollDy = initialDy;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            cvi.ensureOnlyCurrentItemsSelected();
        }
    }

    public void centerIndicatorAround(float position) {
        int integralPos = (int) position;
        cvi.showOffsetIndicator(cvi.fixPosition(integralPos), position - integralPos);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        cumulativeScrollDx += dx;
        cumulativeScrollDy += dy;
        if (cvi.getIndicatorDrawableId() != 0) {

            LinearLayoutManager layout = (LinearLayoutManager) recyclerView.getLayoutManager();
            int rvFirst = layout.findFirstVisibleItemPosition();
            int rvLast = layout.findLastVisibleItemPosition();

            if (rvFirst > rvLast) {
                int tmp = rvFirst;
                rvFirst = rvLast;
                rvLast = tmp;
            }

            for (int rvPosition = rvFirst; rvPosition <= rvLast; rvPosition++) {
                // This ViewHolder's associated cell will be at least partially covered by indicator
                ViewHolder holder = recyclerView.findViewHolderForLayoutPosition(rvPosition);

                final int cviPosition = cvi.fixPosition(rvPosition);

                /**
                 * If there's no rvFirst view, then just go to the next one.
                 */
                if (holder != null) {
                    View child = holder.itemView;

                    // rvStart is relative to the screen (so like -17 for just off the top/start side)
                    float rvStart;
                    int childLength;

                    if (layout.getOrientation() == LinearLayoutManager.VERTICAL) {
                        rvStart = child.getY();
                        childLength = child.getHeight();
                    } else {
                        rvStart = child.getX();
                        childLength = child.getWidth();
                    }

                    float cviPositionOffset = -(rvStart / childLength);

                    // Cover the provided position...
                    cvi.showOffsetIndicator(cviPosition, cviPositionOffset);
                }
            }
        }
    }
}

@@ -630,16 +630,59 @@
 refab;%0A%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -1105,16 +1105,29 @@
 tener %7B%0A
+    @NonNull%0A
     priv
@@ -1255,16 +1255,25 @@
 istener(
+@NonNull 
 CutoutVi
@@ -2703,78 +2703,8 @@
 );%0A%0A
-                final int cviPosition = cvi.fixPosition(rvPosition);%0A%0A
     
@@ -2754,21 +2754,24 @@
 's no rv
-First
+Position
  view, t
@@ -2853,24 +2853,300 @@
  != null) %7B%0A
+                    showOffsetFor(holder, layout.getOrientation());%0A                 %7D%0A             %7D%0A        %7D%0A    %7D%0A%0A    public void showOffsetFor(@NonNull ViewHolder holder, int rvOrientation) %7B%0A        final int cviPosition = cvi.fixPosition(holder.getLayoutPosition());%0A%0A
             
@@ -3384,26 +3384,18 @@
     if (
-layout.get
+rv
 Orientat
@@ -3397,18 +3397,16 @@
 entation
-()
  == Line
@@ -3902,50 +3902,8 @@
 t);%0A
-                %7D%0A            %7D%0A        %7D%0A
     

Move ViewHolder-analysing code into a separate method