package com.gh.gamecenter.qa.video.detail.desc

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.entity.VideoDescItemEntity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailActivity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailViewModel

class VideoDescAdapter(
    context: Context,
    val mVideoDetailViewModel: ForumVideoDetailViewModel,
    val mViewModel: VideoDescViewModel
) :
    ListAdapter<VideoDescItemEntity>(context) {
    private var mContentIsExpand = false
    private var expandHeight = 0
    private var shrinkHeight = 0

    override fun areItemsTheSame(oldItem: VideoDescItemEntity?, newItem: VideoDescItemEntity?): Boolean {
        return oldItem == newItem || oldItem?.recommendVideo?.id == newItem?.recommendVideo?.id
    }

    override fun areContentsTheSame(oldItem: VideoDescItemEntity?, newItem: VideoDescItemEntity?): Boolean {
        return oldItem?.recommendVideo?.count?.vote == newItem?.recommendVideo?.count?.vote
                && oldItem?.recommendVideo?.count?.comment == newItem?.recommendVideo?.count?.comment
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            ItemViewType.ITEM_FOOTER
        } else {
            val entity = mEntityList[position]
            when {
                entity.topVideoInfo != null -> {
                    VIDEO_TOP_DETAIL
                }
                entity.recommendVideo != null -> {
                    VIDEO_RECOMMEND
                }
                else -> {
                    -1
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                FooterViewHolder(mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false))
            }
            VIDEO_TOP_DETAIL -> {
                VideoDescTopViewHolder(
                    parent.toBinding(),
                    mContentIsExpand,
                    shrinkHeight,
                    expandHeight,
                    mVideoDetailViewModel,
                    mViewModel
                )
            }
            VIDEO_RECOMMEND -> {
                VideoItemViewHolder(parent.toBinding())
            }
            else -> {
                throw NullPointerException()
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNotEmpty()) mEntityList.size + FOOTER_ITEM_COUNT else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is VideoDescTopViewHolder -> {
                val videoDescItemEntity = mEntityList[position]
                holder.bindData(videoDescItemEntity.topVideoInfo!!)
            }
            is VideoItemViewHolder -> {
                val entity = mEntityList[position]
                val recommendVideo = entity.recommendVideo
                ImageUtils.display(holder.binding.videoPoster, recommendVideo?.poster)
                holder.binding.durationTv.text = TimeUtils.formatVideoDuration(recommendVideo?.length ?: 0)
                holder.binding.videoTitleTv.text = recommendVideo?.title
                holder.binding.userName.text = recommendVideo?.user?.name
                holder.binding.voteAndCommentTv.text =
                    "点赞 ${recommendVideo?.count?.vote} · 评论 ${recommendVideo?.count?.comment}"
                holder.binding.root.setOnClickListener {
                    recommendVideo?.let {
                        mContext.startActivity(ForumVideoDetailActivity.getIntent(mContext, it.id, it.bbs?.id ?: ""))
                    }
                    val bbsType = if (mViewModel.topVideoDetail?.bbs?.type == "game_bbs") "游戏论坛" else "综合论坛"
                    NewLogUtils.logVideoDetailClick(
                        "click_video_detail_for_you_video",
                        mViewModel.topVideoDetail?.user?.id ?: "",
                        "视频贴", mViewModel.topVideoDetail?.bbs?.id ?: "",
                        bbsType, mViewModel.topVideoDetail?.id ?: "", recommendVideo?.id ?: "", position - 1
                    )
                }
            }
            is FooterViewHolder -> {
                holder.initItemPadding()
                holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            }
        }
    }

    companion object {
        const val VIDEO_TOP_DETAIL = 10
        const val VIDEO_RECOMMEND = 11
    }
}