package com.rgt.superr.utils

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

object MyAnimationUtils {


    fun rotation(view: View, durationTime: Long = 2000) {
        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 ofFloat(end: Float, durationTime: Long, update: (value: Float) -> Unit) {
        ValueAnimator.ofFloat(0f, end).apply {
            duration = durationTime
            interpolator = LinearInterpolator()
            addUpdateListener { animation ->
                val value = animation.animatedValue as Float
                update(value)
            }
        }.start()
    }

    fun translationX(
        view: View,
        start: Float,
        end: Float,
        durationTime: Long = 1500,
        delay: Long = 0
    ) {
        ObjectAnimator.ofFloat(view, View.TRANSLATION_X, start, end).apply {
            duration = durationTime
            startDelay = delay
            start()
        }
    }
}