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.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.core.utils.DisplayUtils;

public class VerticalItemDecoration extends RecyclerView.ItemDecoration {

    private Paint paint;
    private int mInterval;
    private int mIntervalLR;
    private boolean mIsShowIntervalTop;

    public VerticalItemDecoration(Context context, float interval, boolean isShowIntervalTop, int colorRes) {
        paint = new Paint();
        if (colorRes != 0) {
            paint.setColor(ContextCompat.getColor(context, colorRes));
        } else {
            paint.setColor(ContextCompat.getColor(context, R.color.background));
        }
        mInterval = DisplayUtils.dip2px(context, interval);
        mIsShowIntervalTop = isShowIntervalTop;
    }

    public VerticalItemDecoration(Context context, float interval, boolean isShowIntervalTop) {
        this(context, interval, isShowIntervalTop, 0);
    }

    public VerticalItemDecoration(Context context, int interval, boolean isShowIntervalTop, int intervalLR) {
        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, R.color.background));
        mInterval = DisplayUtils.dip2px(context, interval);
        mIsShowIntervalTop = isShowIntervalTop;
        mIntervalLR = DisplayUtils.dip2px(context, intervalLR);
    }

    // cutting
    public VerticalItemDecoration(Context context, boolean isShowIntervalTop) {
        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, R.color.bg_ededed));
        mInterval = (int) context.getResources().getDimension(R.dimen.cutting_line);
        mIsShowIntervalTop = isShowIntervalTop;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        for (int i = 0, size = parent.getChildCount(); i < size; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            int top = child.getBottom() + layoutParams.bottomMargin;
            int bottom = top + mInterval;

            if (i == 0 && mIsShowIntervalTop) {
                c.drawRect(left + mIntervalLR, 0, right - mIntervalLR, mInterval, paint);
            } else {
                c.drawRect(left + mIntervalLR, top, right - mIntervalLR, bottom, paint);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        int position = parent.getChildPosition(view);
        if (position == 0 && mIsShowIntervalTop) {
            outRect.set(0, mInterval, 0, mInterval);
        } else {
            outRect.set(0, 0, 0, mInterval);
        }
    }
}
