package com.ym.game.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Activity
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ym.library.listener.OnRecycleItemClickListener
import com.ym.library.base.BaseRecyclerAdapter
import com.ym.library.utils.Utils
import com.ym.xynt.R
import com.ym.library.module.NewsEntity

/**
 *Craeted by ${junqi.li}
 *Craeted by 2020/9/18
 */
class WelfareTaskAdpater :
    BaseRecyclerAdapter<NewsEntity, WelfareTaskAdpater.WelfareTaskHolder> {

    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<NewsEntity>

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

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

    override fun onBindViewHolder(holder: WelfareTaskHolder, position: Int) {
        if (mList.size > 0) {
            val data = mList[position]
            holder.tvTitle.text = data.title
            holder.tvSubtitle.text = data.description

            if (data?.awardRangeStart != null && data?.awardRangeEnd != null) {
                holder.tvCount.text = "+${data?.awardRangeStart}~${data?.awardRangeEnd}"
            } else {
                holder.tvCount.text = "+${data.coins}"
            }

            if (data?.adRenderStyle != null && data?.adRenderStyle == 3) {
//                holder?.layoutBg.setBackgroundResource(R.mipmap.icon_bg_fuli_item)
                holder?.layoutBg.setBackgroundResource(R.drawable.withdraw_bg)
            } else if (data?.adRenderStyle != null && data?.adRenderStyle == 2) {
//                holder?.layoutBg.setBackgroundResource(R.mipmap.icon_bg_fuli_item)
                holder?.layoutBg.setBackgroundResource(R.drawable.withdraw_bg)
            } else {
//                holder?.layoutBg.setBackgroundResource(R.mipmap.icon_bg_fuli_item_white)
                holder?.layoutBg.setBackgroundResource(R.drawable.withdraw_bg)
            }

            if (data?.buttonSway != null && data?.buttonSway == 1) {
                startAnimator(holder?.layoutStatus)
            } else {
                resetAnimator(holder?.layoutStatus)
            }

            if (data?.adMarkImage != null && "" != data?.adMarkImage) {
                holder?.ivTitle?.visibility = View.VISIBLE
                Glide.with(mContext!!).load(data?.adMarkImage)
                    .into(holder?.ivTitle)
            } else {
                holder?.ivTitle?.visibility = View.GONE
            }

            holder.ivStatus.setOnClickListener {
                if (Utils.isFastClick()) {
                    listener.onItemClick(it, position, mList[position])
                }
            }

            holder?.itemView?.setOnClickListener {
                listener.onItemClick(it, position, mList[position])
            }

            // 0未完成 1已领取 2可领取
            when (data.status) {
                0 -> {
                    if (data?.adRenderStyle != null && data?.adRenderStyle == 3) {
                        holder.ivStatus.setImageResource(R.drawable.icon_item_welfare_task_complete)
                    } else {
                        holder.ivStatus.setImageResource(R.drawable.icon_item_welfare_task_complete)
                    }
                   // holder?.tvStatus.text = "去完成"
                }
                1 -> {
                    resetAnimator(holder?.layoutStatus)
                    holder.ivStatus.setImageResource(R.drawable.icon_item_welfare_task_receiveed)
                   // holder?.tvStatus.text = "已领取"
                }
                2 -> {
                    holder.ivStatus.setImageResource(R.drawable.icon_item_welfare_task_receive)
                   // holder?.tvStatus.text = "立即领取"
                }
            }
        }
    }

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

    inner class WelfareTaskHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val layoutBg: RelativeLayout = itemView.findViewById(R.id.layout_welfare_item_bg)
        val tvTitle: TextView = itemView.findViewById(R.id.tv_welfare_item_title)
        val tvSubtitle: TextView = itemView.findViewById(R.id.tv_welfare_item_subtitle)
        val tvCount: TextView = itemView.findViewById(R.id.tv_welfare_item_count)
        val tvStatus: TextView = itemView.findViewById(R.id.tv_welfare_item_status)
        val ivStatus: ImageView = itemView.findViewById(R.id.iv_welfare_item_status)
        val ivTitle: ImageView = itemView.findViewById(R.id.iv_welfare_item_title)
        val layoutStatus: RelativeLayout = itemView.findViewById(R.id.layout_welfare_item_status)
    }

    private var animatorList: MutableList<ObjectAnimator> = ArrayList()
    private var viewList: MutableList<View> = ArrayList()

    private fun startAnimator(view: View) {
        val oa: ObjectAnimator = ObjectAnimator.ofFloat(
            view,
            View.ROTATION,
            0F, 8F, -8F, 0F, 0F, 0F, 0F, 0F, 0F
        )
        oa.repeatCount = ValueAnimator.INFINITE
        oa.setDuration(1000).start()
        animatorList.add(oa)
        viewList.add(view)
    }

    private fun resetAnimator(view: View) {
        val oa: ObjectAnimator = ObjectAnimator.ofFloat(
            view,
            View.ROTATION,
            0F, 0F
        )
        oa.repeatCount = ValueAnimator.INFINITE
        oa.setDuration(1000).start()
        animatorList.add(oa)
        viewList.add(view)
    }


    fun clearAnimation() {
        if (animatorList.size > 0) {
            for (animator in animatorList) {
                animator?.cancel()
            }
            animatorList.clear()
        }
        if (viewList.size > 0) {
            for (view in viewList) {
                view?.clearAnimation()
            }
            viewList.clear()
        }
    }

}