package com.swago.room.piaotiao

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.Activity
import android.content.Context
import android.text.Html
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.FragmentActivity
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.model.RedEnvelope
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.room.R
import com.swago.room.databinding.ViewHongBaoBinding
import com.swago.room.databinding.ViewWaftBinding
import java.util.concurrent.ConcurrentLinkedQueue

//红包飘条
class HongbaoPiaoTiaoView : LinearLayout, IRoomActiveListener {

    private var binding : ViewHongBaoBinding? = null
    private var isBusy = false
    private var animatorSet: AnimatorSet? = null

    private var queue = ConcurrentLinkedQueue<RedEnvelope>()

    private var mActivity: Activity? = null

    var goToTargetRoom:((anchorId:String)->Unit)? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView(context)
    }

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

    fun addQueue(redEnvelope: RedEnvelope){
        queue.add(redEnvelope)
        if (!isBusy){
            startRedEnvelop(redEnvelope)
        }
    }

    fun setActivity(activity: FragmentActivity?){
        this.mActivity = activity
    }


    private fun startRedEnvelop(redEnvelope: RedEnvelope){
        binding?.apply {
            isBusy = true
            visibility = View.VISIBLE
            val sendName = "<font color='#FFDB43'>${redEnvelope.senderName}</font>"
            if (redEnvelope.bigNotice == 1){
                binding?.ivJump?.visibility = View.VISIBLE
                binding?.marqueeTextView?.text = Html.fromHtml(AppContext.getContext().resources.getString(R.string.red_bg_piao_tiao_big).format(sendName,redEnvelope.anchorName))
                binding?.ll?.setBackgroundResource(R.mipmap.bg_red_notice)
                binding?.ivJump?.setOnClickListener(object:NoDoubleClickListener(){
                    override fun onClick() {
                        goToTargetRoom?.invoke(redEnvelope.anchorId)
                    }
                })
            }else{
                binding?.ivJump?.visibility = View.GONE
                binding?.marqueeTextView?.text = Html.fromHtml(AppContext.getContext().resources.getString(R.string.red_bg_piao_tiao).format(sendName))
                binding?.ll?.setBackgroundResource(R.mipmap.bg_red_notice)
            }
            val objectAnimator = ObjectAnimator.ofFloat(this@HongbaoPiaoTiaoView,"translationX",(DpPxUtil.getScreenWidth()+width).toFloat(),0f)
            objectAnimator.duration = 1000
            val alphaAnimator = ObjectAnimator.ofFloat(this@HongbaoPiaoTiaoView,"alpha",1f,1f)
            alphaAnimator.duration = 5000
            animatorSet = AnimatorSet()
            animatorSet?.play(alphaAnimator)?.after(objectAnimator)
            animatorSet?.start()
            animatorSet?.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {}
                override fun onAnimationEnd(animation: Animator) {
                    visibility = View.GONE
                    isBusy = false
                    queue.poll()
                    queue.peek()?.let {
                        startRedEnvelop(it)
                    }
                }

                override fun onAnimationCancel(animation: Animator) {}
                override fun onAnimationRepeat(animation: Animator) {}
            })
            binding?.marqueeTextView?.post {
                binding?.marqueeTextView?.startScroll()
            }
        }
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        clearAnim()
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
        clearAnim()
    }

    override fun closeRoomed() {
        mActivity = null
        clearAnim()
        SwagoRoomManager.removeListener(this)
    }

    private fun clearAnim(){
        queue.clear()
        animatorSet?.let {
            if(it.isRunning){
                it.cancel()
            }
            isBusy = false
        }
    }

}