package com.me.ui.widget.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

/**
 * @author studiotang on 17/3/29
 */
public class LinearItemDecoration extends RecyclerView.ItemDecoration {

    private static final int DEFAULT_DIVIDER_SIZE = 1;

    private int mOrientation = LinearLayoutManager.VERTICAL;

    private int mDividerSize;

    private Paint mPaint;

    public LinearItemDecoration(Context context) {
        mDividerSize = (int) TypedValue.applyDimension(DEFAULT_DIVIDER_SIZE,
                TypedValue.COMPLEX_UNIT_DIP, context.getResources().getDisplayMetrics());
    }

    public LinearItemDecoration setOrientation(int orientation) {
        this.mOrientation = orientation;
        if (orientation != LinearLayoutManager.VERTICAL
                && orientation != LinearLayoutManager.HORIZONTAL) {
            throw new IllegalArgumentException("Invalid params orientation!");
        }
        return this;
    }

    public LinearItemDecoration setDividerSize(int dividerSize) {
        mDividerSize = dividerSize;
        return this;
    }

    public LinearItemDecoration setColor(int color) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
        return this;
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mPaint == null) {
            return;
        }

        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDividerSize;
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDividerSize;
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            outRect.set(0, 0, 0, mDividerSize);
        } else {
            outRect.set(0, 0, mDividerSize, 0);
        }
    }
}

@@ -156,16 +156,68 @@
 s.Rect;%0A
+import android.support.v7.widget.GridLayoutManager;%0A
 import a
@@ -254,32 +254,32 @@
 rLayoutManager;%0A
-
 import android.s
@@ -2187,32 +2187,125 @@
 ildSize; i++) %7B%0A
+            if (isLastRaw(parent, i, 1, childSize)) %7B%0A                return;%0A            %7D%0A%0A
             fina
@@ -2934,32 +2934,140 @@
 ildSize; i++) %7B%0A
+            if (isLastColumn(parent, i, 1, childSize)) %7B%0A                return;%0A            %7D%0A            %0A
             fina
@@ -3694,16 +3694,16 @@
 ze, 0);%0A
-
         
@@ -3709,11 +3709,949 @@
  %7D%0A    %7D
+%0A%0A    // %E6%98%AF%E5%90%A6%E6%98%AF%E6%9C%80%E5%90%8E%E4%B8%80%E5%88%97%0A    private boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) %7B%0A        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();%0A        if (layoutManager instanceof GridLayoutManager) %7B%0A            if ((pos + 1) %25 spanCount == 0)%0A                return true;%0A        %7D else %7B%0A            if (pos == childCount - 1)%0A                return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    // %E6%98%AF%E5%90%A6%E6%98%AF%E6%9C%80%E5%90%8E%E4%B8%80%E8%A1%8C%0A    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) %7B%0A        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();%0A        if (layoutManager instanceof GridLayoutManager) %7B%0A            childCount = childCount - childCount %25 spanCount;%0A            if (pos %3E= childCount)%0A                return true;%0A        %7D else %7B%0A            if (pos == childCount - 1)%0A                return true;%0A        %7D%0A        return false;%0A    %7D
 %0A%7D%0A

Update linear item decoration