package com.swago.room.audio.widget

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.model.live.audio.AudioSendGiftModel
import com.swago.room.R
import com.swago.room.databinding.ViewAudioContainGiftBinding

class AudioContainGiftView : ConstraintLayout, IRoomActiveListener {


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

    private var binding: ViewAudioContainGiftBinding? = null
    private var allAnimationSetList  = ArrayList<AnimatorSet>()
    private var allAudioGiftViewList  = ArrayList<AudioGiftView>()

    @Volatile
    private var currentModel: AudioSendGiftModel? = null
    var isBusy = false

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

    fun playGiftAnim(audioSendGiftModel: AudioSendGiftModel) {
        binding?.apply {
            isBusy = true
            clearAnimData()
            //默认初始化8个
            if (allAudioGiftViewList.isEmpty()){
                for (i in 0 until 8){
                    val audioGiftAnimView = AudioGiftView(context)
                    val layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
                        this.topToTop = R.id.clMultiAudio
                        this.bottomToBottom = R.id.clMultiAudio
                        this.startToStart = R.id.clMultiAudio
                        this.endToEnd = R.id.clMultiAudio
                    }
                    audioGiftAnimView.layoutParams = layoutParams
                    allAudioGiftViewList.add(audioGiftAnimView)
                }
            }
            //如果后续超过几个的话 就在添加几个
            if (audioSendGiftModel.receivers.size>allAudioGiftViewList.size){
                for (i in 0 until (audioSendGiftModel.receivers.size-allAudioGiftViewList.size)){
                    val audioGiftAnimView = AudioGiftView(context)
                    val layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
                        this.topToTop = R.id.clMultiAudio
                        this.bottomToBottom = R.id.clMultiAudio
                        this.startToStart = R.id.clMultiAudio
                        this.endToEnd = R.id.clMultiAudio
                    }
                    audioGiftAnimView.layoutParams = layoutParams
                    allAudioGiftViewList.add(audioGiftAnimView)
                }
            }

            audioSendGiftModel.receivers.forEachIndexed {position, positionBean ->
                //创建从屏幕中心弹出的动画view
                allAudioGiftViewList[position].setViewData(audioSendGiftModel.giftIcon, audioSendGiftModel.giftNum)
                clMultiAudio.addView(allAudioGiftViewList[position])

                //从中间缩放出来
                val alphaAnim = ObjectAnimator.ofFloat(allAudioGiftViewList[position], "alpha", 1f, 1f)
                val scaleX = ObjectAnimator.ofFloat(allAudioGiftViewList[position], "scaleX", 0f, 1f)
                val scaleY = ObjectAnimator.ofFloat(allAudioGiftViewList[position], "scaleY", 0f, 1f)
                val scaleFromCenter = AnimatorSet()
                scaleFromCenter.playTogether(scaleX,scaleY,alphaAnim)
                scaleFromCenter.setTarget(allAudioGiftViewList[position])
                scaleX.duration = 300
                scaleY.duration = 300

                //从中间向其他地方位移缩放
                val moveX = ObjectAnimator.ofFloat(allAudioGiftViewList[position],"translationX", positionBean.dx)
                val moveY = ObjectAnimator.ofFloat(allAudioGiftViewList[position],"translationY", positionBean.dy)
                val scaleXX = ObjectAnimator.ofFloat(allAudioGiftViewList[position],"scaleX",1f,0.2f)
                val scaleYY = ObjectAnimator.ofFloat(allAudioGiftViewList[position],"scaleY",1f,0.2f)
                val moveAndScale = AnimatorSet()
                moveAndScale.playTogether(moveX,moveY,scaleXX,scaleYY)
                moveAndScale.duration = 700

                val alphaAnimX = ObjectAnimator.ofFloat(allAudioGiftViewList[position], "alpha", 1f, 0f)
                alphaAnimX.duration = 200

                val moveAnimatorPlus = AnimatorSet()
                moveAnimatorPlus.play(alphaAnimX).after(moveAndScale)

                //在头像上方消失
                val alphaAnimEnd = ObjectAnimator.ofFloat(allAudioGiftViewList[position],"alpha",1f,0f)
                alphaAnimEnd.duration = 800
                //向头像上方移动
                val upYAnima = ObjectAnimator.ofFloat(allAudioGiftViewList[position],"translationY",positionBean.offSetY)
                val alphaAnimUp = ObjectAnimator.ofFloat(allAudioGiftViewList[position], "alpha", 0f, 1f)
                val upAnimatorSet = AnimatorSet()
                upAnimatorSet.playTogether(upYAnima,alphaAnimUp)
                upAnimatorSet.duration = 200

                val animationScaleAndMoveSet = AnimatorSet()
                animationScaleAndMoveSet.play(moveAnimatorPlus)
                    .after(scaleFromCenter)
                    .after(2300)

                val animatorSet = AnimatorSet()
                animatorSet.play(upAnimatorSet)
                    .after(animationScaleAndMoveSet)
                    .before(alphaAnimEnd)
                animatorSet.setTarget(allAudioGiftViewList[position])
                allAnimationSetList.add(animatorSet)

                animatorSet.addListener(object: Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        currentModel = null
                        isBusy = false
                    }

                    override fun onAnimationCancel(animation: Animator) {

                    }

                    override fun onAnimationRepeat(animation: Animator) {}

                })
            }
            allAnimationSetList.forEach {
                it.start()
            }
        }
    }
    

    private fun clearAnimData(){
        allAnimationSetList.forEach {
            it.cancel()
        }
        allAudioGiftViewList.forEach {
            it.clearImageView()
            //属性动画重置
            it.translationX = 0f
            it.translationY = 0f
        }
        allAnimationSetList.clear()
        binding?.clMultiAudio?.removeAllViews()
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        isBusy = false
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
       
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
       
    }

    override fun closeRoomed() {
        isBusy = false
        clearAnimData()
        SwagoRoomManager.removeListener(this)
    }

}