package com.gamedog.vididin.widget


import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.view.isVisible
import com.gamedog.vididin.databinding.LayoutDragIconViewBinding
import com.gamedog.vididin.manager.TaskManager

class HomeDragIconView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private var mBinding: LayoutDragIconViewBinding


    init {
        mBinding = LayoutDragIconViewBinding.inflate(LayoutInflater.from(context), this, true)
        mBinding.run {
            progressBar.setProgressListener { progress->
                if (progress >= 100) {
                    tvGoldNum.text = buildString {
                        append("+")
                        append(TaskManager.instance().getHomeWatchDurationRewardNum())
                    }

                    executeGoldNumAnim()
                }
            }
        }
    }


    private fun executeGoldNumAnim() {
        val animView = mBinding.llGoldRoot

        val alphaAnimator = ObjectAnimator.ofFloat(
            animView,
            "alpha",
            0.5f, 1.0f, 0.5f
        ).apply {
            duration = 500
        }

        val scaleXAnimator = ObjectAnimator.ofFloat(
            animView,
            "scaleX",
            0.5f, 1.0f, 0.5f
        ).apply {
            duration = 500
        }

        val scaleYAnimator = ObjectAnimator.ofFloat(
            animView,
            "scaleY",
            0.5f, 1.0f, 0.5f
        ).apply {
            duration = 500
        }


        val animatorSet = AnimatorSet()
        animatorSet.playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator)
        animatorSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationCancel(animation: Animator) {
                animView.isVisible = false
                mBinding.progressBar.setProgress(0)
            }

            override fun onAnimationEnd(animation: Animator) {
                animView.isVisible = false
                mBinding.progressBar.setProgress(0)
            }

            override fun onAnimationRepeat(animation: Animator) {

            }

            override fun onAnimationStart(animation: Animator) {

            }
        })
        animatorSet.start()
    }

}



