package com.gh.gamecenter.forum.list

import android.content.Context
import android.view.View
import android.view.ViewGroup
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.core.utils.ToastUtils
import com.gh.gamecenter.common.utils.debounceActionWithInterval
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.databinding.ForumMyFollowBinding
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.eventbus.EBForumFollowChange
import com.gh.gamecenter.forum.detail.ForumDetailActivity
import org.greenrobot.eventbus.EventBus

class ForumListAdapter(
    context: Context,
    val entrance: String,
    val mViewModel: ForumListViewModel?
) : ListAdapter<ForumEntity>(context) {

    override fun getItemCount(): Int {
        return if (mEntityList.isNullOrEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            itemCount - 1 -> ItemViewType.ITEM_FOOTER
            else -> ItemViewType.ITEM_BODY
        }
    }

    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 onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ForumItemViewHolder -> {
                holder.binding.run {
                    root.setBackgroundColor(R.color.background_white.toColor(mContext))
                    forumNameTv.setTextColor(R.color.text_title.toColor(mContext))
                    topLine.setBackgroundColor(R.color.divider.toColor(mContext))

                    val forumEntity = mEntityList[position]
                    forumNameTv.text = forumEntity.name
                    if (forumEntity?.type == "official_bbs") {
                        forumIcon.displayGameIcon(forumEntity.icon, null)
                    } else {
                        forumIcon.displayGameIcon(
                            forumEntity.game.getIcon(),
                            forumEntity.game.iconSubscript,
                            forumEntity.game.iconFloat
                        )
                    }
                    topLine.visibility = if (position == 0) View.GONE else View.VISIBLE

                    if (mViewModel?.type == ForumListActivity.TYPE_FOLLOW) {
                        moreIv.visibility = View.VISIBLE
                        followTv.visibility = View.GONE
                    } else {
                        moreIv.visibility = View.GONE
                        followTv.visibility = View.VISIBLE

                        followTv.run {
                            text = if (forumEntity.me.isFollowForum) {
                                background = R.drawable.button_round_gray_light.toDrawable(mContext)
                                setTextColor(R.color.text_subtitleDesc.toColor(mContext))
                                "已关注"
                            } else {
                                background = R.drawable.button_round_primary_light.toDrawable(mContext)
                                setTextColor(R.color.theme_font.toColor(mContext))
                                "关注"
                            }
                        }

                        followTv.setOnClickListener {
                            debounceActionWithInterval(R.id.followTv) {
                                CheckLoginUtils.checkLogin(mContext, entrance) {
                                    if (forumEntity.me.isFollowForum) {
                                        mViewModel?.unFollowForum(forumEntity.id) {
                                            forumEntity.me.isFollowForum = false
                                            followTv.setBackgroundResource(R.drawable.button_round_primary_light)
                                            followTv.setTextColor(R.color.theme_font.toColor(mContext))
                                            followTv.text = "关注"
                                            EventBus.getDefault().post(EBForumFollowChange(forumEntity, false))
                                        }
                                    } else {
                                        mViewModel?.followForum(forumEntity.id) {
                                            forumEntity.me.isFollowForum = true
                                            followTv.setBackgroundResource(R.drawable.button_round_gray_light)
                                            followTv.setTextColor(R.color.text_subtitleDesc.toColor(mContext))
                                            followTv.text = "已关注"
                                            ToastUtils.showToast("关注成功")
                                            EventBus.getDefault().post(EBForumFollowChange(forumEntity, true))
                                        }
                                    }
                                }
                            }
                        }
                    }

                    root.setOnClickListener {
                        var event = ""
                        var location = ""
                        when (mViewModel?.type) {
                            ForumListActivity.TYPE_FOLLOW -> {
                                event = "click_followforum_forum"
                                location = "关注论坛页"
                            }

                            ForumListActivity.TYPE_HOT -> {
                                event = "click_hotforum_forum"
                                location = "热门论坛页"
                            }

                            ForumListActivity.TYPE_OFFICIAL -> {
                                event = "click_multipleforum_forum"
                                location = "综合论坛页"
                            }
                        }
                        val bbsType = if (forumEntity.type == "official_bbs") "综合论坛" else "游戏论坛"
                        NewLogUtils.logForumListClick(event, location, forumEntity.id, bbsType)
                        mContext.startActivity(ForumDetailActivity.getIntent(mContext, forumEntity.id, entrance))
                    }
                }
            }

            is FooterViewHolder -> {
                holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    inner class ForumItemViewHolder(val binding: ForumMyFollowBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}