package com.ym.game.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.ym.ddhy.R
import com.ym.library.base.BaseRecyclerAdapter
import com.ym.library.listener.OnRecycleItemClickListener
import com.ym.library.module.DiamondEntity
import com.ym.library.module.NewsEntity
import com.ym.library.utils.Utils

/**
 *Craeted by ${junqi.li}
 *Craeted by 2020/9/18
 */
class WelfareDiamondAdapter :
    BaseRecyclerAdapter<DiamondEntity, WelfareDiamondAdapter.ViewHolder> {

    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<DiamondEntity>

    constructor(
        content: Context?, listener: OnRecycleItemClickListener<DiamondEntity>
    ) : super() {
        this.listener = listener
        mContext = content
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (mList.size > 0) {
            val data = mList[position]
            if (data.show == 1) {
                holder?.ivStatus.setImageResource(R.drawable.icon_item_welfare_diamond)
            } else {
                holder?.ivStatus.setImageResource(R.drawable.icon_item_welfare_diamond_gray)
            }

            if (position == mList.size - 1) {
                if (data.coins!! >= data.max!!) {
                    holder?.ivStatus.setImageResource(R.drawable.icon_welfare_box_open)
                    val oa: ObjectAnimator = ObjectAnimator.ofFloat(
                        holder.ivStatus,
                        View.ROTATION,
                        0F, 30F, -25F, 0F, 0F, 0F, 0F, 0F, 0F
                    )
                    oa.repeatCount = ValueAnimator.INFINITE
                    oa.setDuration(2000).start()
                } else {
                    holder?.ivStatus.clearAnimation()
                    holder?.ivStatus.setImageResource(R.drawable.icon_welfare_box_close)
                }
                holder?.ivDottedLine.visibility = View.GONE
                holder?.itemView?.setOnClickListener {
                    if (data.coins!! >= data.max!!){
                        listener?.onItemClick(it,position,mList[position])
                    } else {
                        Utils.showToast(mContext,"钻石不足")
                    }
                }
            } else {
                holder?.ivDottedLine.visibility = View.VISIBLE
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_welfare_diamond, parent, false)
        )
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivStatus: ImageView = itemView.findViewById(R.id.iv_item_diamond_status)
        val ivDottedLine: ImageView = itemView.findViewById(R.id.iv_item_dotted_line)
    }
}