package com.jld.kotilntest.room.adapter

import android.animation.ObjectAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.jld.vod.R
import com.jld.vod.model.bean.GameBean


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2019/11/19 13:42
 */
class GameAdapter :
    PagingDataAdapter<GameBean, GameAdapter.LightSwitchViewHolder>(DIFF_CALLBACK) {
    private var itemClickListener: IGameItemClickListener? = null

    override fun onBindViewHolder(holder: LightSwitchViewHolder, position: Int) {
        val concert: GameBean? = getItem(position)

        // Note that "concert" is a placeholder if it'rank_mark null.
        holder.bindTo(concert)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LightSwitchViewHolder {
        return LightSwitchViewHolder(parent)
    }

    companion object {
        var isShowDeletes = false
        private var isUnistallApp = false
        private lateinit var localObjectAnimator: ObjectAnimator
        private lateinit var sAnimator: ObjectAnimator

        private val DIFF_CALLBACK = object :
            DiffUtil.ItemCallback<GameBean>() {
            //是否为同一个Item
            override fun areContentsTheSame(oldItem: GameBean, newItem: GameBean): Boolean {
                // Log.e("tag","areContentsTheSame"+Thread.currentThread().name+ oldItem.titleName +"  new :"+newItem.titleName)
                return oldItem == newItem
            }

            //数据内容是否发生变化
            override fun areItemsTheSame(oldItem: GameBean, newItem: GameBean): Boolean {
                // Log.e("tag","areItemsTheSame"+ oldItem.titleName +"  new :"+newItem.titleName)
                return oldItem == newItem
            }
        }
    }

    inner class LightSwitchViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(

        LayoutInflater.from(parent.context).inflate(
            R.layout.item_game_recyclerview, parent, false)) {
        var img = itemView.findViewById<AppCompatImageView>(R.id.aciv_item_game_pic)
        var tv = itemView.findViewById<TextView>(R.id.tv_game_title)
        var ll = itemView.findViewById<LinearLayout>(R.id.ll_game)
        var iv_del = itemView.findViewById<ImageView>(R.id.iv_grid_img_del)
        fun bindTo(gameBean: GameBean?) {

            Glide.with(itemView).load(com.jld.vod.config.Config.ApiBaseUrl+"/synchronization/previewImg?imgUrl="+gameBean!!.imgUrl)
                .skipMemoryCache(true).diskCacheStrategy(
                    DiskCacheStrategy.ALL).into(img)
            tv.text = gameBean?.name

            if (isShowDeletes) {
                iv_del.setVisibility(View.VISIBLE)
            } else {
                iv_del.setVisibility(View.GONE)
            }

            ll.setOnClickListener {
                //if (!isShowDeletes) {
                    gameBean!!.install?.let { it1 -> itemClickListener!!.onItemClickListener(it1) }
               // }
            }
//            //长按卸载
//            ll.setOnLongClickListener(object :View.OnLongClickListener{
//                override fun onLongClick(v: View?): Boolean {
//                    if (isShowDeletes) {
//                        isShowDeletes = false;
//                    } else {
//                        isShowDeletes = true;
//                    }
//                    setShowDelete(isShowDeletes) //通知是否显示卸载按钮
//
//                    setUninstallApp(true)
//
//                    return false
//                }
//            })
//            if (isUnistallApp) {
//                showShakeAnim(ll)
//            }
        }
    }
    fun setOnGameClickListener(itemClickListener: IGameItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    interface IGameItemClickListener {
        fun onItemClickListener(
            install: String
        )
    }
//
//    fun setShowDelete(isShowDelete: Boolean) {
//        isShowDeletes = isShowDelete
//        notifyDataSetChanged()
//    }
//    fun setUninstallApp(paramBoolean: Boolean) {
//        isUnistallApp = paramBoolean
//    }
//
//    /**
//     * 卸载抖动动画
//     *
//     * @param paramView
//     * @param paramInt
//     */
//    private fun showShakeAnim(paramView: View) {
//        localObjectAnimator = ObjectAnimator.ofFloat(paramView, "rotation", *floatArrayOf(-2.0f, 2.0f))
//        localObjectAnimator.setDuration(100L)
//        localObjectAnimator.setInterpolator(AccelerateDecelerateInterpolator())
//        localObjectAnimator.setRepeatCount(-1)
//        localObjectAnimator.setRepeatMode(ObjectAnimator.REVERSE)
//        localObjectAnimator.start()
//        sAnimator = ObjectAnimator.ofFloat(paramView, "x", *floatArrayOf(paramView.x - 0.5f, paramView.x + 0.5f))
//        //         this.mInitX = paramView.getX();
//        sAnimator.setDuration(100L)
//        sAnimator.setInterpolator(AccelerateDecelerateInterpolator())
//        sAnimator.setRepeatCount(Int.MAX_VALUE)
//        sAnimator.setRepeatMode(ObjectAnimator.REVERSE)
//        sAnimator.start()
//    }
}