package com.gestoreasy.ui.widgets

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.view.View
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

/**
 * Helper para ações de swipe em RecyclerView
 */
class SwipeActionHelper(
    private val onSwipeLeft: ((Int) -> Unit)? = null,
    private val onSwipeRight: ((Int) -> Unit)? = null,
    private val leftActionText: String = "Excluir",
    private val rightActionText: String = "Editar",
    private val leftActionColor: Int = Color.parseColor("#EF4444"),
    private val rightActionColor: Int = Color.parseColor("#3B82F6")
) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
    
    private val paint = Paint().apply {
        isAntiAlias = true
        textSize = 48f
        textAlign = Paint.Align.CENTER
    }
    
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean = false
    
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        val position = viewHolder.adapterPosition
        when (direction) {
            ItemTouchHelper.LEFT -> onSwipeLeft?.invoke(position)
            ItemTouchHelper.RIGHT -> onSwipeRight?.invoke(position)
        }
    }
    
    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        val itemView = viewHolder.itemView
        val itemHeight = itemView.bottom - itemView.top
        val itemWidth = itemView.right - itemView.left
        
        // Background para swipe esquerdo (vermelho)
        if (dX < 0) {
            val background = RectF(
                itemView.right + dX,
                itemView.top.toFloat(),
                itemView.right.toFloat(),
                itemView.bottom.toFloat()
            )
            paint.color = leftActionColor
            c.drawRect(background, paint)
            
            // Texto
            paint.color = Color.WHITE
            val textY = itemView.top + itemHeight / 2f + (paint.textSize / 3)
            c.drawText(leftActionText, background.centerX(), textY, paint)
        }
        
        // Background para swipe direito (azul)
        if (dX > 0) {
            val background = RectF(
                itemView.left.toFloat(),
                itemView.top.toFloat(),
                itemView.left + dX,
                itemView.bottom.toFloat()
            )
            paint.color = rightActionColor
            c.drawRect(background, paint)
            
            // Texto
            paint.color = Color.WHITE
            val textY = itemView.top + itemHeight / 2f + (paint.textSize / 3)
            c.drawText(rightActionText, background.centerX(), textY, paint)
        }
        
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
    }
}

