package com.adealink.weparty.room.gift.adapter

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.log.Log
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dpf
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.ItemRoomGiftBinding
import com.adealink.weparty.room.gift.data.RoomGiftItemData
import com.adealink.weparty.util.formatNumber
import java.lang.ref.WeakReference

class RoomGiftItemViewBinder(private val listener: (RoomGiftItemData) -> Unit) :
    ItemViewBinder<RoomGiftItemData, RoomGiftItemViewBinder.ViewHolder>() {

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ViewHolder {
        return ViewHolder(ItemRoomGiftBinding.inflate(inflater, parent, false)).apply {
            initView()
        }
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        item: RoomGiftItemData
    ) {
        holder.update(item)
    }

    inner class ViewHolder(
        rootBinding: ItemRoomGiftBinding
    ) : BindingViewHolder<ItemRoomGiftBinding>(rootBinding) {

        private var item: RoomGiftItemData? = null

        fun initView() {
            binding.root.onClick {
                if (item?.isSelected == true) {
                    playClickAnim(binding.ivIcon)
                }
                item?.let {
                    listener.invoke(it)
                }
            }
        }

        fun update(item: RoomGiftItemData) {
            this.item = item
            binding.ivIcon.setImageUrl(item.giftInfo.resource?.icon)
            binding.tvName.text = item.giftInfo.resource?.getDisplayName()
            binding.tvPrice.text = formatNumber(item.giftInfo.price, false)
            if (item.isSelected) {
                binding.root.background = getCompatDrawable(R.drawable.room_gift_selected_bg)
            } else {
                binding.root.background = null
            }
            if (item.isSelected) {
                playClickAnim(binding.ivIcon)
            }
        }
    }

    private fun playClickAnim(iconView: View) {
        GiftIconAnimator(WeakReference(iconView)).start()
    }

    class GiftIconAnimator(val viewRef: WeakReference<View?>) {

        private var animatorSet = AnimatorSet()

        private val view: View?
            get() {
                return viewRef.get()
            }

        init {
            view?.let {
                val translationY = ObjectAnimator.ofFloat(it, View.TRANSLATION_Y, (-4f).dpf(), 0f, (-2.5f).dpf(), 0f).apply {
                    duration = 800
                }
                animatorSet.play(translationY)
            }
        }

        fun start() {
            if (animatorSet.isRunning) {
                return
            }

            animatorSet.start()
        }

        fun cancel() {
            animatorSet.cancel()
            view?.scaleX = 1F
            view?.scaleY = 1f
        }
    }
}