package com.base.scanqrclear.luma

import android.animation.ValueAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ProgressBar

object AnimationHelps {

    fun rotation(view: View, durationTime: Long) {
        ValueAnimator.ofFloat(0f, 360f).apply {
            duration = durationTime
            interpolator = LinearInterpolator()
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.RESTART
            addUpdateListener { animation ->
                val animatedValue = animation.animatedValue as Float
                view.rotation = animatedValue
            }
        }.start()
    }

    fun progress(progressBar: ProgressBar, start: Int = 0, end: Int = 100, durationTime: Long = 1500) {
        ValueAnimator.ofInt(start, end).apply {
            duration = durationTime
            interpolator = LinearInterpolator()
            addUpdateListener { animation ->
                progressBar.progress = animation.animatedValue as Int
            }
        }.start()
    }

    fun progress(progressBar: CircleProgressBar, endProgress: Int, durationTime: Long = 1500): ValueAnimator {
        val animator = ValueAnimator.ofInt(0, endProgress).apply {
            duration = durationTime
            interpolator = LinearInterpolator()
            addUpdateListener { animation ->
                val progress = animation.animatedValue as Int
                progressBar.setProgress(progress / 100f)
            }
        }
        animator.start()
        return animator
    }

    fun progress(progressBar: MyProgressBar, endProgress: Int, durationTime: Long = 1500, complete: (() -> Unit?)? = null): ValueAnimator {
        return progress(progressBar, 0, endProgress, durationTime, complete)
    }

    fun progress(
        progressBar: MyProgressBar,
        startProgress: Int,
        endProgress: Int,
        durationTime: Long = 1500,
        complete: (() -> Unit?)? = null
    ): ValueAnimator {
        val animator = ValueAnimator.ofInt(startProgress, endProgress).apply {
            duration = durationTime
            interpolator = LinearInterpolator()
            addUpdateListener { animation ->
                val progress = animation.animatedValue as Int
                progressBar.setProgress(progress)
                if (progress == endProgress) {
                    complete?.invoke()
                }
            }
        }
        animator.start()
        return animator
    }
}