package com.gh.gamecenter.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by khy on 2017/3/28.
 */
public class GridDivider extends RecyclerView.ItemDecoration {

    private int mSpanCount;
    private int mDividerStrokeWidth = 1;

    private boolean mIsFilterLast;

    private Paint mColorPaint;
    private Drawable mDividerDrawable;

    /**
     * @param dividerStrokeWidth 分割线的线宽
     * @param dividerColor       分割线的颜色
     */
    public GridDivider(Context context, int dividerStrokeWidth, int spanCount, int dividerColor) {
        this(context);
        mDividerStrokeWidth = dividerStrokeWidth;
        mSpanCount = spanCount;
        mColorPaint = new Paint();
        mColorPaint.setColor(dividerColor);
    }

    public GridDivider(Context context, int dividerStrokeWidth, int spanCount, int dividerColor, boolean isFilterLast) {
        this(context);
        mDividerStrokeWidth = dividerStrokeWidth;
        mSpanCount = spanCount;
        mColorPaint = new Paint();
        mColorPaint.setColor(dividerColor);
        mIsFilterLast = isFilterLast;
    }

    public GridDivider(Context context) {
        int[] attrs = new int[]{android.R.attr.listDivider};
        final TypedArray ta = context.obtainStyledAttributes(attrs);
        this.mDividerDrawable = ta.getDrawable(0);
        ta.recycle();
    }

    /**
     * @param dividerStrokeWidth 分割线的线宽
     * @param dividerDrawable    图片分割线
     */
    public GridDivider(Context context, int dividerStrokeWidth, Drawable dividerDrawable) {
        this(context);
        mDividerStrokeWidth = dividerStrokeWidth;
        mDividerDrawable = dividerDrawable;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        //画水平和垂直分割线
        drawHorizontalDivider(c, parent);
        drawVerticalDivider(c, parent);
    }

    private void drawHorizontalDivider(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (mIsFilterLast && i == childCount - 1) continue;
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            final int left = child.getLeft() - params.leftMargin - mDividerStrokeWidth;
            final int right = child.getRight() + params.rightMargin;
            int top = 0;
            int bottom = 0;

            // 最上面一行
            if ((i / mSpanCount) == 0) {
                //当前item最上面的分割线
                top = child.getTop();
                //当前item下面的分割线
                bottom = top + mDividerStrokeWidth;
                mDividerDrawable.setBounds(left, top, right, bottom);
                mDividerDrawable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }
                top = child.getBottom() + params.bottomMargin;
                bottom = top + mDividerStrokeWidth;
            } else {
                top = child.getBottom() + params.bottomMargin;
                bottom = top + mDividerStrokeWidth;
            }
            //画分割线
            mDividerDrawable.setBounds(left, top, right, bottom);
            mDividerDrawable.draw(c);
            if (mColorPaint != null) {
                c.drawRect(left, top, right, bottom, mColorPaint);
            }
        }
    }

    private void drawVerticalDivider(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;

            int left = 0;
            int right = 0;

            //左边第一列
            if ((i % mSpanCount) == 0) {
                //item左边分割线
                left = child.getLeft();
                right = left + mDividerStrokeWidth;
                mDividerDrawable.setBounds(left, top, right, bottom);
                mDividerDrawable.draw(c);
                if (mColorPaint != null) {
                    c.drawRect(left, top, right, bottom, mColorPaint);
                }
                //item右边分割线
                left = child.getRight() + params.rightMargin - mDividerStrokeWidth;
                right = left + mDividerStrokeWidth;
            } else {
                //非左边第一列
                left = child.getRight() + params.rightMargin - mDividerStrokeWidth;
                right = left + mDividerStrokeWidth;
            }
            //画分割线
            mDividerDrawable.setBounds(left, top, right, bottom);
            mDividerDrawable.draw(c);
            if (mColorPaint != null) {
                c.drawRect(left, top, right, bottom, mColorPaint);
            }

        }
    }
}
