package com.base.browserwhite.ui.activity.search

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.browserwhite.R
import com.base.browserwhite.bean.SearchRecordBean
import com.base.browserwhite.databinding.ItemSearchrecordBinding
import com.base.browserwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter


class SearchRecordAdapter : BaseQuickAdapter<SearchRecordBean, SearchRecordAdapter.SearchRecordViewHolder>() {
    inner class SearchRecordViewHolder(view: View) : ViewHolder(view) {

    }

    var jumpAction: ((content: String) -> Unit)? = null
    var deleteAction: (() -> Unit)? = null

    override fun onBindViewHolder(holder: SearchRecordViewHolder, position: Int, item: SearchRecordBean?) {
        if (item == null) return

        val binding = ItemSearchrecordBinding.bind(holder.itemView)
        binding.ivDelete.isVisible = item.isDelete
        binding.tvContent.isVisible = !item.isDelete
        binding.tvContent.text = item.content

        binding.tvContent.setOnClickListener {
            jumpAction?.invoke(item.content)
        }
        binding.ivDelete.setOnClickListener {
            deleteAction?.invoke()
        }
        if (!item.isDelete) {
            showItemAnimation(binding.root)
        }
    }


    private fun showItemAnimation(view: View) {
        // 创建一个动画集合
        val animatorSet = AnimatorSet()

        // 创建一个属性动画，改变透明度
        val alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)
        alphaAnimator.setDuration(500) // 设置动画持续时间
        alphaAnimator.interpolator = AccelerateDecelerateInterpolator() // 设置加速器
//        alphaAnimator.start()

        // 创建一个属性动画，先往右边移动100像素，然后回到原位置
        val moveRightAnimator = ObjectAnimator.ofFloat(view, "translationX", 10f, 0f)
        moveRightAnimator.setDuration(500) // 设置动画持续时间为1000毫秒

        moveRightAnimator.interpolator = AccelerateDecelerateInterpolator() // 设置插值器为先加速后减速
//        moveRightAnimator.start()

        animatorSet.playTogether(alphaAnimator, moveRightAnimator)
        animatorSet.start()
    }


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