package com.swago.room.gift.control

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.text.Html
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.util.AppContext
import com.swago.room.R
import com.swago.room.databinding.LayoutSvgDanDaoViewBinding
import java.util.*

/**
 *@date 2022/2/24 10:33
 *description:
 */
class SvgDanDaoView : ConstraintLayout {

    private var binding: LayoutSvgDanDaoViewBinding? = null
    private val imGiftModelList = LinkedList<IMGiftModel>()
    var isBusy = false
    private var animatorSet: AnimatorSet? = null


    constructor(context: Context) : super(context)
    constructor(context: Context, mAttributeSet: AttributeSet?) : super(context, mAttributeSet) {
        initView(context)
    }

    private fun initView(context: Context) {
        binding = LayoutSvgDanDaoViewBinding.inflate(LayoutInflater.from(context), this, true)
    }


    fun addData(imGiftModel: IMGiftModel) {
        isBusy = true
        imGiftModelList.add(imGiftModel)
        if (animatorSet?.isRunning != true) {
            loopNextWaft()
        }
    }

    private fun loopNextWaft() {
        binding?.apply {
            if (imGiftModelList.isEmpty()) {
                isBusy = false
                return
            }
            val next = imGiftModelList.pop()
            Glide.with(context).load(next.senderAvatar)
                .placeholder(R.mipmap.default_avatar)
                .error(R.mipmap.default_avatar)
                .into(ivAvatar)
            val sendName = "<font color='#FFDB43'>${next.senderName}</font>"
            val giftName = "<font color='#FFDB43'>${next.giftName}</font>"
            val receiverName = "<font color='#FFDB43'>${next.receiverName}</font>"
            marqueeTextView.text = Html.fromHtml(
                AppContext.getContext().resources.getString(R.string.big_gift)
                    .format(sendName, giftName, receiverName)
            )
            marqueeTextView.post {
                marqueeTextView.startScroll()
            }
            animatorSet = createAnim(cl)
            animatorSet?.start()
        }
    }

    private fun createAnim(cl: ConstraintLayout): AnimatorSet {
        cl.visibility = View.VISIBLE
        val stayAnimal = ValueAnimator.ofFloat(1f, 0f)
        stayAnimal.duration = 4500
        val innerAnimatorSet = AnimatorSet()
        innerAnimatorSet.play(stayAnimal)

        innerAnimatorSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
                binding?.marqueeTextView?.stopScroll()
                cl.visibility = View.GONE
                animatorSet = null
                loopNextWaft()
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }
        })

        return innerAnimatorSet
    }


    fun clear() {
        binding?.cl?.visibility = View.GONE
        imGiftModelList.clear()
        animatorSet = null

    }

}
