package com.swago.room.giftdandao

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.BounceInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.FrameLayout
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.ViewItemGiftBinding

/**
 *@date 2022/1/22 16:57
 *description:
 */
class GiftDanDaoView : FrameLayout {

    var isBusy = false
    private var binding : ViewItemGiftBinding? = null


    var imCurrentGiftModel:IMGiftModel? = null

    constructor(context: Context) : super(context){
        initView(context)
    }

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

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

    fun addData(imGiftModel: IMGiftModel){
        isBusy = true
        binding?.apply {
            Glide.with(ivAvatar.context)
                .load(imGiftModel.senderAvatar)
                .placeholder(R.mipmap.default_avatar)
                .error(R.mipmap.default_avatar)
                .into(ivAvatar)
            Glide.with(ivAvatar.context)
                .load(imGiftModel.giftIcon)
                .placeholder(R.mipmap.default_avatar)
                .error(R.mipmap.default_avatar)
                .into(ivGiftIcon)

            tvSenderName.text = imGiftModel.senderName
            tvSenderTo.text = AppContext.getContext().resources.getString(R.string.send_to).format(imGiftModel.receiverName)
            tvNum.text = "x${imGiftModel.giftNum}"
            clItemGift.visibility = View.VISIBLE
            if (imCurrentGiftModel!=null&&imGiftModel.isCombo==1){

            }else{
                //执行进场动画
                giftEnterAnimator.start()
            }
            imCurrentGiftModel = imGiftModel

            clItemGift.removeCallbacks(showRunnable)
            clItemGift.postDelayed(showRunnable, 3000)
            
            if (imGiftModel.multiple>500){
                clWinPrize.visibility = View.VISIBLE
                tvWinPrize.text = AppContext.getContext().resources.getString(R.string.win_num_prize).format(imGiftModel.multiple)
                clWinPrize.removeCallbacks(winPrizeRunnable)
                clWinPrize.postDelayed(winPrizeRunnable, 1000)
            }
        }


    }


    private val giftEnterAnimator by lazy {
        val tx = ObjectAnimator.ofFloat(binding!!.clItemGift, "translationX", -binding!!.clItemGift.width.toFloat(), 0f)
        val ap = ObjectAnimator.ofFloat(binding!!.clItemGift, "alpha", 0.5f, 1f)
        tx.interpolator = DecelerateInterpolator()
        tx.duration = 400
        tx.repeatCount = 0
        ap.duration = 200
        ap.repeatCount = 0
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(tx, ap)
        animatorSet
    }

    private val showRunnable by lazy {
        Runnable {
            clear()
            binding?.apply {
                clItemGift.translationX = -(clItemGift.width).toFloat()
            }
        }
    }

    private val winPrizeRunnable by lazy {
        Runnable {
            binding!!.clWinPrize.visibility = View.INVISIBLE
            binding!!.tvWinPrize.text=""
        }
    }


    fun clear() {
        isBusy = false
        imCurrentGiftModel=null
        binding?.apply {
            clWinPrize.visibility = View.INVISIBLE
            clItemGift.visibility = View.INVISIBLE
            clWinPrize.removeCallbacks(winPrizeRunnable)
            clItemGift.removeCallbacks(showRunnable)
        }
    }

}