package com.miya.fastcashier.widget

import android.graphics.Canvas
import android.graphics.Paint
import android.view.View
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.RecyclerView

/**
 * 通用的RecyclerDivider
 */
class Divider(divideWidth: Int, color: Int) : ItemDecoration() {
    private val paint: Paint
    private val divideWidth: Int
    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val left = parent.paddingStart
        val right = parent.measuredWidth - parent.paddingEnd
        var child: View
        var childPosition: Int
        val validChildCount = parent.childCount
        for (i in 0 until validChildCount) {
            child = parent.getChildAt(i)
            childPosition = parent.getChildAdapterPosition(child)
            if (childPosition == validChildCount - 1) {
                break
            }
            val layoutParams = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + layoutParams.bottomMargin
            val bottom = top + divideWidth
            c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
        }
    }

    init {
        paint = Paint()
        paint.isAntiAlias = true
        paint.color = color
        this.divideWidth = divideWidth
    }
}