package com.gh.gamecenter.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class GridSpacingItemColorDecoration extends RecyclerView.ItemDecoration {

    private int dividerWidth;
    private int dividerHeight;
    private int dividerWidthTop;
    private int dividerWidthBot;
    private Paint dividerPaint;

    /**
     * @param dividerDp    分割块宽高,单位:dp
     * @param dividerColor 分割块颜色
     */
    public GridSpacingItemColorDecoration(Context context, int dividerDp, @ColorRes int dividerColor) {
        this(context, dividerDp, dividerDp, dividerColor);
    }

    /**
     * @param dividerWidthDp  分割块宽,单位:dp
     * @param dividerHeightDp 分割块高,单位:dp
     * @param dividerColor    分割块颜色
     */
    public GridSpacingItemColorDecoration(Context context, int dividerWidthDp, int dividerHeightDp, @ColorRes int dividerColor) {
        this.dividerPaint = new Paint();
        this.dividerPaint.setColor(ContextCompat.getColor(context, dividerColor));

        this.dividerWidth = dip2px(context, dividerWidthDp);
        this.dividerHeight = dip2px(context, dividerHeightDp);
        this.dividerWidthTop = dividerHeight / 2;
        this.dividerWidthBot = dividerHeight - dividerWidthTop;
    }

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

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (!isLastRaw(parent, i, getSpanCount(parent), childCount)) {//最后一行不设置颜色
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                        .getLayoutParams();
                final int left = child.getLeft() - params.leftMargin;
                final int right = !isLastColum(parent, i, getSpanCount(parent)) ? child.getRight() + params.rightMargin
                        + dividerWidth : child.getRight() + params.rightMargin;
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + dividerHeight;
                c.drawRect(left, top, right, bottom, dividerPaint);
            }
        }
    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (!isLastColum(parent, i, getSpanCount(parent)) && i != childCount - 1) {//最后一列和最后个item后面不设置颜色
                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 + dividerWidth;
                c.drawRect(left, top, right, bottom, dividerPaint);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View child, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, child, parent, state);
        int spanCount = getSpanCount(parent);
        int pos = parent.getChildAdapterPosition(child);
        int childCount = parent.getAdapter().getItemCount();
        int column = (pos) % spanCount;// 计算这个child 处于第几列

        outRect.top = isFirstRaw(parent, pos, spanCount) ? 0 : dividerWidthTop;
        outRect.bottom = isLastRaw(parent, pos, spanCount, childCount) ? 0 : dividerWidthBot;

        outRect.left = (column * dividerWidth / spanCount);
        outRect.right = dividerWidth - (column + 1) * dividerWidth / spanCount;

    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        return layoutManager instanceof GridLayoutManager && (pos + 1) % spanCount == 0;
    }

    private boolean isFirstRaw(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        return layoutManager instanceof GridLayoutManager && pos < spanCount;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                              int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            int rawChildCount = childCount - childCount % spanCount;
            if (rawChildCount == childCount) {
                return pos >= rawChildCount - spanCount;
            }
            return pos >= rawChildCount;
        }
        return false;
    }

    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;
    }

    private int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
