package com.ama.core.architecture.util

import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class CommonItemDecoration private constructor(
    private val horizontalSpace: Int,
    private val verticalSpace: Int,
    private val includeEdge: Boolean = false
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect, 
        view: View, 
        parent: RecyclerView, 
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        
        val position = parent.getChildAdapterPosition(view)
        val adapter = parent.adapter ?: return
        val itemCount = adapter.itemCount
        
        when (val layoutManager = parent.layoutManager) {
            is GridLayoutManager -> {
                handleGridLayout(outRect, position, itemCount, layoutManager)
            }
            is LinearLayoutManager -> {
                handleLinearLayout(outRect, position, itemCount, layoutManager)
            }
            is StaggeredGridLayoutManager -> {
                handleStaggeredGridLayout(outRect, position, itemCount, layoutManager)
            }
        }
    }
    
    private fun handleGridLayout(
        outRect: Rect,
        position: Int,
        itemCount: Int,
        layoutManager: GridLayoutManager
    ) {
        val spanCount = layoutManager.spanCount
        val orientation = layoutManager.orientation
        
        if (orientation == GridLayoutManager.VERTICAL) {
            // 垂直网格布局
            val column = position % spanCount
            
            if (includeEdge) {
                outRect.left = horizontalSpace * (spanCount - column) / spanCount
                outRect.right = horizontalSpace * (column + 1) / spanCount
                
                if (position < spanCount) {
                    outRect.top = verticalSpace
                }
                outRect.bottom = verticalSpace
            } else {
                outRect.left = horizontalSpace * column / spanCount
                outRect.right = horizontalSpace * (spanCount - 1 - column) / spanCount
                
                if (position >= spanCount) {
                    outRect.top = verticalSpace
                }
            }
        } else {
            // 水平网格布局
            val row = position % spanCount
            
            if (includeEdge) {
                outRect.top = verticalSpace * (spanCount - row) / spanCount
                outRect.bottom = verticalSpace * (row + 1) / spanCount
                
                if (position < spanCount) {
                    outRect.left = horizontalSpace
                }
                outRect.right = horizontalSpace
            } else {
                outRect.top = verticalSpace * row / spanCount
                outRect.bottom = verticalSpace * (spanCount - 1 - row) / spanCount
                
                if (position >= spanCount) {
                    outRect.left = horizontalSpace
                }
            }
        }
    }
    
    private fun handleLinearLayout(
        outRect: Rect,
        position: Int,
        itemCount: Int,
        layoutManager: LinearLayoutManager
    ) {
        when (layoutManager.orientation) {
            LinearLayoutManager.VERTICAL -> {
                // 垂直线性布局
                outRect.left = horizontalSpace
                outRect.right = horizontalSpace
                
                if (position == 0 && includeEdge) {
                    outRect.top = verticalSpace
                }
                
                if (includeEdge) {
                    outRect.bottom = verticalSpace
                } else {
                    outRect.bottom = if (position == itemCount - 1) 0 else verticalSpace
                }
            }
            LinearLayoutManager.HORIZONTAL -> {
                // 水平线性布局
                outRect.top = verticalSpace
                outRect.bottom = verticalSpace
                
                if (position == 0 && includeEdge) {
                    outRect.left = horizontalSpace
                }
                
                if (includeEdge) {
                    outRect.right = horizontalSpace
                } else {
                    outRect.right = if (position == itemCount - 1) 0 else horizontalSpace
                }
            }
        }
    }
    
    private fun handleStaggeredGridLayout(
        outRect: Rect,
        position: Int,
        itemCount: Int,
        layoutManager: StaggeredGridLayoutManager
    ) {
        outRect.left = horizontalSpace / 2
        outRect.right = horizontalSpace / 2
        outRect.top = verticalSpace / 2
        outRect.bottom = verticalSpace / 2
    }

    companion object {
        fun create(
            horizontalSpace: Int,
            verticalSpace: Int,
            includeEdge: Boolean = true
        ): CommonItemDecoration {
            return CommonItemDecoration(horizontalSpace, verticalSpace, includeEdge)
        }

        fun createFromDp(
            horizontalSpaceDp: Int,
            verticalSpaceDp: Int,
            context: Context,
            includeEdge: Boolean = true
        ): CommonItemDecoration {
            val horizontalSpacePx = dpToPx(horizontalSpaceDp, context)
            val verticalSpacePx = dpToPx(verticalSpaceDp, context)
            return CommonItemDecoration(horizontalSpacePx, verticalSpacePx, includeEdge)
        }
        
        private fun dpToPx(dp: Int, context: Context): Int {
            val density = context.resources.displayMetrics.density
            return (dp * density + 0.5f).toInt()
        }
    }
}