package net.cattaka.android.adaptertoolbox.decoration;

import android.content.Context;
import android.content.res.TypedArray;
import android.databinding.BindingAdapter;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import net.cattaka.android.adaptertoolbox.adapter.listener.ForwardingListener;

/**
 * Created by cattaka on 16/12/11.
 */
public class VerticalListDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDrawable;
    private boolean mIgnoreParentPadding = true;

    public VerticalListDividerItemDecoration(Context context, boolean ignoreParentPadding) {
        mIgnoreParentPadding = ignoreParentPadding;
        final TypedArray a = context.obtainStyledAttributes(new int[]{
                android.R.attr.listDivider
        });
        mDrawable = a.getDrawable(0);
        a.recycle();
    }

    public VerticalListDividerItemDecoration(Context context, boolean ignoreParentPadding, @DrawableRes int drawableRes) {
        mIgnoreParentPadding = ignoreParentPadding;
        mDrawable = ContextCompat.getDrawable(context, drawableRes);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mDrawable == null) {
            return;
        }
        int left = (mIgnoreParentPadding) ? 0 : parent.getPaddingLeft();
        int right = (mIgnoreParentPadding) ? 0 : (parent.getWidth() - parent.getPaddingRight());

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.ViewHolder nextHolder = ForwardingListener.findContainingViewHolder(parent, child);
            RecyclerView.ViewHolder prevHolder = findPrevViewHolder(parent, nextHolder);
            if (nextHolder != null && prevHolder != null && isAssignable(parent, prevHolder, nextHolder)) {
                final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
                final int bottom = child.getTop() - params.topMargin;
                final int top = bottom - mDrawable.getIntrinsicHeight();
                mDrawable.setBounds(left, top, right, bottom);
                mDrawable.draw(c);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.ViewHolder nextHolder = ForwardingListener.findContainingViewHolder(parent, view);
        RecyclerView.ViewHolder prevHolder = findPrevViewHolder(parent, nextHolder);
        if (mDrawable == null && nextHolder != null && prevHolder != null && isAssignable(parent, prevHolder, nextHolder)) {
            outRect.set(0, mDrawable.getIntrinsicHeight(), 0, 0);
        } else {
            outRect.set(0, 0, 0, 0);
        }
    }

    @Nullable
    private RecyclerView.ViewHolder findPrevViewHolder(@NonNull RecyclerView parent, @Nullable RecyclerView.ViewHolder nextHolder) {
        if (nextHolder == null) {
            return null;
        }
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter != null && nextHolder.getLayoutPosition() > 0) {
            View prevView = parent.getChildAt(nextHolder.getLayoutPosition() - 1);
            return ForwardingListener.findContainingViewHolder(parent, prevView);
        } else {
            return null;
        }
    }

    /**
     * Override this if needed.
     */
    public boolean isAssignable(@NonNull RecyclerView parent, @NonNull RecyclerView.ViewHolder prevHolder, @NonNull RecyclerView.ViewHolder nextHolder) {
        return true;
    }

    public Drawable getDrawable() {
        return mDrawable;
    }

    public void setDrawable(Drawable drawable) {
        mDrawable = drawable;
    }

    public boolean isIgnoreParentPadding() {
        return mIgnoreParentPadding;
    }

    public void setIgnoreParentPadding(boolean ignoreParentPadding) {
        mIgnoreParentPadding = ignoreParentPadding;
    }

    @BindingAdapter("verticalListDivider")
    public static void verticalListDivider(RecyclerView view, @DrawableRes int drawableRes) {
        view.addItemDecoration(new VerticalListDividerItemDecoration(view.getContext(), false, drawableRes));
    }

    @BindingAdapter("verticalListDividerIgnoreParentPadding")
    public static void verticalListDividerIgnoreParentPadding(RecyclerView view, @DrawableRes int drawableRes) {
        view.addItemDecoration(new VerticalListDividerItemDecoration(view.getContext(), true, drawableRes));
    }
}

@@ -2038,42 +2038,28 @@
     
-RecyclerView.ViewHolder prevHolder
+Integer prevViewType
  = f
@@ -2061,38 +2061,36 @@
 e = findPrevView
-Holder
+Type
 (parent, nextHol
@@ -2129,38 +2129,40 @@
  != null && prev
-Holder
+ViewType
  != null && isAs
@@ -2174,36 +2174,36 @@
 ble(parent, 
-prev
+next
 Holder, 
 nextHolder))
@@ -2186,34 +2186,36 @@
 nextHolder, 
-nextHolder
+prevViewType
 )) %7B%0A       
@@ -2830,42 +2830,28 @@
     
-RecyclerView.ViewHolder prevHolder
+Integer prevViewType
  = f
@@ -2857,30 +2857,28 @@
 findPrevView
-Holder
+Type
 (parent, nex
@@ -2905,25 +2905,25 @@
  (mDrawable 
-=
+!
 = null && ne
@@ -2946,22 +2946,24 @@
  && prev
-Holder
+ViewType
  != null
@@ -2987,28 +2987,28 @@
 parent, 
-prev
+next
 Holder, 
 nextHold
@@ -2999,26 +2999,28 @@
 Holder, 
-nextHolder
+prevViewType
 )) %7B%0A   
@@ -3179,37 +3179,21 @@
 private 
-RecyclerView.ViewHold
+Integ
 er findP
@@ -3199,22 +3199,20 @@
 PrevView
-Holder
+Type
 (@NonNul
@@ -3448,38 +3448,39 @@
 & nextHolder.get
-Layout
+Adapter
 Position() %3E 0) 
@@ -3497,41 +3497,38 @@
     
-View prevView = parent.getChildAt
+return adapter.getItemViewType
 (nex
@@ -3538,22 +3538,23 @@
 lder.get
-Layout
+Adapter
 Position
@@ -3566,90 +3566,8 @@
 1);%0A
-            return ForwardingListener.findContainingViewHolder(parent, prevView);%0A
     
@@ -3730,25 +3730,16 @@
  parent,
- @NonNull
  Recycle
@@ -3759,63 +3759,40 @@
 der 
-prevHolder, @NonNull RecyclerView.ViewHolder nextHolder
+nextViewHolder, int prevViewType
 ) %7B%0A

Fix failed to obtain ViewHolder problem