package com.gh.gamecenter.personalhome.home

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
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.databinding.VideoNewItemBinding
import com.gh.gamecenter.entity.MyVideoEntity
import com.gh.gamecenter.video.VideoItemViewHolder
import com.gh.gamecenter.video.detail.VideoDetailContainerViewModel

class UserVideoHistoryAdapter(context: Context, val mViewModel: UserVideoHistoryViewModel) :
    ListAdapter<MyVideoEntity>(context) {

    override fun areItemsTheSame(oldItem: MyVideoEntity?, newItem: MyVideoEntity?): Boolean {
        return oldItem == newItem
    }

    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)

            val params = holder.binding.videoTitle.layoutParams as ConstraintLayout.LayoutParams
            params.height = ConstraintLayout.LayoutParams.WRAP_CONTENT
            holder.binding.videoTitle.layoutParams = params

            holder.binding.userInfo.visibility = View.GONE
            holder.itemView.setOnClickListener {
                DirectUtils.directToVideoDetail(
                    mContext,
                    entity.id,
                    VideoDetailContainerViewModel.Location.USER_VIDEO.value,
                    false,
                    path = "个人主页-视频"
                )
            }
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }

}