/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.common.util.recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;
    private boolean mShowFirstDivider = true;
    private boolean mShowLastDivider  = true;

    public DividerItemDecoration(Context context, int resId) {

        mDivider = ContextCompat.getDrawable(context, resId);
    }
    public DividerItemDecoration(Context context, int resId, boolean showFirstDivider, boolean showLastDivider) {

        mDivider = ContextCompat.getDrawable(context, resId);
        mShowFirstDivider = showFirstDivider;
        mShowLastDivider = showLastDivider;
    }

    @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //가로
        drawHorizontal(c, parent);
        //세로
        drawVertical(c, parent);
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {

        int childCount = parent.getChildCount();
        for ( int i = 0; i < childCount; i++ ) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            //            Dlog.v("left=" + left + " top=" + top + " right=" + right + " bottom=" + bottom);
            if ( i == childCount - 1 && !mShowLastDivider ) {
                //마지막은 없음
            } else {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {

        final int childCount = parent.getChildCount();
        for ( int i = 0; i < childCount; i++ ) {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();
            //            Dlog.v("left=" + left + " top=" + top + " right=" + right + " bottom=" + bottom);

            if ( i == childCount - 1 && !mShowLastDivider ) {
                //마지막은 없음
            } else {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if ( layoutManager instanceof GridLayoutManager ) {
            if ( (pos + 1) % spanCount == 0 ) {
                return true;
            }
        } else if ( layoutManager instanceof StaggeredGridLayoutManager ) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if ( orientation == StaggeredGridLayoutManager.VERTICAL ) {
                if ( (pos + 1) % spanCount == 0 ) {
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if ( pos >= childCount )
                    return true;
            }
        }
        return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if ( layoutManager instanceof GridLayoutManager ) {
            childCount = childCount - childCount % spanCount;
            if ( pos >= childCount )
                return true;
        } else if ( layoutManager instanceof StaggeredGridLayoutManager ) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if ( orientation == StaggeredGridLayoutManager.VERTICAL ) {
                childCount = childCount - childCount % spanCount;
                if ( pos >= childCount )
                    return true;
            } else {
                if ( (pos + 1) % spanCount == 0 ) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {

        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        if ( isLastRaw(parent, itemPosition, spanCount, childCount) ) {
            if ( mShowLastDivider ) {
                outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
            }
        } else if ( isLastColum(parent, itemPosition, spanCount, childCount) ) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
        }
    }

    private int getSpanCount(RecyclerView parent) {

        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if ( layoutManager instanceof GridLayoutManager ) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if ( layoutManager instanceof StaggeredGridLayoutManager ) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }
}