package com.gh.gamecenter.gamedetail.fuli

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DataCollectionUtils
import com.gh.common.util.NewsUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.NewsDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.ItemGameDetailNewsBinding
import com.gh.gamecenter.feature.entity.NewsEntity

class GameNewsAdapter(
    val context: Context,
    val article: ArrayList<NewsEntity>,
    val mFuLiViewModel: FuLiViewModel?,
    val mEntrance: String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return NewsViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = article.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val newsEntity = article[position]
        if (holder is NewsViewHolder) {
            holder.binding.newsTypeTv.text = newsEntity.type
            holder.binding.newsTitleTv.text = newsEntity.title
            if (newsEntity.type == "公告") {
                holder.binding.newsTypeTv.background = DrawableView.getCornerGradientDrawable(
                    ContextCompat.getColor(context, R.color.text_FFB749),
                    ContextCompat.getColor(context, R.color.text_FF6D3C), 5f
                )
            } else {
                holder.binding.newsTypeTv.background = DrawableView.getCornerGradientDrawable(
                    ContextCompat.getColor(context, R.color.text_3CB9FF),
                    ContextCompat.getColor(context, R.color.theme), 5f
                )
            }
            holder.itemView.setOnClickListener {
                skipNewsDetail(article, position)
            }
        }
    }

    private fun skipNewsDetail(articles: List<NewsEntity>, position: Int) {
        val newsEntity = articles[position]

        DataCollectionUtils.uploadClick(context, "新手攻略", "游戏详情", newsEntity.title)

        // 统计阅读量
        NewsUtils.statNewsViews(newsEntity.id)
        NewsDetailActivity.startNewsDetailActivity(
            context, newsEntity,
            mEntrance + "+(游戏详情[" + mFuLiViewModel?.game?.name + "]:新手攻略-列表[" + (position + 1) + "])"
        )
    }

    class NewsViewHolder(var binding: ItemGameDetailNewsBinding) : RecyclerView.ViewHolder(binding.root)
}