package com.github.vivchar.rendererrecyclerviewadapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
//import android.util.Log;
import android.view.View;

import java.io.Serializable;
import java.util.HashMap;

/**
 * Created by Vivchar Vitaly on 20.10.17.
 */
public class CompositeViewState <VH extends CompositeViewHolder> implements ViewState<VH>, Serializable {

	@NonNull
	protected HashMap<Integer, ViewState> mViewStates;
	protected int mPosition;
	protected int mTopOffset;
	protected int mLeftOffset;

	public <VH extends CompositeViewHolder> CompositeViewState(@NonNull final VH holder) {
		mViewStates = holder.getAdapter().getStates();
		final RecyclerView.LayoutManager layoutManager = holder.getRecyclerView().getLayoutManager();
		if (layoutManager instanceof LinearLayoutManager) {
			/* To get rid of Parcelable, https://stackoverflow.com/a/35287828/4894238 */
			mPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
			final View item = holder.getRecyclerView().getChildAt(0);
			mTopOffset = (item == null) ? 0 : (item.getTop() - holder.getRecyclerView().getPaddingTop());
			mLeftOffset = (item == null) ? 0 : (item.getLeft() - holder.getRecyclerView().getPaddingLeft());
//			Log.d("###", "save mPosition: " + mPosition + " mTopOffset: " + mTopOffset);
		} else {
			mPosition = 0;
			mTopOffset = 0;
		}
	}

	@Override
	public void restore(@NonNull final VH holder) {
		holder.getAdapter().setStates(mViewStates);

		final RecyclerView.LayoutManager layoutManager = holder.getRecyclerView().getLayoutManager();
		if (mPosition != -1 && layoutManager instanceof LinearLayoutManager) {
//			Log.d("###", "restore mPosition: " + mPosition + " mTopOffset: " + mTopOffset);
			((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(mPosition, Math.max(mTopOffset, mLeftOffset));
		}
	}
}
@@ -193,18 +193,16 @@
 erView;%0A
-//
 import a
@@ -218,16 +218,16 @@
 il.Log;%0A
+
 import a
@@ -613,41 +613,8 @@
 blic
- %3CVH extends CompositeViewHolder%3E
  Com
@@ -943,21 +943,65 @@
 %0A%09%09%09
-mPosition
+@NonNull final LinearLayoutManager linearLayoutManager
  = 
-(
 (Lin
@@ -1031,17 +1031,52 @@
 tManager
-)
+;%0A%09%09%09mPosition = linearLayoutManager
 .findFir
@@ -1358,18 +1358,16 @@
 eft());%0A
-//
 %09%09%09Log.d
@@ -1431,24 +1431,56 @@
 + mTopOffset
+ + %22 mLeftOffset %22 + mLeftOffset
 );%0A%09%09%7D else 
@@ -1802,18 +1802,113 @@
 ager) %7B%0A
-//
+%09%09%09@NonNull final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;%0A
 %09%09%09Log.d
@@ -1983,17 +1983,51 @@
 fset
+ + %22 mLeftOffset %22 + mLeftOffset
 );%0A%09%09%09
-((L
+if (l
 inea
@@ -2032,35 +2032,71 @@
 earLayoutManager
-) l
+.canScrollHorizontally()) %7B%0A%09%09%09%09linearL
 ayoutManager).sc
@@ -2091,17 +2091,16 @@
 tManager
-)
 .scrollT
@@ -2134,43 +2134,113 @@
 on, 
-Math.max(mTopOffset, mLeft
+mLeftOffset);%0A%09%09%09%7D else %7B%0A%09%09%09%09linearLayoutManager.scrollToPositionWithOffset(mPosition, mTop
 Offset)
-)
 ;
+%0A%09%09%09%7D
 %0A%09%09%7D

fix CompositeViewState