package com.gh.gamecenter.feedback.view.help

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.core.provider.IWebProvider
import com.gh.gamecenter.core.utils.HtmlUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.feedback.entity.HelpCategoryEntity
import com.gh.gamecenter.feedback.utils.NewLogUtils
import com.gh.gamecenter.floatingwindow.databinding.HelpQaItemBinding

class HelpQaAdapter(val context: Context, val helpCategoryEntity: HelpCategoryEntity, val mQaCollectionId: String?) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return HelpQaItemViewHolder(
            HelpQaItemBinding.inflate(
                LayoutInflater.from(context),
                parent,
                false
            ).root
        )
    }

    override fun getItemCount(): Int = helpCategoryEntity.helpEntities.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val helpEntity = helpCategoryEntity.helpEntities[position]
        val textView = holder.itemView as TextView
        textView.text = helpEntity.title
        textView.setOnClickListener {
            val webActivity =
                ARouter.getInstance().build(RouteConsts.provider.webActivity).navigation() as? IWebProvider

            if (helpEntity.content.startsWith("http")) {
                context.startActivity(webActivity?.getIntent(context, helpEntity.content, false))
            } else {
                val url = if (EnvHelper.isDevEnv) {
                    Constants.HELP_ADDRESS_DEV
                } else {
                    Constants.HELP_ADDRESS
                }
                val qaIntent = webActivity?.getQAIntent(
                    context,
                    "${url}${helpEntity.id}&help_id=${helpCategoryEntity.id}",
                    helpCategoryEntity.name,
                    true,
                    if (!mQaCollectionId.isNullOrEmpty()) 0 else 1
                )
                qaIntent?.let {
                    context.startActivity(it)
                }

            }
            if (!mQaCollectionId.isNullOrEmpty()) {
                MtaHelper.onEvent("QA", "QA合集点击", "点击首页+${HtmlUtils.stripHtml(helpEntity.title)}")
            } else {
                MtaHelper.onEvent("意见反馈", "使用帮助点击", "点击首页+${HtmlUtils.stripHtml(helpEntity.title)}")
            }
            NewLogUtils.logEnterQaContent(
                "QA首页",
                helpEntity.id,
                HtmlUtils.filterHtmlLabel(helpEntity.title),
                helpCategoryEntity.id
            )
        }
    }

    class HelpQaItemViewHolder(view: View) : RecyclerView.ViewHolder(view)
}