package com.gh.gamecenter.qa.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.common.util.*
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.utils.fromHtml
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.core.utils.HtmlUtils
import com.gh.gamecenter.databinding.ForumItemBinding
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.entity.ForumEntity

class ChooseForumContainerAdapter(
    content: Context,
    val type: String,
    val viewModel: ChooseForumContainerViewModel,
    val onSelectCallback: ((entity: CommunityEntity) -> Unit)? = null
) : ListAdapter<ForumEntity>(content) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                FooterViewHolder(mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false))
            }
            else -> {
                ForumItemViewHolder(parent.toBinding())
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNotEmpty()) {
            if (type != ChooseForumContainerFragment.ChooseForumType.ATTENTION.value) {
                mEntityList.size + FOOTER_ITEM_COUNT
            } else {
                mEntityList.size
            }
        } else 0
    }

    override fun getItemViewType(position: Int): Int {
        if (type != ChooseForumContainerFragment.ChooseForumType.ATTENTION.value) {
            if (position == itemCount - 1) return ItemViewType.ITEM_FOOTER
            return ItemViewType.ITEM_BODY
        } else {
            return ItemViewType.ITEM_BODY
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ForumItemViewHolder) {
            val forumEntity = mEntityList[position]
            holder.binding.forumNameTv.text = forumEntity.name.fromHtml()
            holder.binding.followTv.background =
                if (forumEntity.isFollow) R.drawable.button_round_gray_light.toDrawable() else R.drawable.bg_forum_follow.toDrawable()
            holder.binding.followTv.text = if (forumEntity.isFollow) "已关注" else "关注"
            holder.binding.followTv.setTextColor(
                if (forumEntity.isFollow) R.color.text_subtitleDesc.toColor(mContext) else R.color.theme_font.toColor(
                    mContext
                )
            )

            val icon = forumEntity.icon.ifEmpty { forumEntity.game.getIcon() }
            holder.binding.forumIcon.displayGameIcon(icon, forumEntity.game.iconSubscript, forumEntity.game.iconFloat)
            holder.binding.followTv.visibility = View.GONE
            holder.itemView.setOnClickListener {
                val tabType = if (type == ChooseForumContainerFragment.ChooseForumType.SEARCH.value) "论坛tab" else ""
                val bbsType = if (forumEntity.type == "official_bbs") "综合论坛" else "游戏论坛"
                NewLogUtils.logForumSearchResultClick(
                    tabType,
                    forumEntity.id,
                    bbsType,
                    "",
                    "",
                    "",
                    viewModel.searchKey,
                    position + 1
                )
                onSelectCallback?.invoke(
                    CommunityEntity(
                        forumEntity.id,
                        HtmlUtils.stripHtml(forumEntity.name),
                        type = forumEntity.type,
                        game = forumEntity.game.toCommunityGameEntity(),
                        icon = icon,
                        iconSubscript = forumEntity.game.iconSubscript
                    )
                )
            }
        } else if (holder is FooterViewHolder) {
            holder.initItemPadding()
            holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            holder.hint.textSize = 12f
            holder.hint.setTextColor(ContextCompat.getColor(mContext, R.color.aaaaaa))
        }
    }

    class ForumItemViewHolder(val binding: ForumItemBinding) : BaseRecyclerViewHolder<ForumEntity>(binding.root)
}