package com.casic.br.utils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import java.util.*


class GridItemDecoration(context: Context) : RecyclerView.ItemDecoration() {
    private val attrs = intArrayOf(android.R.attr.listDivider)
    private var dividerDrawable: Drawable? = null

    init {
        val a = context.obtainStyledAttributes(attrs)
        dividerDrawable = a.getDrawable(0)
        a.recycle()
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        drawHorizontalLine(c, parent)
        drawVerticalLine(c, parent)
    }

    private fun drawHorizontalLine(c: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val left = child.left - params.leftMargin
            val right = child.right + params.rightMargin + dividerDrawable!!.intrinsicWidth
            val top = child.bottom + params.bottomMargin
            val bottom = top + dividerDrawable!!.intrinsicHeight
            dividerDrawable!!.setBounds(left, top, right, bottom)
            dividerDrawable!!.draw(c)
        }
    }

    private fun drawVerticalLine(c: Canvas?, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.top - params.topMargin
            val bottom = child.bottom + params.bottomMargin
            val left = child.right + params.rightMargin
            val right = left + dividerDrawable!!.intrinsicWidth
            dividerDrawable!!.setBounds(left, top, right, bottom)
            dividerDrawable!!.draw(c!!)
        }
    }

    override fun getItemOffsets(
        outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
    ) {
        val spanCount = getSpanCount(parent)
        val childCount = Objects.requireNonNull(parent.adapter).itemCount
        val itemPosition = parent.getChildAdapterPosition(view)
        if (isLastColumn(parent, itemPosition, spanCount, childCount)) {
            // 如果是最后一列，则不需要绘制右边
            if (itemPosition == childCount - 1) {
                outRect[0, 0, 0] = 0
            } else {
                outRect[0, 0, 0] = dividerDrawable!!.intrinsicHeight
            }
        } else if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
            // 如果是最后一行，则不需要绘制底部
            outRect[0, 0, dividerDrawable!!.intrinsicWidth] = 0
        } else {
            outRect[0, 0, dividerDrawable!!.intrinsicWidth] = dividerDrawable!!.intrinsicHeight
        }
    }

    private fun getSpanCount(parent: RecyclerView): Int {
        // 列数
        var spanCount = -1
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            spanCount = layoutManager.spanCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            spanCount = layoutManager.spanCount
        }
        return spanCount
    }

    private fun isLastColumn(
        parent: RecyclerView, pos: Int, spanCount: Int, childCount: Int
    ): Boolean {
        var tempCount = childCount
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            // 如果是最后一列，则不需要绘制右边
            return (pos + 1) % spanCount == 0
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val orientation = layoutManager.orientation
            return if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                // 如果是最后一列，则不需要绘制右边
                (pos + 1) % spanCount == 0
            } else {
                tempCount -= tempCount % spanCount
                // 如果是最后一列，则不需要绘制右边
                pos >= tempCount
            }
        }
        return false
    }

    private fun isLastRaw(
        parent: RecyclerView, pos: Int, spanCount: Int, childCount: Int
    ): Boolean {
        var tempCount = childCount
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            var last = tempCount % spanCount
            if (last == 0) {
                last = spanCount
            }
            tempCount -= last
            // 如果是最后一行，则不需要绘制底部
            return pos >= tempCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val orientation = layoutManager.orientation
            // StaggeredGridLayoutManager 且纵向滚动
            return if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                var last = tempCount % spanCount
                if (last == 0) {
                    last = spanCount
                }
                tempCount -= last
                // 如果是最后一行，则不需要绘制底部
                pos >= tempCount
            } else {
                // StaggeredGridLayoutManager 且横向滚动，如果是最后一行，则不需要绘制底部
                (pos + 1) % spanCount == 0
            }
        }
        return false
    }
}