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

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.util.DisplayUtil


class HorizontalItemDecoration(
    private val space: Int,
    private val startPadding: Int,
    private val endPadding: Int,
) :
    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)
        val totalCount = parent.adapter!!.itemCount
        if (DisplayUtil.isRtlLayout()) {
            when (position) {
                0 -> { //第一个
                    outRect.right = startPadding
                    outRect.left = space / 2
                }

                totalCount - 1 -> { //最后一个
                    outRect.right = space / 2
                    outRect.left = endPadding
                }

                else -> { //中间其它的
                    outRect.right = space / 2
                    outRect.left = space / 2
                }
            }
        } else {
            when (position) {
                0 -> { //第一个
                    outRect.left = startPadding
                    outRect.right = space / 2
                }

                totalCount - 1 -> { //最后一个
                    outRect.left = space / 2
                    outRect.right = endPadding
                }

                else -> { //中间其它的
                    outRect.left = space / 2
                    outRect.right = space / 2
                }
            }
        }

    }
}
