package com.gh.gamecenter.personalhome.rating

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.common.util.NewsUtils
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.common.utils.fromHtml
import com.gh.gamecenter.common.utils.setTextWithHighlightedTextWrappedInsideWrapper
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.adapter.viewholder.PersonalHomeRatingViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.NormalListViewModel
import com.gh.gamecenter.databinding.PersonalHomeRatingBinding

class RatingAdapter(
    context: Context,
    private val mEntrance: String,
    private val mListViewModel: NormalListViewModel<MyRating>
) : ListAdapter<MyRating>(context) {

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }
            else -> {
                view = mLayoutInflater.inflate(R.layout.personal_home_rating, parent, false)
                PersonalHomeRatingViewHolder(PersonalHomeRatingBinding.bind(view))
            }
        }
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is PersonalHomeRatingViewHolder -> {
                val entity = mEntityList[holder.adapterPosition]
                val historyEntity = entity.transformHistoryEntity()
                holder.binding.comment.text =
                    if (historyEntity.count.reply > 0) NumberUtils.transSimpleCount(historyEntity.count.reply) else ""
                holder.binding.vote.isChecked = historyEntity.me.isVoted
                holder.binding.vote.text =
                    if (historyEntity.count.vote > 0) NumberUtils.transSimpleCount(historyEntity.count.vote) else ""
                holder.binding.ratingStart.rating = historyEntity.comment.star.toFloat()

                holder.binding.userCommand.text = (mContext.getString(
                    R.string.personal_home_rating_command,
                    NewsUtils.getFormattedTime(entity.time),
                    entity.game.name
                )).fromHtml()
                holder.binding.content.setTextWithHighlightedTextWrappedInsideWrapper(
                    text = entity.content,
                    copyClickedText = true
                )
                holder.itemView.setOnClickListener {
                    GameDetailActivity.startGameDetailActivity(
                        mContext,
                        entity.game.id,
                        mEntrance,
                        isSkipGameComment = true
                    )
                }
            }
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mListViewModel, mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }
}