package com.gh.gamecenter.forum.home.follow.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.debounceActionWithInterval
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.RecyclerFollowRecommendBinding
import com.gh.gamecenter.eventbus.EBUserFollow
import com.gh.gamecenter.feature.entity.UserEntity
import com.gh.gamecenter.forum.home.follow.ResetDataChangeHelper

class FollowRecommendListAdapter(
    context: Context,
    private val listener: OnChildEventListener
) : RecyclerView.Adapter<FollowRecommendListAdapter.RecommendViewHolder>() {

    private val resetDataChangeHelper = ResetDataChangeHelper<UserEntity>(context, this)

    fun submitList(data: List<UserEntity>) {
        resetDataChangeHelper.submitList(data) {
            it.id ?: ""
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecommendViewHolder {
        return RecommendViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int =
        resetDataChangeHelper.dataList.size

    override fun onBindViewHolder(holder: RecommendViewHolder, position: Int) {
        val item = resetDataChangeHelper.dataList[position]
        val context = holder.itemView.context

        holder.binding.ivIcon.displayGameIcon(item.icon, null)

        holder.binding.tvFollowerCount.text = if (item.count.fans > NUMBER_MAX) {
            context.getString(R.string.fans_with_number_max)
        } else {
            context.getString(R.string.fans_with_number, item.count.fans)
        }

        val createCount = item.count.question + item.count.communityArticle + item.count.video
        holder.binding.tvArticleCount.text = if (createCount > NUMBER_MAX) {
            context.getString(R.string.creations_with_number_max)
        } else {
            context.getString(R.string.creations_with_number, createCount)
        }
        holder.binding.tvSign.text = if (item.introduce.isBlank()) {
            context.getString(R.string.user_default_introduce)
        } else {
            item.introduce
        }

        holder.binding.tvFollow.text = if (item.isFollowed) {
            context.getString(R.string.concerned)
        } else {
            context.getString(R.string.menu_concern)
        }

        holder.itemView.background = R.drawable.bg_shape_recycler_follow_recommend.toDrawable(context)

        holder.itemView.setOnClickListener {
            listener.onItemClick(position, item.id ?: "")
        }

        holder.binding.tvFollow.setOnClickListener {
            debounceActionWithInterval(it.id, 1000) {
                listener.onFollowClick(position, item.id ?: "", !item.isFollowed)
            }
        }
    }

    fun updateFollowed(change: EBUserFollow) {
        val position = resetDataChangeHelper.dataList.indexOfFirst { it.id == change.userId }
        if (position != -1) {
            val item = resetDataChangeHelper.dataList[position]
            item.isFollowed = change.isFollow
            notifyItemChanged(position)
        }
    }

    companion object {

        private const val NUMBER_MAX = 99
    }

    class RecommendViewHolder(val binding: RecyclerFollowRecommendBinding) : ViewHolder(binding.root)

    interface OnChildEventListener {

        fun onItemClick(childPosition: Int, userId: String)

        fun onFollowClick(childPosition: Int, userId: String, isFollow: Boolean)
    }
}