package com.gh.gamecenter.feedback.view.qa

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.therouter.TheRouter
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.provider.IWebProvider
import com.gh.gamecenter.core.utils.HtmlUtils
import com.gh.gamecenter.feedback.R
import com.gh.gamecenter.feedback.databinding.ItemHelpRecommendBinding
import com.gh.gamecenter.feedback.entity.HelpEntity
import com.gh.gamecenter.feedback.utils.NewLogUtils
import com.gh.gamecenter.feedback.view.help.HelpAndFeedbackViewModel

class HelpQaDefaultAdapter(
    context: Context,
    private val mIsFromSearch: Boolean = false,
    private val mViewModel: HelpAndFeedbackViewModel? = null
) :
    ListAdapter<HelpEntity>(context) {
    private var mRecommendItemCount = 0
    public override fun setListData(updateData: List<HelpEntity>) {
        mRecommendItemCount = updateData.count { it.type == "recommend" }
        super.setListData(updateData)
    }

    override fun getItemViewType(position: Int): Int {
        if (mIsFromSearch && position == 0) return ITEM_NORMAL

        val realPosition = if (mIsFromSearch) position - 1 else position
        val isRecommend = mEntityList.safelyGetInRelease(realPosition)?.type == "recommend"
        return if (isRecommend) {
            ITEM_RECOMMEND
        } else {
            ITEM_NORMAL
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ITEM_NORMAL) {
            HelpItemViewHolder(parent.toBinding())
        } else {
            HelpRecommendItemViewHolder(parent.toBinding())
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (mIsFromSearch && position == 0 && holder is HelpItemViewHolder) {
            holder.binding.run {
                content.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(mContext))
                content.textSize = 16F
                content.typeface = Typeface.DEFAULT_BOLD
                content.text = "别人在搜"
                content.removeDrawable()
                arrowIv.visibility = View.GONE
                indexTv.visibility = View.GONE
                divider.visibility = View.GONE
                root.setOnClickListener(null)
            }
            return
        }

        val realPosition = if (mIsFromSearch) position - 1 else position
        val entity = mEntityList.safelyGetInRelease(realPosition) ?: return

        if (holder is HelpRecommendItemViewHolder) {
            holder.binding.run {
                root.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(mContext))
                container.background = com.gh.gamecenter.common.R.drawable.bg_shape_space_radius_8.toDrawable(mContext)
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(mContext))
                root.setPadding(16F.dip2px(), if (realPosition == 0) 8F.dip2px() else 0, 16F.dip2px(), 8F.dip2px())
                titleTv.text = entity.title
                labelIv.goneIf(entity.recommendType.isEmpty() || entity.recommendType == "none") {
                    labelIv.setImageResource(
                        when (entity.recommendType) {
                            "new" -> R.drawable.label_new
                            "hot" -> R.drawable.label_hot
                            "activity" -> R.drawable.label_activity
                            "notice" -> R.drawable.label_announcement
                            else -> return@goneIf
                        }
                    )
                }
                root.setOnClickListener {
                    val webActivity = TheRouter.get(IWebProvider::class.java)
                    if (entity.content.startsWith("http")) {
                        mContext.startActivity(
                            webActivity?.getIntent(
                                mContext,
                                entity.content,
                                false
                            )
                        )
                    } else {
                        val url = if (EnvHelper.isDevEnv) {
                            Constants.HELP_ADDRESS_DEV
                        } else {
                            Constants.HELP_ADDRESS
                        }
                        mContext.startActivity(
                            webActivity?.getQAIntent(
                                mContext,
                                "${url}${entity.id}&help_id=${entity.helpId}",
                                if (mIsFromSearch) "别人在搜" else mViewModel?.selectedCategory?.name ?: "",
                                false,
                                1
                            )
                        )
                    }
                }
            }
        }

        if (holder is HelpItemViewHolder) {
            holder.bindHelpItem(
                mContext,
                entity,
                if (mIsFromSearch) "别人在搜" else mViewModel?.selectedCategory?.name ?: "",
                "",
                getNormalItemIndex(position),
                position != getLastPosition()
            ) {
                NewLogUtils.logEnterQaContent(
                    if (mIsFromSearch) "别人在搜" else "帮助中心",
                    entity.id,
                    HtmlUtils.filterHtmlLabel(entity.title),
                    entity.helpId,
                    mContext.javaClass.simpleName
                )
            }
        }
    }

    private fun getNormalItemIndex(position: Int) =
        if (mEntityList.isEmpty()) 0 else if (mIsFromSearch) position - mRecommendItemCount - 1 else position - mRecommendItemCount
    private fun getLastPosition() = if (mEntityList.isEmpty()) 0 else itemCount - 1

    override fun getItemCount(): Int =
        if (mEntityList.isEmpty()) 0 else if (mIsFromSearch) mEntityList.size + TOP_ITEM_COUNT else mEntityList.size

    class HelpRecommendItemViewHolder(val binding: ItemHelpRecommendBinding) : BaseRecyclerViewHolder<Any>(binding.root)

    companion object {
        private const val ITEM_RECOMMEND = 103
        private const val ITEM_NORMAL = 104
    }
}