package com.ux.video.file.filerecovery.utils

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.ux.video.file.filerecovery.App
import com.ux.video.file.filerecovery.utils.ExtendFunctions.dpToPx


/**
 * @param spanCount   列数
 * @param spacing     列与列之间的间距（dp）
 * @param edgeSpacing RecyclerView 左右两边的边距（dp）
 */
class GridSpacingItemDecoration(
    private val spanCount: Int,
    private val itemSpacing: Int,
    private val edgeSpacing: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val column = position % spanCount

        val itemSpacingPx = itemSpacing.dpToPx(App.mAppContext)
        val edgePx = edgeSpacing.dpToPx(App.mAppContext)

        when (column) {
            0 -> { // 第一列
                outRect.left = edgePx
                outRect.right = itemSpacingPx / 2
            }
            spanCount - 1 -> { // 最后一列
                outRect.left = itemSpacingPx / 2
                outRect.right = edgePx
            }
            else -> { // 中间列
                outRect.left = itemSpacingPx / 2
                outRect.right = itemSpacingPx / 2
            }
        }

        // 顶部间距（第一行也加）
        if (position < spanCount) {
            outRect.top = itemSpacingPx
        }
        // 底部间距
        outRect.bottom = itemSpacingPx
    }
}


