package com.base.scanqrclear.ui.main

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.FunctionUIBean
import com.base.scanqrclear.databinding.ItemFunctionBinding
import com.base.scanqrclear.ui.adapter.CommonViewHolder
import com.base.scanqrclear.utils.AppPreferences
import com.base.scanqrclear.utils.LogEx
import com.base.scanqrclear.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class FunctionAdapter : BaseQuickAdapter<FunctionUIBean, CommonViewHolder>() {

    private val TAG = "FunctionAdapter"
    var itemClick: ((key: String) -> Unit)? = null
    var isGuide: Boolean = false

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: FunctionUIBean?) {
        item ?: return
        val binding = ItemFunctionBinding.bind(holder.itemView)
        binding.iv.setImageResource(item.icon)
        binding.tvDesc.text = item.desc

        kotlin.runCatching {
            if (binding.fl.tag is AnimatorSet) {
                (binding.fl.tag as AnimatorSet).cancel()
                LogEx.logDebug(TAG, "AnimatorSet cancel")
            }
        }

        LogEx.logDebug(TAG, "isGuide=$isGuide showGuide=${item.showGuide} isPlaying=${item.isPlaying}")
        if (!isGuide) {
            if (item.showGuide) {
                isGuide = true
                item.isPlaying = true
                showHeartAnimation(binding.fl)
            }
        }
        binding.root.setOnClickListener {
            itemClick?.invoke(item.key)
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
        return CommonViewHolder(R.layout.item_function.inflate(parent))
    }


    private fun showHeartAnimation(view: View): AnimatorSet {
        val scaleXAnimator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.75f, 1f)
        val scaleYAnimator = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.75f, 1f)

        scaleXAnimator.duration = 800 // 动画持续时间，可根据需要调整
        scaleYAnimator.duration = 800

        scaleXAnimator.interpolator = AccelerateDecelerateInterpolator()
        scaleYAnimator.interpolator = AccelerateDecelerateInterpolator()

        scaleXAnimator.repeatCount = ObjectAnimator.INFINITE // 设置动画无限循环
        scaleYAnimator.repeatCount = ObjectAnimator.INFINITE

        val animatorSet = AnimatorSet()
        animatorSet.playTogether(scaleXAnimator, scaleYAnimator) // 同时播放两个动画
        animatorSet.start()

        view.tag = animatorSet

        return animatorSet
    }


    @SuppressLint("NotifyDataSetChanged")
    fun changeGuide() {
        isGuide = false
        items.forEach { it.showGuide = AppPreferences.getInstance().getBoolean("${it.key}_guide", true) }
        notifyDataSetChanged()
    }

}