package com.adealink.weparty.commonui.recycleview.itemdecoration

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

/**
 * 给非独占一行的item 添加间隔
 */
class NonFullSpanItemDecoration(
    val spacingH: Int = 0,
    val spacingV: Int = 0,
    val extraTop: Int = 0,
    val extraBottom: Int = 0,
    val paddingStartEnd: Int = 0
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State,
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        val position = parent.getChildAdapterPosition(view) // 当前 item 的位置
        if (position == RecyclerView.NO_POSITION) return

        val spanCount = getSpanCount(parent)
        val spanSize = getSpanSize(parent, position)

        // 计算当前 item 之前有多少个“占满整行的 item”
        val fullRowItemsBefore = countFullRowItemsBefore(parent, position)

        // 计算去掉占满整行 item 后的真实位置
        val realPosition = position - fullRowItemsBefore
        val column = realPosition % spanCount // item 处于第几列
        val isRtl = parent.layoutDirection == View.LAYOUT_DIRECTION_RTL

        // 如果 item 占满整行，则不添加间隔
        if (spanSize == spanCount) return

        // 计算 item 间隔，使其在网格中均匀分布
        val totalSpacing = paddingStartEnd * 2 + spacingH * (spanCount - 1)
        val itemSpacing = totalSpacing / spanCount
        var left = 0
        var right = 0

        when (column) {
            0 -> { // 第一列
                left = paddingStartEnd
                right = itemSpacing - paddingStartEnd
            }
            spanCount - 1 -> { // 最后一列
                left = itemSpacing - paddingStartEnd
                right = paddingStartEnd
            }
            else -> { // 中间列
                left = spacingH - (itemSpacing - paddingStartEnd)
                right = itemSpacing - left
            }
        }

        outRect.left = if (isRtl) right else left
        outRect.right = if (isRtl) left else right

        // 只给非第一行的 item 添加顶部间隔
        if (realPosition >= spanCount) {
            outRect.top = spacingV
        }else {
            outRect.top = extraTop
        }

        // 计算是否是最后一行
        val totalRows = (state.itemCount - fullRowItemsBefore + spanCount - 1) / spanCount
        val currentRow = realPosition / spanCount + 1
        if (currentRow == totalRows) {
            outRect.bottom += extraBottom
        }
    }

    /**
     * 计算某个 item 之前有多少个“占满整行”的 item
     */
    private fun countFullRowItemsBefore(parent: RecyclerView, position: Int): Int {
        var fullRowCount = 0
        for (i in 0 until position) {
            if (getSpanSize(parent, i) == getSpanCount(parent)) {
                fullRowCount++
            }
        }
        return fullRowCount
    }

    private fun getSpanCount(parent: RecyclerView): Int {
        val layoutManager = parent.layoutManager
        return when (layoutManager) {
            is GridLayoutManager -> layoutManager.spanCount
            is StaggeredGridLayoutManager -> layoutManager.spanCount
            else -> -1
        }
    }

    private fun getSpanSize(parent: RecyclerView, position: Int): Int {
        val layoutManager = parent.layoutManager
        return when (layoutManager) {
            is GridLayoutManager -> layoutManager.spanSizeLookup.getSpanSize(position)
            is StaggeredGridLayoutManager -> 1
            else -> -1
        }
    }
}
