package com.gh.gamecenter.home.test_v2

import android.graphics.Rect
import android.view.View
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.core.utils.DisplayUtils

/**
 * @author : liujiarui
 * date : 2023/4/20
 * description : 新游开测，游戏列表 item decoration ,
 * 仅用于新游开测,让条目右侧漏出部分，或者最后一列左侧条目漏出部分
 */
class HomeGameTestV2Decoration : RecyclerView.ItemDecoration() {

    private val mParentWidth = DisplayUtils.getScreenWidth() - 32F.dip2px()

    companion object {
        private const val SPAN_COUNT = 3
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val itemCount = state.itemCount
        val position = parent.getChildAdapterPosition(view)
        var lastModPosition = itemCount % SPAN_COUNT
        if (lastModPosition == 0) lastModPosition = SPAN_COUNT
        val isLastRow = (itemCount - position) <= lastModPosition
        view.updateLayoutParams {
            width = mParentWidth - if (isLastRow) {
                8F.dip2px()
            } else {
                24F.dip2px()
            }
        }
        val padding = 16F.dip2px()
        if (isLastRow) {
            view.setPadding(padding, 0, padding, 12F.dip2px())
        } else {
            view.setPadding(padding, 0, 0, 12F.dip2px())
        }
    }
}