package com.gh.gamecenter.video.game

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.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.NormalListViewModel
import com.gh.gamecenter.databinding.VideoNewItemBinding
import com.gh.gamecenter.entity.MyVideoEntity
import com.gh.gamecenter.video.VideoItemViewHolder
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel

class GameVideoAdapter(
    context: Context,
    private val mViewModel: NormalListViewModel<MyVideoEntity>,
    private val mEntrance: String,
    private val mSortValue: String?,
    val gameId: String
) : ListAdapter<MyVideoEntity>(context) {

    override fun getItemViewType(position: Int): Int {
        if (position == itemCount - 1) return ItemViewType.ITEM_FOOTER
        return 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.video_new_item, parent, false)
                return VideoItemViewHolder(VideoNewItemBinding.bind(view))
            }
        }
    }

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

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is VideoItemViewHolder) {
            val entity = mEntityList[position]
            holder.bindVideoItem(entity)
            holder.itemView.setOnClickListener {
                var fromLocation = ""
                var path = ""

                if (mSortValue?.contains("vote") == true) {
                    fromLocation = VideoDetailContainerViewModel.Location.HOTTEST_GAME_VIDEO.value
                    path = "视频合集-热门"
                } else {
                    fromLocation = VideoDetailContainerViewModel.Location.NEWEST_GAME_VIDEO.value
                    path = "视频合集-最新"
                }
                DirectUtils.directToVideoDetail(
                    mContext,
                    entity.id,
                    fromLocation,
                    false,
                    gameId = gameId,
                    entrance = mEntrance,
                    path = path
                )

                holder.binding.userInfo.setOnClickListener {
                    DirectUtils.directToHomeActivity(mContext, entity.user.id, mEntrance, path)
                }
            }
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }
}