package com.gh.gamecenter.common.view

import android.graphics.Rect
import android.view.View
import androidx.annotation.Dimension
import androidx.recyclerview.widget.RecyclerView

/**
 * 简单的 recyclerView Item 装饰类
 *
 * @param onlyDecorateTheFirstItem 只装饰第一个 item
 * @param notDecorateTheFirstItem 不装饰第一个 item
 * @param notDecorateTheLastItem 不装饰最后一个 item
 * @param notDecorateTheFirstTwoItems 不装饰前两个 item
 *
 * left, top ,right, bottom 分别是四边
 */
class SpacingItemDecoration(
    var onlyDecorateTheFirstItem: Boolean = false,
    var notDecorateTheFirstItem: Boolean = false,
    var notDecorateTheLastItem: Boolean = false,
    var notDecorateTheFirstTwoItems: Boolean = false,
    @Dimension(unit = Dimension.PX)
    var left: Int = 0,
    @Dimension(unit = Dimension.PX)
    var top: Int = 0,
    @Dimension(unit = Dimension.PX)
    var right: Int = 0,
    @Dimension(unit = Dimension.PX)
    var bottom: Int = 0
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        if (onlyDecorateTheFirstItem) {
            if (parent.getChildAdapterPosition(view) == 0) outRect.set(left, top, right, bottom)
        } else {
            if (parent.getChildAdapterPosition(view) == 0
                && (notDecorateTheFirstItem || notDecorateTheFirstTwoItems)
            ) {
                outRect.set(0, 0, 0, 0)
            } else if (parent.getChildAdapterPosition(view) == 1 && notDecorateTheFirstTwoItems) {
                outRect.set(0, 0, 0, 0)
            } else if (parent.getChildAdapterPosition(view) == parent.adapter!!.itemCount - 1 && notDecorateTheLastItem) {
                outRect.set(0, 0, 0, 0)
            } else {
                outRect.set(left, top, right, bottom)
            }
        }
    }
}
