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

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.view.GridSpacingItemColorDecoration
import com.gh.gamecenter.core.provider.IDirectProvider
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.feedback.R
import com.gh.gamecenter.feedback.databinding.HelpQaCategoryItemBinding
import com.gh.gamecenter.feedback.entity.HelpCategoryEntity
import com.gh.gamecenter.feedback.utils.NewLogUtils

class HelpQaCategoryAdapter(val context: Context, private val mQaCollectionId: String?) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    val mEntityList = arrayListOf<HelpCategoryEntity>()

    fun setListData(updateData: List<HelpCategoryEntity>) {
        mEntityList.clear()
        mEntityList.addAll(updateData)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.help_qa_category_item, parent, false)
        return HelpQaCategoryViewHolder(HelpQaCategoryItemBinding.bind(view))
    }

    override fun getItemCount(): Int = mEntityList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is HelpQaCategoryViewHolder) {
            val helpCategoryEntity = mEntityList[position]
            ImageUtils.display(holder.binding.categoryIcon, helpCategoryEntity.icon)
            holder.binding.categoryName.text = helpCategoryEntity.name

            holder.binding.qaRv.apply {
                layoutManager = GridLayoutManager(context, 2)
                addItemDecoration(
                    GridSpacingItemColorDecoration(
                        context,
                        28,
                        0,
                        R.color.transparent
                    )
                )
                adapter = HelpQaAdapter(context, helpCategoryEntity, mQaCollectionId)
            }
            holder.binding.categoryMore.setOnClickListener {
                val provider =
                    ARouter.getInstance().build(RouteConsts.provider.directUtils).navigation() as? IDirectProvider
                provider?.directToQa(context, helpCategoryEntity.name, helpCategoryEntity.id)
                if (!mQaCollectionId.isNullOrEmpty()) {
                    MtaHelper.onEvent("QA", "QA合集点击", "点击更多+${helpCategoryEntity.name}")
                } else {
                    MtaHelper.onEvent("意见反馈", "使用帮助点击", "点击更多+${helpCategoryEntity.name}")
                }
                NewLogUtils.logClickQaMoreBtn(helpCategoryEntity.id)
            }
        }
    }

    class HelpQaCategoryViewHolder(val binding: HelpQaCategoryItemBinding) : RecyclerView.ViewHolder(binding.root)

}