package com.atmob.keyboard_android.util.recyclerview

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * 线性LinearLayoutManager的分割线
 */
class LinearDivider(
    // 分割线的大小，单位为px
    private val dividerSizePx: Int,
    // 方向，垂直还是横向
    private val orientation: Int = LinearLayoutManager.VERTICAL,
    // 最后一个条目，是否要有分割线
    private val lastItemHasDivider: Boolean = true
) : RecyclerView.ItemDecoration() {
    /**
     * 画笔
     */
    private val mPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.TRANSPARENT
        style = Paint.Style.FILL
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val totalItemCount = parent.adapter?.itemCount ?: 0

        when (orientation) {
            LinearLayoutManager.VERTICAL -> {
                // 垂直列表：下方添加间距（最后一个项不添加）
                outRect.bottom = if (position < totalItemCount - 1) dividerSizePx else 0
            }

            LinearLayoutManager.HORIZONTAL -> {
                // 水平列表：右侧添加间距（最后一个项不添加）
                outRect.right = if (position < totalItemCount - 1) dividerSizePx else 0
            }
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        when (orientation) {
            LinearLayoutManager.VERTICAL -> drawVerticalDividers(c, parent)
            LinearLayoutManager.HORIZONTAL -> drawHorizontalDividers(c, parent)
        }
    }

    /**
     * 纵向分割线
     */
    private fun drawVerticalDividers(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight

        // 绘制方法
        fun doDraw(childView: View) {
            val params = childView.layoutParams as RecyclerView.LayoutParams
            val top = childView.bottom + params.bottomMargin
            val bottom = top + dividerSizePx
            canvas.drawRect(
                left.toFloat(),
                top.toFloat(),
                right.toFloat(),
                bottom.toFloat(),
                mPaint
            )
        }

        // 最后一项，也有分割线
        val childCount = if (lastItemHasDivider) {
            parent.childCount
        } else {
            // 最后一个项不绘制
            parent.childCount - 1
        }
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            doDraw(child)
        }
    }

    /**
     * 横向分割线
     */
    private fun drawHorizontalDividers(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.height - parent.paddingBottom

        // 绘制方法
        fun doDraw(childView: View) {
            val params = childView.layoutParams as RecyclerView.LayoutParams
            val left = childView.right + params.rightMargin
            val right = left + dividerSizePx
            canvas.drawRect(
                left.toFloat(),
                top.toFloat(),
                right.toFloat(),
                bottom.toFloat(),
                mPaint
            )
        }

        // 最后一项，也有分割线
        val childCount = if (lastItemHasDivider) {
            parent.childCount
        } else {
            // 最后一个项不绘制
            parent.childCount - 1
        }

        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            doDraw(child)
        }
    }
}