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

import android.content.Context
import android.graphics.Typeface
import android.view.View
import androidx.core.view.isVisible
import com.therouter.TheRouter
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
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.feedback.R
import com.gh.gamecenter.feedback.databinding.HelpItemBinding
import com.gh.gamecenter.feedback.entity.HelpEntity

class HelpItemViewHolder(val binding: HelpItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {
    fun bindHelpItem(
        context: Context,
        entity: HelpEntity,
        navigationTitle: String,
        qaCollectionId: String? = "",
        index: Int = -1,
        showDivider: Boolean,
        clickAction: (() -> Unit)? = null
    ) {
        binding.arrowIv.visibility = View.VISIBLE
        binding.content.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
        binding.content.text = entity.title.fromHtml()
        binding.content.typeface = Typeface.DEFAULT
        binding.divider.isVisible = showDivider
        binding.indexTv.goneIf(index == -1) {
            binding.indexTv.setTypeface(Typeface.createFromAsset(context.assets, Constants.DIN_FONT_PATH))
            binding.indexTv.text = "${index + 1}"
            when (index) {
                0 -> binding.indexTv.setTextColor(com.gh.gamecenter.common.R.color.number_1.toColor(context))
                1 -> binding.indexTv.setTextColor(com.gh.gamecenter.common.R.color.number_2.toColor(context))
                2 -> binding.indexTv.setTextColor(com.gh.gamecenter.common.R.color.number_3.toColor(context))
                else -> binding.indexTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
            }
        }
        if (entity.recommendType.isNotEmpty() && entity.recommendType != "none") {
            binding.content.setDrawableEnd(
                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
                }
            )
        } else {
            binding.content.removeDrawable()
        }
        itemView.setOnClickListener {
            val webActivity = TheRouter.get(IWebProvider::class.java)
            if (entity.content.startsWith("http")) {
                context.startActivity(
                    webActivity?.getIntent(
                        context,
                        entity.content,
                        false
                    )
                )
            } else {
                val url = if (EnvHelper.isDevEnv) {
                    Constants.HELP_ADDRESS_DEV
                } else {
                    Constants.HELP_ADDRESS
                }
                context.startActivity(
                    webActivity?.getQAIntent(
                        context,
                        "${url}${entity.id}&help_id=${entity.helpId}",
                        navigationTitle,
                        false,
                        if (!qaCollectionId.isNullOrEmpty()) 0 else 1
                    )
                )
            }
            clickAction?.invoke()
        }
    }
}