package com.gh.gamecenter.help

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.common.util.*
import com.gh.gamecenter.QaActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.NormalListViewModel
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.fromHtml
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.core.utils.HtmlUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.HelpItemBinding
import com.gh.gamecenter.entity.HelpEntity

class HelpContentAdapter(
    context: Context,
    private val mFragment: HelpContentFragment,
    private val mViewModel: NormalListViewModel<HelpEntity>,
    private val mQaId: String?,
    private val mQaCollectionId: String?,
    private val mNavigationTitle: 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(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 -> {
                view = mLayoutInflater.inflate(R.layout.help_item, parent, false)
                HelpItemViewHolder(HelpItemBinding.bind(view))
            }
        }
    }

    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.binding.content.text = entity.title.fromHtml()
                holder.itemView.setOnClickListener {
                    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}",
                                mNavigationTitle,
                                true,
                                if (!mQaCollectionId.isNullOrEmpty()) 0 else 1
                            )
                        )
                    }
                    val category = mFragment.getHelpCategory()
                    val activity = mFragment.activity
                    val navigationTitle = if (activity is QaActivity) {
                        activity.findViewById<TextView>(R.id.normal_title).text.toString()
                    } else "未知"
                    if (searchKey.isNotEmpty()) {
                        if (!mQaCollectionId.isNullOrEmpty()) {
                            MtaHelper.onEvent(
                                "QA",
                                "QA合集搜索结果",
                                "${searchKey}+${HtmlUtils.stripHtml(entity.title)}"
                            )
                        } else {
                            MtaHelper.onEvent(
                                "意见反馈",
                                "使用帮助搜索结果",
                                "${searchKey}+${HtmlUtils.stripHtml(entity.title)}"
                            )
                        }
                        NewLogUtils.logClickQaSearchItem(
                            searchKey,
                            entity.id,
                            HtmlUtils.filterHtmlLabel(entity.title),
                            entity.helpId
                        )
                        NewLogUtils.logEnterQaContent(
                            "搜索结果",
                            entity.id,
                            HtmlUtils.filterHtmlLabel(entity.title),
                            entity.helpId
                        )
                    } else if (!mQaId.isNullOrEmpty()) {
                        MtaHelper.onEvent("QA", "QA内容", navigationTitle + "+" + entity.title)
                        NewLogUtils.logEnterQaContent(
                            "QA",
                            entity.id,
                            HtmlUtils.filterHtmlLabel(entity.title),
                            entity.helpId
                        )
                    } else if (!mQaCollectionId.isNullOrEmpty()) {
                        if (category != null) {
                            MtaHelper.onEvent(
                                "QA",
                                "QA合集内容",
                                navigationTitle + "+" + category.name + "+" + entity.title
                            )
                        } else {
                            MtaHelper.onEvent("QA", "QA合集内容", navigationTitle + "+" + entity.title)
                        }
                    } else {
                        if (category != null) {
                            MtaHelper.onEvent("意见反馈", "使用帮助内容", category.name + "+" + entity.title)
                        } else {
                            MtaHelper.onEvent("意见反馈", "使用帮助内容", entity.title)
                        }
                    }

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

}