package com.gh.gamecenter.qa.article.detail

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.common.util.PackageUtils
import com.gh.gamecenter.adapter.viewholder.UnAvaliableWebviewViewHolder
import com.gh.gamecenter.databinding.FragmentWebWarningBinding
import com.gh.gamecenter.databinding.ItemArticleDetailContentBinding
import com.gh.gamecenter.qa.comment.base.BaseCommentAdapter

class ArticleDetailAdapter(
    context: Context,
    var mViewModel: ArticleDetailViewModel,
    type: AdapterType,
    entrance: String
) :
    BaseCommentAdapter(context, mViewModel, type, entrance) {

    var articleDetailVH: ArticleDetailContentViewHolder? = 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_ARTICLE_DETAIL -> {
                val isWebViewInstalled = PackageUtils.checkWebViewIsAvailable(mContext)
                if (isWebViewInstalled) {
                    val binding: ItemArticleDetailContentBinding =
                        ItemArticleDetailContentBinding.inflate(mLayoutInflater, parent, false)
                    ArticleDetailContentViewHolder(binding, mViewModel).apply { articleDetailVH = 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) {
        when (holder) {
            is ArticleDetailContentViewHolder -> {
                holder.bindView(mEntityList[position].articleDetail!!)
            }

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

            is CommentFilterViewHolder -> {
                holder.bindView(mViewModel.detailEntity)
            }

            else -> super.onBindViewHolder(holder, position)
        }
    }

}