package com.light.prank.sound.app.utils

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.animation.doOnEnd

class CustomRippleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : View(context, attrs) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#FFFFFF")
        style = Paint.Style.FILL
    }

    private val ripples = mutableListOf<Ripple>()
    private val maxRadius = 200f
    private val rippleDuration = 1000L
    private val rippleDelay = 300L

    private var curisRunning = false
    private var isPaused = false

    private val rippleRunnable = object : Runnable {
        override fun run() {
            if (!curisRunning || isPaused) return
            val ripple = Ripple()
            ripples.add(ripple)
            ripple.start()
            postDelayed(this, rippleDelay)
        }
    }

    fun startRipple() {
        if (curisRunning) return
        curisRunning = true
        isPaused = false
        post(rippleRunnable)
    }

    fun stopRipple() {
        curisRunning = false
        isPaused = false
        removeCallbacks(rippleRunnable)
        ripples.forEach { it.cancel() }
        ripples.clear()
        invalidate()
    }

    fun pauseRipple() {
        isPaused = true
        ripples.forEach { it.pause() }
        removeCallbacks(rippleRunnable)
    }

    fun resumeRipple() {
        if (!curisRunning || !isPaused) return
        isPaused = false
        ripples.forEach { it.resume() }
        post(rippleRunnable)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val centerX = width / 2f
        val centerY = height / 2f
        val iterator = ripples.iterator()
        while (iterator.hasNext()) {
            val ripple = iterator.next()
            if (!ripple.isRunning) {
                iterator.remove()
                continue
            }
            paint.alpha = (255 * (1 - ripple.progress)).toInt()
            canvas.drawCircle(centerX, centerY, maxRadius * ripple.progress, paint)
        }
        if (ripples.isNotEmpty()) invalidate()
    }

    inner class Ripple {
        var progress = 0f
        var isRunning = true
        private var animator: ValueAnimator? = null

        fun start() {
            animator = ValueAnimator.ofFloat(0f, 1f).apply {
                duration = rippleDuration
                addUpdateListener {
                    progress = it.animatedValue as Float
                    invalidate()
                }
                doOnEnd {
                    this@Ripple.isRunning = false
                }
                start()
            }
        }

        fun pause() {
            if (animator?.isRunning == true) {
                animator?.pause()
            }
        }

        fun resume() {
            if (animator?.isPaused == true) {
                animator?.resume()
            }
        }

        fun cancel() {
            animator?.cancel()
            isRunning = false
        }
    }
}
