package com.gallery.free.wallpaper

import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class StaggeredGridSpacingItemDecoration(
    private val spanCount: Int,
    private val spacing: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val layoutParams = view.layoutParams as? StaggeredGridLayoutManager.LayoutParams
        val spanIndex = layoutParams?.spanIndex ?: 0

        // 根据列数动态计算间距
        when (spanCount) {
            2 -> {
                // 两列布局
                when (spanIndex) {
                    0 -> {
                        // 第一列：左边间距大，右边间距小
                        outRect.left = spacing
                        outRect.right = spacing / 2
                    }
                    1 -> {
                        // 第二列：左边间距小，右边间距大
                        outRect.left = spacing / 2
                        outRect.right = spacing
                    }
                }

                // 垂直方向间距
                outRect.top = if (isFirstRow(position, spanCount)) 0 else spacing
                outRect.bottom = spacing
            }
            else -> {
                // 其他列数使用均匀间距
                setupUniformSpacing(outRect, spanIndex, spanCount, position)
            }
        }
    }


    private fun setupUniformSpacing(
        outRect: Rect,
        spanIndex: Int,
        spanCount: Int,
        position: Int
    ) {
        // 水平间距
        val column = spanIndex % spanCount

        // 左边距：第一列有完整间距，其他列使用一半间距
        outRect.left = if (column == 0) spacing else spacing / 2

        // 右边距：最后一列有完整间距，其他列使用一半间距
        outRect.right = if (column == spanCount - 1) spacing else spacing / 2

        // 垂直间距
        outRect.top = if (isFirstRow(position, spanCount)) 0 else spacing
        outRect.bottom = spacing
    }

    private fun isFirstRow(position: Int, spanCount: Int): Boolean {
        return position < spanCount
    }
}

object GridDecorationUtils {
    /**
     * 为 RecyclerView 快速添加间距装饰器
     */
    fun applySpacing(
        recyclerView: RecyclerView,
        spacingDp: Int,
        spanCount: Int = 2
    ) {
        val spacingInPixels = recyclerView.context.resources
            .getDimensionPixelSize(spacingDp)

        recyclerView.addItemDecoration(
            StaggeredGridSpacingItemDecoration(spanCount, spacingInPixels)
        )
    }

    /**
     * 计算网格列数
     */
    fun calculateSpanCount(context: Context, itemWidthDp: Int = 180): Int {
        val displayMetrics = context.resources.displayMetrics
        val screenWidthPx = displayMetrics.widthPixels
        val screenWidthDp = screenWidthPx / displayMetrics.density

        return maxOf(2, (screenWidthDp / itemWidthDp).toInt())
    }
}