package com.duben.microtribe.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.duben.library.utils.GlideUtils
import com.duben.microtribe.R
import com.duben.microtribe.mvp.model.VedioBean

class TopAdapter(val topList: MutableList<VedioBean>?) :
    RecyclerView.Adapter<TopAdapter.ViewHolder>() {

    lateinit var context: Context

    lateinit var mOnItemClickListener: OnItemClickListener

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val rlTopItemRoot: LinearLayout = view.findViewById(R.id.rlTopItemRoot)
        val ivTopItem: ImageView = view.findViewById(R.id.ivTopItem)
        val tvTopItemLable: TextView = view.findViewById(R.id.tvTopItemLable)
        val tvTopItemCount: TextView = view.findViewById(R.id.tvTopItemCount)
        val tvTopItemTitle: TextView = view.findViewById(R.id.tvTopItemTitle)
        val tvTopItemText: TextView = view.findViewById(R.id.tvTopItemText)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        val view = LayoutInflater.from(context).inflate(R.layout.item_rv_top, parent, false)
        val viewHolder = ViewHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val topBean = topList?.get(position)
        if (topBean != null) {
            holder.tvTopItemLable.text = "${position + 1}"
            holder.tvTopItemCount.text = topBean.showNum
            holder.tvTopItemTitle.text = "${topBean.title}"
            if (topBean.orderTags != null && topBean.orderTags.size > 0) {
                val sb = StringBuilder()
                for (i in 0 until topBean.orderTags.size) {
                    sb.append(topBean.orderTags.get(i) + " ")
                }
                holder.tvTopItemText.text = "${sb}"
            }

            GlideUtils.loadImageViewGifForCenterCrop(
                context,
                topBean.coverImage,
                holder.ivTopItem
            )
        }

        when (position) {
            0 -> {
                holder.tvTopItemLable.setBackgroundResource(R.mipmap.y43)
            }
            1 -> {
                holder.tvTopItemLable.setBackgroundResource(R.mipmap.y42)
            }
            2 -> {
                holder.tvTopItemLable.setBackgroundResource(R.mipmap.y41)
            }
            else -> {
                holder.tvTopItemLable.setBackgroundResource(R.mipmap.y4)
            }
        }

        holder.rlTopItemRoot.setOnClickListener {
            mOnItemClickListener.onItemClick(position)
        }


        holder.itemView.setTag(position)
    }

    override fun getItemCount() = topList?.size ?: 0

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }
}