package com.kdanmobile.reader.screen.reader.mediabox.signature

import android.graphics.Rect
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View


class SignatureRecyclerViewItemDecoration(private val space: Int, private val additionalPadding: Int) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val position = parent.getChildAdapterPosition(view)
        val childCount = parent.adapter?.itemCount ?: 0
        val layoutManager = parent.layoutManager as GridLayoutManager
        val currentRow = (position / layoutManager.spanCount)
        val lastRow = ((childCount - 1) / layoutManager.spanCount)
        outRect.bottom = when (currentRow == lastRow) {
            true -> space + additionalPadding
            false -> space
        }
    }
}