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

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.NormalListViewModel
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.HtmlUtils
import com.gh.gamecenter.feedback.entity.HelpEntity
import com.gh.gamecenter.feedback.utils.NewLogUtils

class HelpContentAdapter(
    context: Context,
    private val mFragment: HelpContentFragment,
    private val mViewModel: NormalListViewModel<HelpEntity>,
    private val mIsFromHelpAndFeedback: Boolean,
    private val mQaCollectionId: String?,
    private val mNavigationTitle: String,
    private val searchType: String,
    private val location: String
) : ListAdapter<HelpEntity>(context) {
    var searchKey = ""

    private fun replayKeyword(title: String?): String? {
        val translationKey = escapeExprSpecialWord(mFragment.searchKey)
        return if (TextUtils.isEmpty(translationKey)) title
        else title?.replace(translationKey?.toRegex()!!, getHtmlKeyword())
    }

    // 特殊符号转译
    private fun escapeExprSpecialWord(s: String?): String? {
        var keyword = s
        if (!keyword.isNullOrEmpty()) {
            val fbsArr =
                arrayOf("\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|")
            for (key in fbsArr) {
                if (keyword!!.contains(key)) {
                    keyword = keyword.replace(key, "\\" + key)
                }
            }
        }
        return keyword
    }

    private fun getHtmlKeyword(): String {
        return "<font color=\"#ff4147\">" + mFragment.searchKey + "</font>"
    }

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                (view.layoutParams as RecyclerView.LayoutParams).apply {
                    height = RecyclerView.LayoutParams.WRAP_CONTENT
                    view.layoutParams = this
                }
                view.setPadding(0, 20F.dip2px(), 0, 20F.dip2px())
                FooterViewHolder(view)
            }

            else -> {
                HelpItemViewHolder(parent.toBinding())
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is HelpItemViewHolder -> {
                val entity = mEntityList[position]
                holder.bindHelpItem(mContext, entity, mNavigationTitle, mQaCollectionId, -1, true) {
                    if (searchKey.isNotEmpty()) {
                        NewLogUtils.logClickQaSearchItem(
                            searchKey,
                            entity.id,
                            HtmlUtils.filterHtmlLabel(entity.title),
                            entity.helpId
                        )
                        NewLogUtils.logEnterQaContent(
                            "搜索结果",
                            entity.id,
                            HtmlUtils.filterHtmlLabel(entity.title),
                            entity.helpId,
                            mContext.javaClass.simpleName
                        )
                    } else {
                        NewLogUtils.logEnterQaContent(
                            if (mIsFromHelpAndFeedback) "帮助与反馈" else "其他位置",
                            entity.id,
                            HtmlUtils.filterHtmlLabel(entity.title),
                            entity.helpId,
                            mContext.javaClass.simpleName
                        )
                    }
                    SensorsBridge.trackQASearchResultClick(
                        GlobalActivityManager.getCurrentPageEntity().pageId,
                        GlobalActivityManager.getCurrentPageEntity().pageName,
                        location,
                        searchKey,
                        searchType,
                        entity.id,
                        entity.title,
                        position
                    )
                }
            }

            is FooterViewHolder -> {
                holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
                holder.hint.text = "没有更多了~"
                holder.hint.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(mContext))
                holder.hint.textSize = 12F
            }
        }
    }

}