package com.swago.room.danmu

import android.animation.Animator
import android.animation.ObjectAnimator
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
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.im.DanMuBean
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.SwagoInfo
import com.swago.loadUrl
import com.swago.room.databinding.ViewDanMuBinding
import java.util.concurrent.ConcurrentLinkedQueue

class DanMuView : ConstraintLayout,IRoomActiveListener {

    private var binding : ViewDanMuBinding? = null
    private var isBusy = false
    private var objectAnimator:ObjectAnimator? = null

    private var queue = ConcurrentLinkedQueue<DanMuBean>()

    private var mActivity: Activity? = 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 = ViewDanMuBinding.inflate(LayoutInflater.from(context), this, true)
    }

    fun addQueue(danMuBean: DanMuBean){
        queue.add(danMuBean)
        if (!isBusy){
            startDanMu(danMuBean)
        }
    }

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


    private fun startDanMu(danMuBean: DanMuBean){
        binding?.apply {
            isBusy = true
            visibility = View.VISIBLE
            mActivity?.let { ivAvatar.loadUrl(it,danMuBean.senderAvatar) }
            tvContent.text = danMuBean.content
            tvLevel.setUserLevel(danMuBean.senderLevel)
            tvName.text = danMuBean.senderName
            if (SwagoInfo.isMirror()){
                objectAnimator = ObjectAnimator.ofFloat(this@DanMuView,"translationX",(DpPxUtil.getScreenWidth()+width).toFloat(),-width.toFloat())
            }else{
                objectAnimator = ObjectAnimator.ofFloat(this@DanMuView,"translationX",(DpPxUtil.getScreenWidth()+width).toFloat(),-width.toFloat())
            }
            objectAnimator?.duration = 5000
            objectAnimator?.start()
            objectAnimator?.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {}
                override fun onAnimationEnd(animation: Animator) {
                    visibility = View.GONE
                    isBusy = false
                    queue.poll()
                    queue.peek()?.let {
                        startDanMu(it)
                    }
                }

                override fun onAnimationCancel(animation: Animator) {}
                override fun onAnimationRepeat(animation: Animator) {}
            })


        }
    }

    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()
        objectAnimator?.let {
            if(it.isRunning){
                it.cancel()
            }
            isBusy = false
        }
    }

}