package com.qimai.android.widgetlib.rvrelate;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * created by wangwei   ON 2019-12-03  email:wangwei_5521@163.com
 *
 * @version 1.1.1
 * @Description
 **/
public class GridItemDivider extends RecyclerView.ItemDecoration {

    // 多少列
    private Drawable mDivider;
    private final Rect mBounds = new Rect();

    private int mode = CLOSE;
    public final static int CLOSE = 1;
    public final static int OPEN = 2;

    public GridItemDivider(Drawable drawable) {
        this.mDivider = drawable;
    }

    public GridItemDivider(int dividerColor, int divideWidth) {
    }

    public void setMode(int mode) {
        this.mode = mode;
    }


    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (!(parent.getLayoutManager() instanceof GridLayoutManager)) {
            throw new IllegalStateException("请使用GridLayoutManager");
        }
        int mNumcolums = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        c.save();
        int childCount = parent.getChildCount();
        int closeChildCount = childCount;
        if (mode == CLOSE && childCount % mNumcolums != 0) {
            closeChildCount = childCount + (mNumcolums - childCount % mNumcolums);
        }
        View child = null;
        for (int i = 0; i < closeChildCount; i++) {
            if (i < childCount) {
                child = parent.getChildAt(i);
                if ((i + 1) % mNumcolums != 0) {
                    // 竖线
                    mDivider.setBounds(child.getRight(), child.getTop(), child.getRight() + mDivider.getIntrinsicWidth(), child.getBottom());
                    mDivider.draw(c);
                }
                // 横线
                mDivider.setBounds(child.getLeft(), child.getBottom(), child.getRight() + mDivider.getIntrinsicWidth(), child.getBottom() + mDivider.getIntrinsicWidth());
                mDivider.draw(c);
            }
            if (childCount != closeChildCount && i >= childCount && i < closeChildCount - 1) {
                int top = child.getTop();
                int left = child.getRight() * (i - childCount + 2) + mDivider.getIntrinsicWidth() * (i - childCount + 1);
                int right = (child.getRight() + mDivider.getIntrinsicWidth()) * (i - childCount + 2);
                int bottom = child.getBottom();
                mDivider.setBounds(
                        left, top, right, bottom
                );
                mDivider.draw(c);
            }
            if (childCount != closeChildCount && i >= childCount && i < closeChildCount) {
                int top = child.getBottom();
                int left = child.getRight() * (i - childCount + 1) + mDivider.getIntrinsicWidth() * (i - childCount + 1);
                int right = child.getRight() * (i - childCount + 2) + mDivider.getIntrinsicWidth() * (i - childCount + 2);
                int bottom = child.getBottom() + mDivider.getIntrinsicWidth();
                mDivider.setBounds(
                        left, top, right, bottom
                );
                mDivider.draw(c);
            }
        }
        c.restore();
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        int childPos = parent.getChildAdapterPosition(view);
        if ((childPos + 1) % ((GridLayoutManager) parent.getLayoutManager()).getSpanCount() == 0) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicHeight(), mDivider.getIntrinsicHeight());
        }
    }

}
