package com.swago.room.piaotiao

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 android.widget.FrameLayout
import android.widget.LinearLayout
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.room.R
import com.swago.room.databinding.ViewItemGiftBinding
import com.swago.room.databinding.ViewWaftBinding
import com.swago.room.widget.MarqueeTextView
import java.util.*

/**
 *@date 2021/12/16 19:33
 *description:
 */
class WaftView : FrameLayout {

    var isBusy = false

    private val imGiftModelList = LinkedList<IMGiftModel>()
    private var binding : ViewWaftBinding? = null

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

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


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

    private var animatorSet: AnimatorSet? = null

    private fun createAnim(view: View): AnimatorSet {
        val objectAnimator = ObjectAnimator.ofFloat(
            view,
            "translationX",
            DpPxUtil.getScreenWidth().toFloat(),
            0f
        )
        objectAnimator.duration = 1000
        val stayAnimal = ValueAnimator.ofFloat(1f, 0f)
        stayAnimal.duration = 4500
        val innerAnimatorSet = AnimatorSet()
        innerAnimatorSet.playTogether(objectAnimator, stayAnimal)

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

            override fun onAnimationEnd(animation: Animator?) {
                animatorSet = null
                binding?.ll?.visibility = View.INVISIBLE
                loopNextWaft()
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
                binding?.ll?.visibility = View.VISIBLE
            }
        })

        return innerAnimatorSet
    }

    private fun loopNextWaft() {
        if (imGiftModelList.isEmpty()) {
            isBusy = false
            return
        }
        val next = imGiftModelList.pop()
        val sendName = "<font color='#FFDB43'>${next.senderName}</font>"
        val giftName = "<font color='#FFDB43'>${next.giftName}</font>"
        val receiverName = "<font color='#FFDB43'>${next.receiverName}</font>"
        binding?.marqueeTextView?.text = Html.fromHtml(AppContext.getContext().resources.getString(R.string.big_gift_msg).format(sendName,giftName,receiverName))
        binding?.marqueeTextView?.post {
            binding?.marqueeTextView?.startScroll()
        }
        binding?.let {
            animatorSet = createAnim(it.ll)
            animatorSet?.start()
        }

    }


    fun clear() {
        imGiftModelList.clear()
        animatorSet = null
        binding?.ll?.visibility = View.INVISIBLE
    }

}