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

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

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

/**
 * Default android divider
 * mCategoryRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), null));
 * Custom divider as drawable (deprecated) with first divider (before first element) and last divider (after last element)
 * mCategoryRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity().getDrawable(R.drawable.ic_launcher), true, true));
 * Custom divider as resource id
 * mCategoryRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), R.drawable.ic_launcher, true, true));
 * Space divider
 * mCategoryRecyclerView.addItemDecoration(new SpaceItemDecoration(getActivity(), R.dimen.list_space, true, true));
 */
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int mSpace;
    private boolean mShowFirstDivider = false;
    private boolean mShowLastDivider  = false;

    int mOrientation = -1;

    public SpaceItemDecoration(Context context, AttributeSet attrs) {

        mSpace = 0;
    }

    public SpaceItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider, boolean showLastDivider) {

        this(context, attrs);
        mShowFirstDivider = showFirstDivider;
        mShowLastDivider = showLastDivider;
    }

    public SpaceItemDecoration(int spaceInPx) {

        mSpace = spaceInPx;
    }

    public SpaceItemDecoration(int spaceInPx, boolean showFirstDivider, boolean showLastDivider) {

        this(spaceInPx);
        mShowFirstDivider = showFirstDivider;
        mShowLastDivider = showLastDivider;
    }

    public SpaceItemDecoration(Context ctx, int resId) {

        mSpace = ctx.getResources().getDimensionPixelSize(resId);
    }

    public SpaceItemDecoration(Context ctx, int resId, boolean showFirstDivider, boolean showLastDivider) {

        this(ctx, resId);
        mShowFirstDivider = showFirstDivider;
        mShowLastDivider = showLastDivider;
    }

    @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        if ( mSpace == 0 ) {
            return;
        }

        if ( mOrientation == -1 )
            getOrientation(parent);

        int position = parent.getChildAdapterPosition(view);
        if ( position == RecyclerView.NO_POSITION || (position == 0 && !mShowFirstDivider) ) {
            return;
        }

        if ( mOrientation == LinearLayoutManager.VERTICAL ) {
            outRect.top = mSpace;
            if ( mShowLastDivider && position == (state.getItemCount() - 1) ) {
                outRect.bottom = outRect.top;
            }
        } else {
            outRect.left = mSpace;
            if ( mShowLastDivider && position == (state.getItemCount() - 1) ) {
                outRect.right = outRect.left;
            }
        }
    }

    private int getOrientation(RecyclerView parent) {

        if ( mOrientation == -1 ) {
            if ( parent.getLayoutManager() instanceof LinearLayoutManager ) {
                LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
                mOrientation = layoutManager.getOrientation();
            } else {
                throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager.");
            }
        }
        return mOrientation;
    }
}