package nammari.network.ui.widget;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.ViewUtils;
import android.view.View;

/**
 * Created by nammari on 1/21/16.
 */
public class ItemDecorationAlbumColumns extends RecyclerView.ItemDecoration {

    private int mSizeGridSpacingPx;
    private int mGridSize;

    private boolean mNeedLeftSpacing = false;

    public ItemDecorationAlbumColumns(int gridSpacingPx, int gridSize) {
        mSizeGridSpacingPx = gridSpacingPx;
        mGridSize = gridSize;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        boolean isRtl = ViewUtils.isLayoutRtl(view);
        int frameWidth = (int) ((parent.getWidth() - (float) mSizeGridSpacingPx * (mGridSize - 1)) / mGridSize);
        int padding = parent.getWidth() / mGridSize - frameWidth;
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
        if (itemPosition < mGridSize) {
            outRect.top = 0;
        } else {
            outRect.top = mSizeGridSpacingPx;
        }
        if (itemPosition % mGridSize == 0) {
            outRect.left = isRtl ? padding : 0;
            outRect.right = isRtl ? 0 : padding;
            mNeedLeftSpacing = true;
        } else if ((itemPosition + 1) % mGridSize == 0) {
            mNeedLeftSpacing = false;
            outRect.right = isRtl ? padding : 0;
            outRect.left = isRtl ? 0 : padding;
        } else if (mNeedLeftSpacing) {
            mNeedLeftSpacing = false;
            if (isRtl) {
                outRect.right = mSizeGridSpacingPx - padding;
                if ((itemPosition + 2) % mGridSize == 0) {
                    outRect.left = mSizeGridSpacingPx - padding;
                } else {
                    outRect.left = mSizeGridSpacingPx / 2;
                }
            } else {
                outRect.left = mSizeGridSpacingPx - padding;
                if ((itemPosition + 2) % mGridSize == 0) {
                    outRect.right = mSizeGridSpacingPx - padding;
                } else {
                    outRect.right = mSizeGridSpacingPx / 2;
                }
            }
        } else if ((itemPosition + 2) % mGridSize == 0) {
            mNeedLeftSpacing = false;
            outRect.left = isRtl ? mSizeGridSpacingPx - padding : mSizeGridSpacingPx / 2;
            outRect.right = isRtl ? mSizeGridSpacingPx / 2 : mSizeGridSpacingPx - padding;
        } else {
            mNeedLeftSpacing = false;
            outRect.left = mSizeGridSpacingPx / 2;
            outRect.right = mSizeGridSpacingPx / 2;
        }
        outRect.bottom = 0;
    }
}

@@ -1339,12 +1339,14 @@
 g = 
-true
+!isRtl
 ;%0A  
@@ -1424,37 +1424,37 @@
 edLeftSpacing = 
-false
+isRtl
 ;%0A            ou
@@ -1598,37 +1598,37 @@
 edLeftSpacing = 
-false
+isRtl
 ;%0A            if
@@ -2331,37 +2331,37 @@
 edLeftSpacing = 
-false
+isRtl
 ;%0A            ou
@@ -2575,21 +2575,21 @@
 acing = 
-false
+isRtl
 ;%0A      

fix rtl