package com.xty.common.recycleUtils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.xty.common.SizeUtil

/**
 * managerType  layoutManager 默认为0
 *
 *
 *  GridLayoutManager  ：1
 *
 */
class RecycleItem(var context:Context,var lineHeight:Int,var managerType:Int=0): RecyclerView.ItemDecoration() {
    var vSpace=0f
    var hSpace=0f
    fun setSpace(h:Int,v:Int){
        vSpace=SizeUtil.px2dp(context,h.toFloat())
        hSpace=SizeUtil.px2dp(context,v.toFloat())
    }
    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
    }



    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
    }


    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
      var line=  SizeUtil.px2dp(context,lineHeight.toFloat())
        when(managerType) {
            0 -> outRect.set(0, line.toInt(), 0, line.toInt())
            1->{
                val gridLayoutManager = parent.layoutManager as GridLayoutManager
                val spanCount = gridLayoutManager.spanCount
                val childLayoutPosition = parent.getChildLayoutPosition(view)
                if(childLayoutPosition%spanCount==0){
                    outRect.set(0, vSpace.toInt(), (hSpace/2).toInt(), 0)
                }else if(childLayoutPosition+1%spanCount==0){
                    outRect.set( (hSpace/2).toInt(), vSpace.toInt(), 0, 0)
                }else{
                    outRect.set( (hSpace/2).toInt(), vSpace.toInt(),  (hSpace/2).toInt(), 0)
                }



            }
        }
    }
}