package com.gestoreasy.ui.animations

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.recyclerview.widget.RecyclerView

/**
 * Utilitários de animação para melhorar UX
 */
object AnimationUtils {
    
    /**
     * Fade in/out para views
     */
    fun fadeIn(view: View, duration: Long = 300) {
        view.alpha = 0f
        view.visibility = View.VISIBLE
        view.animate()
            .alpha(1f)
            .setDuration(duration)
            .setInterpolator(DecelerateInterpolator())
            .start()
    }
    
    fun fadeOut(view: View, duration: Long = 300, onEnd: () -> Unit = {}) {
        view.animate()
            .alpha(0f)
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.GONE
                    onEnd()
                }
            })
            .start()
    }
    
    /**
     * Slide in/out
     */
    fun slideInFromBottom(view: View, duration: Long = 300) {
        view.translationY = view.height.toFloat()
        view.alpha = 0f
        view.visibility = View.VISIBLE
        view.animate()
            .translationY(0f)
            .alpha(1f)
            .setDuration(duration)
            .setInterpolator(DecelerateInterpolator())
            .start()
    }
    
    fun slideOutToBottom(view: View, duration: Long = 300, onEnd: () -> Unit = {}) {
        view.animate()
            .translationY(view.height.toFloat())
            .alpha(0f)
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.GONE
                    onEnd()
                }
            })
            .start()
    }
    
    /**
     * Scale animation
     */
    fun scaleIn(view: View, duration: Long = 200) {
        view.scaleX = 0f
        view.scaleY = 0f
        view.alpha = 0f
        view.visibility = View.VISIBLE
        view.animate()
            .scaleX(1f)
            .scaleY(1f)
            .alpha(1f)
            .setDuration(duration)
            .setInterpolator(DecelerateInterpolator())
            .start()
    }
    
    fun scaleOut(view: View, duration: Long = 200, onEnd: () -> Unit = {}) {
        view.animate()
            .scaleX(0f)
            .scaleY(0f)
            .alpha(0f)
            .setDuration(duration)
            .setInterpolator(AccelerateDecelerateInterpolator())
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.GONE
                    onEnd()
                }
            })
            .start()
    }
    
    /**
     * Animações para RecyclerView
     */
    fun animateRecyclerViewItems(recyclerView: RecyclerView) {
        val adapter = recyclerView.adapter ?: return
        for (i in 0 until adapter.itemCount) {
            val view = recyclerView.layoutManager?.findViewByPosition(i)
            view?.let {
                it.alpha = 0f
                it.translationY = 50f
                it.animate()
                    .alpha(1f)
                    .translationY(0f)
                    .setDuration(300)
                    .setStartDelay(i * 50L)
                    .setInterpolator(DecelerateInterpolator())
                    .start()
            }
        }
    }
    
    /**
     * Shake animation para erros
     */
    fun shake(view: View, duration: Long = 500) {
        val animator = ObjectAnimator.ofFloat(view, "translationX", 0f, 25f, -25f, 25f, -25f, 15f, -15f, 6f, -6f, 0f)
        animator.duration = duration
        animator.start()
    }
    
    /**
     * Pulse animation
     */
    fun pulse(view: View, duration: Long = 1000, repeat: Boolean = true) {
        val animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f, 1f)
        animator.duration = duration
        if (repeat) {
            animator.repeatCount = ObjectAnimator.INFINITE
        }
        animator.start()
    }
}

