package com.gh.gamecenter.qa.questions.newdetail

import android.content.Context
import android.text.SpannableStringBuilder
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.PackageUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.UnAvaliableWebviewViewHolder
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.view.CustomLinkMovementMethod
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.FragmentWebWarningBinding
import com.gh.gamecenter.databinding.ItemArticleDetailContentBinding
import com.gh.gamecenter.qa.answer.detail.SimpleAnswerDetailActivity
import com.gh.gamecenter.qa.comment.base.BaseCommentAdapter

class NewQuestionDetailAdapter(
    context: Context,
    var mViewModel: NewQuestionDetailViewModel,
    type: AdapterType,
    val entrance: String
) :
    BaseCommentAdapter(context, mViewModel, type, entrance) {

    var questionDetailVH: QuestionDetailContentViewHolder? = null

    override fun loadChange(status: LoadStatus?) {
        if (status == LoadStatus.INIT) {
            mIsNetworkError = false
            mIsOver = false
            mIsLoading = true
            return
        } else {
            super.loadChange(status)
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ITEM_QUESTION_DETAIL -> {
                val isWebViewInstalled = PackageUtils.checkWebViewIsAvailable(mContext)
                if (isWebViewInstalled) {
                    val binding: ItemArticleDetailContentBinding =
                        ItemArticleDetailContentBinding.inflate(mLayoutInflater, parent, false)
                    QuestionDetailContentViewHolder(binding, mViewModel).apply { questionDetailVH = this }
                } else {
                    val binding: FragmentWebWarningBinding =
                        FragmentWebWarningBinding.inflate(mLayoutInflater, parent, false)
                    UnAvaliableWebviewViewHolder(binding)
                }
            }

            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        when (holder) {
            is QuestionDetailContentViewHolder -> {
                holder.bindView(mEntityList[position].questionDetail!!)
            }

            is UnAvaliableWebviewViewHolder -> {
                holder.bindView(480f)
            }

            is CommentFilterViewHolder -> {
                holder.bindView(questions = mViewModel.questionDetail)
            }
            is CommentItemViewHolder -> {
                val comment = mEntityList[position].commentNormal
                val spannableStringBuilder = SpannableStringBuilder()
                if (comment?.type == "answer") {
                    val detailSpan = SpanBuilder("[查看回答详情]").click(mContext, 0, 8, R.color.theme_font) {
                        val intent = SimpleAnswerDetailActivity.getIntent(
                            holder.itemView.context, comment.id
                                ?: "", entrance, "问题详情"
                        )
                        holder.itemView.context.startActivity(intent)
                    }.build()
                    spannableStringBuilder.append(detailSpan)
                    holder.binding.contentTv.maxEms = 20
                } else {
                    holder.binding.contentTv.maxEms = Int.MAX_VALUE
                }
                spannableStringBuilder.append(comment?.content)
                holder.binding.contentTv.text = spannableStringBuilder
                holder.binding.contentTv.movementMethod = CustomLinkMovementMethod.getInstance()
            }
            else -> super.onBindViewHolder(holder, position)
        }
    }
}