package com.zj365.health.weight;

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.tamsiree.rxkit.RxAppTool
import com.tamsiree.rxkit.RxConstTool
import com.tamsiree.rxkit.RxDBTool
import com.tamsiree.rxkit.RxImageTool
import com.tamsiree.rxkit.RxTool

class GridDividerItemDecoration(
        private val spacing: Int,
        private val spanCount: Int
) : RecyclerView.ItemDecoration() {
        private val paint: Paint = Paint().apply {
                color = Color.parseColor("#EBF0F2")  // 设置分割线的颜色
                style = Paint.Style.FILL // 设置分割线的样式
                strokeWidth = RxImageTool.dip2px(0.5f).toFloat() // 设置分割线的宽度
        }
        override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
        ) {
        super.getItemOffsets(outRect, view, parent, state)
        val position = parent.getChildAdapterPosition(view)
        val column = position % spanCount

        outRect.left = spacing - column * spacing / spanCount
        outRect.right = (column + 1) * spacing / spanCount

        if (position >= spanCount) {
        outRect.top = spacing
        }
        }

        override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        drawVerticalLines(c, parent)
//        drawHorizontalLines(c, parent)
        }

private fun drawVerticalLines(c: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        val rowCount = childCount / spanCount

        for (i in 0 until childCount) {
        val child = parent.getChildAt(i)
        val params = child.layoutParams as RecyclerView.LayoutParams
        val position = parent.getChildAdapterPosition(child)
        val column = position % spanCount

        val startX = child.right + params.rightMargin
        val startY = child.top - params.topMargin
        val stopX = startX
        val stopY = child.bottom + params.bottomMargin + spacing

        if (column < spanCount - 1) {
        c.drawLine(startX.toFloat(), startY.toFloat(), stopX.toFloat(), stopY.toFloat(), paint)
        }

        if (position >= rowCount * spanCount && column < spanCount - 1) {
        c.drawLine(startX.toFloat(), startY.toFloat(), stopX.toFloat(), startY.toFloat(), paint)
        }
        }
        }

private fun drawHorizontalLines(c: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        val rowCount = childCount / spanCount

        for (i in 0 until childCount) {
        val child = parent.getChildAt(i)
        val params = child.layoutParams as RecyclerView.LayoutParams
        val position = parent.getChildAdapterPosition(child)

        val startX = child.left - params.leftMargin
        val startY = child.bottom + params.bottomMargin
        val stopX = child.right + params.rightMargin + spacing
        val stopY = startY

        if (position % spanCount < spanCount - 1) {
        c.drawLine(startX.toFloat(), startY.toFloat(), stopX.toFloat(), stopY.toFloat(), paint)
        }

        if (position / spanCount < rowCount) {
        c.drawLine(startX.toFloat(), startY.toFloat(), startX.toFloat(), stopY.toFloat(), paint)
        }
        }
        }
        }

