package com.rich.keyboard.skin.utils;


import android.graphics.Rect;
import android.view.View;

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

public class MyItemDecoration extends RecyclerView.ItemDecoration {

    private final int horizontalSpace; // item之间的水平间距
    private final int verticalSpace;   // item之间的垂直间距
    private final int edgeSpace;       // 距离 RecyclerView 边缘的间距

    public MyItemDecoration(int horizontalSpace, int verticalSpace, int edgeSpace) {
        this.horizontalSpace = horizontalSpace;
        this.verticalSpace = verticalSpace;
        this.edgeSpace = edgeSpace;
    }

    @Override
    public void getItemOffsets(
            @NonNull Rect outRect,
            @NonNull View view,
            @NonNull RecyclerView parent,
            @NonNull RecyclerView.State state
    ) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        int position = parent.getChildAdapterPosition(view);
        int itemCount = state.getItemCount();

        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager grid = (GridLayoutManager) layoutManager;
            int spanCount = grid.getSpanCount();
            int column = position % spanCount;

            // 列间距 (左右均分)
            outRect.left = horizontalSpace * (spanCount - column) / spanCount;
            outRect.right = horizontalSpace * (column + 1) / spanCount;

            // 上边距（首行用 edgeSpace）
            if (position < spanCount) {
                outRect.top = edgeSpace;
            } else {
                outRect.top = verticalSpace;
            }

            // 下边距
            outRect.bottom = position >= itemCount - spanCount ? edgeSpace : 0;

        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager stagger = (StaggeredGridLayoutManager) layoutManager;
            int spanCount = stagger.getSpanCount();
            int column = position % spanCount;

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

            outRect.top = position < spanCount ? edgeSpace : verticalSpace;
            outRect.bottom = edgeSpace;

        } else if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linear = (LinearLayoutManager) layoutManager;
            if (linear.getOrientation() == RecyclerView.VERTICAL) {
                outRect.left = edgeSpace;
                outRect.right = edgeSpace;

                outRect.top = position == 0 ? edgeSpace : verticalSpace;
                outRect.bottom = position == itemCount - 1 ? edgeSpace : 0;
            } else {
                outRect.top = edgeSpace;
                outRect.bottom = edgeSpace;

                outRect.left = position == 0 ? edgeSpace : horizontalSpace;
                outRect.right = position == itemCount - 1 ? edgeSpace : 0;
            }
        }
    }
}
