package com.lee.democollection.view.itemdecoration

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View

/**
 * Created by Allen on 2017/6/1.
 */
class GridDividerItemDecoration : RecyclerView.ItemDecoration {
    var mDivider : Drawable ?= null
    var mDividerSize = -1
    var mClose = true
    var mEnableRow = true  //允许行线
    var mEnableCol = true   //允许列线


    constructor(close : Boolean){
        mClose = close
        mDivider = ColorDrawable(Color.BLACK)
        mDividerSize = 1
    }

    fun setEnableRow(enableRow : Boolean){
        mEnableRow = enableRow
    }

    fun setEnableCol(enableCol : Boolean){
        mEnableCol = enableCol
    }

    fun setDividerSize(dividerSize : Int){
        mDividerSize = dividerSize
    }

    fun setDivider(divider : Drawable){
        mDivider = divider
    }

    override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
        if(mClose){
//            var pos = parent?.getChildAdapterPosition(view)
//            var layout = parent.layoutManager as GridLayoutManager
//            val spanCount = layout.spanCount
            with(parent!!){
                var pos = getChildAdapterPosition(view)
                var layout = layoutManager as GridLayoutManager
                val spanCount = layout.spanCount

                var fisrtRow = pos < spanCount //第一行
//                var lastRow = childCount - pos <= spanCount //最后一行
                var firstCol = pos % spanCount == 0 //第一列
//                var lastCol = pos % spanCount == spanCount - 1 || pos == childCount - 1  //最后一列

                var left = 0
                var top = 0
                if(firstCol){
                    left = mDividerSize
                }
                if(fisrtRow){
                    top = mDividerSize
                }
                outRect?.set(left, top, mDividerSize, mDividerSize)
            }


        }else{
            outRect?.set(0, 0, mDividerSize, mDividerSize)
        }
    }

    override fun onDraw(c: Canvas?, parent: RecyclerView?, state: RecyclerView.State?) {
        val childCount = parent!!.childCount
        var layout = parent.layoutManager as GridLayoutManager
        val spanCount = layout.spanCount

        for (i in 0 until childCount){
            var fisrtRow = i < spanCount //第一行
            var lastRow = childCount - i <= spanCount //最后一行
            var firstCol = i % spanCount == 0 //第一列
            var lastCol = i % spanCount == spanCount - 1 || i == childCount - 1  //最后一列

            var child = parent.getChildAt(i)
            if(mEnableCol){
                /**
                 * 横向绘制竖线
                 */
                drawHorizontal(c, child, firstCol, lastCol)
            }
            if(mEnableRow){
                /**
                 * 竖向绘制横线
                 */
                drawVertical(c, child, fisrtRow, lastRow)
            }
        }
    }

    fun drawHorizontal(c : Canvas ?, child : View?, firstCol : Boolean, lastCol : Boolean){
        if(firstCol && mClose){
            mDivider!!.setBounds(child!!.left - mDividerSize, child.top, child.left, child.bottom + mDividerSize)
            mDivider!!.draw(c)
        }
        if(mClose || !lastCol){
            mDivider!!.setBounds(child!!.right, child.top, child.right + mDividerSize, child.bottom + mDividerSize)
            mDivider!!.draw(c)
        }
    }

    fun drawVertical(c : Canvas ?, child : View?, fistRow : Boolean, lastRow : Boolean){
        if(fistRow && mClose){
            var left = if(mEnableRow && mEnableCol) child!!.left - mDividerSize else child!!.left

            mDivider!!.setBounds(left, child.top - mDividerSize, child.right + mDividerSize, child.top)
            mDivider!!.draw(c)
        }
        if(mClose || !lastRow){
            mDivider!!.setBounds(child!!.left, child.bottom, child.right + mDividerSize, child.bottom + mDividerSize)
            mDivider!!.draw(c)
        }
    }
}