package com.gh.gamecenter.gamedetail.detail.viewholder

import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ItemGameDetailAnnouncementBinding
import com.gh.gamecenter.databinding.ItemGameDetailImageLinkBinding
import com.gh.gamecenter.databinding.ItemGameDetailLinkBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.gh.gamecenter.gamedetail.entity.GameDetailLink

class GameDetailAnnouncementItemViewHolder(
    val binding: ItemGameDetailAnnouncementBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {

    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkAnnouncement ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            titleTv.text = entity.name
            if (recyclerView.adapter !is GameDetailAnnouncementAdapter) {
                recyclerView.isNestedScrollingEnabled = false
                recyclerView.layoutManager = LinearLayoutManager(context)
                recyclerView.adapter = GameDetailAnnouncementAdapter(entity.data)
            } else {
                recyclerView.adapter?.run { notifyItemRangeChanged(0, itemCount) }
            }
        }
    }

    inner class GameDetailAnnouncementAdapter(val dataList: List<GameDetailLink>) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun getItemViewType(position: Int): Int {
            val data = dataList[position]
            return if (data.img.isEmpty()) ITEM_LINK else ITEM_IMAGE_LINK
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return if (viewType == ITEM_LINK) {
                GameDetailAnnouncementItemViewHolder(parent.toBinding())
            } else {
                GameDetailAnnouncementImageItemViewHolder(parent.toBinding())
            }
        }

        override fun getItemCount(): Int = dataList.size

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val data = dataList.getOrNull(position) ?: return
            if (holder is GameDetailAnnouncementItemViewHolder) {
                holder.binding.root.setPadding(4F.dip2px(), 0, 4F.dip2px(), 0)
                GameDetailAdvertisingItemViewHolder.bindItem(
                    context,
                    holder.binding,
                    data,
                    true,
                    "游戏详情-资讯公告",
                    baseTrackData.apply {
                        moduleName = itemData?.linkAnnouncement?.name ?: ""
                        subModuleName = data.text
                        supSequence = position + 1
                    }) { gameStatus }
            }
            if (holder is GameDetailAnnouncementImageItemViewHolder) {
                holder.binding.root.setPadding(16F.dip2px(), 0, 16F.dip2px(), if (position == itemCount - 1) 0 else 12F.dip2px())
                GameDetailAdvertisingImageItemViewHolder.bindImageItem(
                    context,
                    holder.binding,
                    data,
                    true,
                    "游戏详情-资讯公告",
                    baseTrackData.apply {
                        moduleName = itemData?.linkAnnouncement?.name ?: ""
                        subModuleName = data.text
                        supSequence = position + 1
                    }
                ) { gameStatus }
            }
        }

    }

    inner class GameDetailAnnouncementItemViewHolder(val binding: ItemGameDetailLinkBinding) :
        RecyclerView.ViewHolder(binding.root)

    inner class GameDetailAnnouncementImageItemViewHolder(val binding: ItemGameDetailImageLinkBinding) :
        RecyclerView.ViewHolder(binding.root)

    companion object {
        const val ITEM_LINK = 0
        const val ITEM_IMAGE_LINK = 1
    }
}