package com.gh.gamecenter.gamedetail.desc

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.DefaultUrlHandler
import com.gh.common.util.*
import com.gh.common.util.LogUtils
import com.gh.gamecenter.NewsDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.ItemGameRaidersBinding
import com.gh.gamecenter.databinding.ItemGameRaidersFixedTopBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.NewsEntity

class GameRaidersAdapter(
    val context: Context,
    val articles: ArrayList<NewsEntity>,
    val mEntrance: String,
    val game: GameEntity?
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDefaultHorizontalPadding by lazy { R.dimen.game_detail_item_horizontal_padding.toPx() }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_ARTICLE) {
            RaidersViewHolder(parent.toBinding())
        } else {
            RaidersFixedTopViewHolder(
                ItemGameRaidersFixedTopBinding.inflate(
                    LayoutInflater.from(context),
                    parent,
                    false
                )
            )
        }
    }

    override fun getItemCount(): Int = articles.size

    override fun getItemViewType(position: Int): Int {
        return if (articles[position].priority == Int.MAX_VALUE) {
            TYPE_ARTICLE_FIXED_TOP
        } else {
            TYPE_ARTICLE
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
        params.leftMargin = if (position == 0) mDefaultHorizontalPadding else 8F.dip2px()
        params.rightMargin = if (position == itemCount - 1) mDefaultHorizontalPadding else 0F.dip2px()
        holder.itemView.layoutParams = params

        if (holder is RaidersViewHolder) {
            val newsEntity = articles[position]
            holder.binding.root.setRootBackgroundDrawable(R.drawable.background_shape_white_radius_5)
            holder.binding.titleTv.setTextColor(R.color.text_title.toColor(context))
            holder.binding.contentTv.setTextColor(R.color.text_subtitleDesc.toColor(context))
            holder.binding.timeTv.setTextColor(R.color.text_subtitleDesc.toColor(context))
            holder.binding.line.setBackgroundColor(R.color.divider.toColor(context))
            holder.binding.titleTv.text = newsEntity.title ?: ""
            holder.binding.contentTv.text = newsEntity.intro ?: ""
            holder.binding.timeTv.text =
                if (TimeUtils.isToday(newsEntity.publishOn)) "今天" else TimeUtils.getFormatTime(newsEntity.publishOn)
            holder.itemView.setOnClickListener {
                skipNewsDetail(newsEntity, position)
            }
        } else if (holder is RaidersFixedTopViewHolder) {
            val newsEntity = articles[position]
            holder.binding.root.setRootBackgroundDrawable(R.drawable.background_shape_white_radius_5)
            holder.binding.titleTv.setTextColor(R.color.text_title.toColor(context))
            holder.binding.descTv.setTextColor(R.color.text_subtitleDesc.toColor(context))
            holder.binding.titleTv.text = newsEntity.title ?: ""
            ImageUtils.display(holder.binding.backgroundIv, newsEntity.thumb)
            holder.binding.descTv.text = newsEntity.type
            holder.itemView.setOnClickListener {
                LogUtils.logGameDetailFixedTopArticleClick(game?.id, game?.name, newsEntity.link)

                val isUrlIntercepted = DefaultUrlHandler.interceptUrl(
                    context,
                    newsEntity.link ?: "",
                    "新手攻略"
                )
                if (!isUrlIntercepted) {
                    DirectUtils.directToWebView(context, newsEntity.link ?: "")
                }
            }
        }
    }

    private fun skipNewsDetail(article: NewsEntity, position: Int) {
        DataCollectionUtils.uploadClick(context, "新手攻略", "游戏详情", article.title)

        MtaHelper.onEvent("游戏详情_新", "新手攻略卡片", "${game?.name}+${article.title}")

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

    companion object {
        const val TYPE_ARTICLE = 123
        const val TYPE_ARTICLE_FIXED_TOP = 124
    }

    class RaidersViewHolder(var binding: ItemGameRaidersBinding) : RecyclerView.ViewHolder(binding.root)
    class RaidersFixedTopViewHolder(var binding: ItemGameRaidersFixedTopBinding) : RecyclerView.ViewHolder(binding.root)
}