package com.gh.gamecenter.qa.comment

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.OnCommentCallBackListener
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.databinding.StairsCommentItemBinding
import com.gh.gamecenter.entity.CommentEntity

class StairsCommentAdapter(
    context: Context,
    var mViewModel: NewCommentViewModel,
    var mIsShowingConversation: Boolean,
    var mCommentCallBackListener: OnCommentCallBackListener,
    var mCommentOptionClickListener: OnCommentOptionClickListener? = null,
    var mEntrance: String
) : ListAdapter<CommentEntity>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ItemViewType.LOADING) {
            val view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false)
            FooterViewHolder(view)
        } else {
            val view = mLayoutInflater.inflate(R.layout.stairs_comment_item, parent, false)
            StairsCommentViewHolder(StairsCommentItemBinding.bind(view))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is StairsCommentViewHolder) {
            val commentEntity = mEntityList[position]
            holder.initCommentViewHolder(
                holder,
                mViewModel,
                commentEntity,
                mEntrance,
                mIsShowingConversation,
                mCommentOptionClickListener,
                mCommentCallBackListener
            )
        } else if (holder is FooterViewHolder) {
            initFooterViewHolder(holder)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            ItemViewType.LOADING
        } else 100
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT
    }

    private fun initFooterViewHolder(holder: FooterViewHolder) {
        if (mIsNetworkError) {
            holder.loading.visibility = View.GONE
            holder.hint.setText(R.string.loading_error_network)
        } else if (!mIsOver) {
            holder.hint.setText(R.string.loading)
            holder.loading.visibility = View.VISIBLE
        } else if (mEntityList.size == 0) {
            holder.loading.visibility = View.GONE
            holder.hint.setText(R.string.comment_empty)
        } else {
            holder.hint.setText(R.string.comment_nomore)
            holder.loading.visibility = View.GONE
        }
    }

}