package com.gh.gamecenter.mypost

import android.app.Activity
import android.content.Context
import android.text.SpannableStringBuilder
import android.util.SparseBooleanArray
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.common.util.DirectUtils
import com.gh.common.view.ImageContainerView
import com.gh.common.view.ImageContainerView.Companion.toImageContainerData
import com.gh.gamecenter.ImageViewerActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding
import com.gh.gamecenter.feature.entity.ForumVideoEntity
import com.gh.gamecenter.entity.PersonalHistoryEntity
import com.gh.gamecenter.forum.home.ArticleItemVideoView
import com.gh.gamecenter.personalhome.PersonalItemViewHolder
import com.gh.gamecenter.personalhome.home.UserHistoryViewModel
import com.gh.gamecenter.feature.entity.AnswerEntity
import com.gh.gamecenter.forum.home.AnswerArticleVideoViewEventHelper
import com.gh.gamecenter.forum.home.ArticleItemVideoView.Companion.toArticleVideoData
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.OrientationUtils

class MyPostAdapter(
    context: Context,
    private val mListViewModel: UserHistoryViewModel,
    private val mEntrance: String,
    private val itemClickCallback: (historyEntity: PersonalHistoryEntity, position: Int) -> Unit
) : ListAdapter<PersonalHistoryEntity>(context) {

    private var mExpandSparseBooleanArray = SparseBooleanArray()

    override fun getItemViewType(position: Int): Int {
        return when (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(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }

            else -> {
                view = mLayoutInflater.inflate(R.layout.community_answer_item, parent, false)
                PersonalItemViewHolder(mContext, CommunityAnswerItemBinding.bind(view), itemClickCallback)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is PersonalItemViewHolder -> bindNormalItem(holder, position)
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mListViewModel, mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    private fun bindNormalItem(holder: PersonalItemViewHolder, position: Int) {
        val historyEntity = mEntityList[position]

        holder.bindPersonalItem(historyEntity, mEntrance, position)
    }

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