package com.gh.gamecenter.common.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.roundToInt

class CustomDividerItemDecoration @JvmOverloads constructor(
    context: Context,
    var onlyDecorateTheFirstItem: Boolean = false,
    var notDecorateTheFirstItem: Boolean = false,
    var notDecorateTheLastItem: Boolean = false,
    var notDecorateTheFirstTwoItems: Boolean = false,
    var notDecorateTheLastTwoItems: Boolean = false,
    var notDecorateTheFirstThreeItems: Boolean = false
) : DividerItemDecoration(context, LinearLayout.VERTICAL) {
    private val mBounds = Rect()

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        if (onlyDecorateTheFirstItem) {
            if (parent.getChildAdapterPosition(view) == 0) outRect[0, 0, 0] = drawable!!.intrinsicHeight
        } else {
            if (parent.getChildAdapterPosition(view) == 0
                && (notDecorateTheFirstItem || notDecorateTheFirstTwoItems || notDecorateTheFirstThreeItems)
            ) {
                outRect.set(0, 0, 0, 0)
            } else if (parent.getChildAdapterPosition(view) == 1 && (notDecorateTheFirstTwoItems || notDecorateTheFirstThreeItems)) {
                outRect.set(0, 0, 0, 0)
            } else if (parent.getChildAdapterPosition(view) == 2 && notDecorateTheFirstThreeItems) {
                outRect.set(0, 0, 0, 0)
            } else if (parent.getChildAdapterPosition(view) == parent.adapter!!.itemCount - 2 && notDecorateTheLastTwoItems) {
                outRect.set(0, 0, 0, 0)
            } else if (parent.getChildAdapterPosition(view) == parent.adapter!!.itemCount - 1 && (notDecorateTheLastItem || notDecorateTheLastTwoItems)) {
                outRect.set(0, 0, 0, 0)
            } else {
                outRect[0, 0, 0] = drawable!!.intrinsicHeight
            }
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.layoutManager == null || drawable == null) {
            return
        }
        drawVertical(c, parent)
    }

    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        canvas.save()
        val left: Int
        val right: Int
        if (parent.clipToPadding) {
            left = parent.paddingLeft
            right = parent.width - parent.paddingRight
            canvas.clipRect(
                left, parent.paddingTop, right,
                parent.height - parent.paddingBottom
            )
        } else {
            left = 0
            right = parent.width
        }
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            parent.getDecoratedBoundsWithMargins(child, mBounds)
            var bottom = mBounds.bottom + child.translationY.roundToInt()
            val rect = Rect()
            getItemOffsets(rect, child, parent, RecyclerView.State())
            val top = if (rect.bottom > 0) {
                bottom - (drawable?.intrinsicHeight ?: 0)
            } else {
                bottom = 0
                0
            }
            drawable?.setBounds(left, top, right, bottom)
            drawable?.draw(canvas)
        }
        canvas.restore()
    }
}