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 spaceDp: Int,
    private val startPaddingDp: Int,
    private val endPaddingDp: 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 = startPaddingDp
                    outRect.left = spaceDp / 2
                }

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

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

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

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

    }
}
