package com.mints.goodmoney.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.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mints.goodmoney.R
import com.mints.goodmoney.mvp.model.FriendHallMsgBean
import com.mints.goodmoney.ui.adapter.listener.OnItemClickListener
import com.mints.library.utils.GlideUtils

class InvitedAdapter(context: Context, invitedData: MutableList<FriendHallMsgBean.ListBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val HOLDER_TYPE_EMPTY = 0
        const val HOLDER_TYPE_INVITED = 1
    }

    private var mContext: Context = context
    private var invitedData: List<FriendHallMsgBean.ListBean> = invitedData

    private var mOnItemClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == HOLDER_TYPE_EMPTY) {
            val emptyView = LayoutInflater.from(parent.context).inflate(R.layout.item_empty, parent, false)
            return EmptyHolder(emptyView)
        }

        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_recy_invited, parent, false)
        return InvitedHolder(view)
    }

    override fun getItemCount(): Int {
        return if (invitedData.isEmpty()) {
            1
        } else {
            invitedData.size
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (invitedData.isEmpty()) {
            return HOLDER_TYPE_EMPTY
        }
        return HOLDER_TYPE_INVITED
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        if (viewHolder is EmptyHolder) {
            return
        }

        val holder = viewHolder as InvitedHolder

        when (position) {
            0 -> {
                holder.tvNum.visibility = View.GONE
                holder.ivRanking.visibility = View.VISIBLE
                holder.ivRanking.setImageResource(R.mipmap.icon_ranking_first)
            }
            1 -> {
                holder.tvNum.visibility = View.GONE
                holder.ivRanking.visibility = View.VISIBLE
                holder.ivRanking.setImageResource(R.mipmap.icon_ranking_second)
            }
            2 -> {
                holder.tvNum.visibility = View.GONE
                holder.ivRanking.visibility = View.VISIBLE
                holder.ivRanking.setImageResource(R.mipmap.icon_ranking_third)
            }
            else -> {
                holder.tvNum.visibility = View.VISIBLE
                holder.ivRanking.visibility = View.GONE
                holder.tvNum.text = "" + (position + 1)
            }
        }
        val data = invitedData[position]
        GlideUtils.loadCircleImageView(mContext, data.head, holder.ivAvatar, R.mipmap.ic_avatar_ph, R.mipmap.ic_avatar_ph)
        holder.tvName.text = data.name

        holder.tvCoin.text = "" + data.contribution

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(holder.itemView, position)
        }
    }

    inner class EmptyHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class InvitedHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvNum: TextView = itemView.findViewById(R.id.item_invited_tv_num)
        val ivAvatar: ImageView = itemView.findViewById(R.id.item_invited_iv_avatar)
        val ivRanking: ImageView = itemView.findViewById(R.id.item_invite_iv_ranking)
        val tvName: TextView = itemView.findViewById(R.id.item_invited_tv_name)
        val tvCoin: TextView = itemView.findViewById(R.id.item_invited_tv_coin)
    }

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

}