package com.gh.gamecenter.gamedetail

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.ItemGameDetailContentCardContentBinding
import com.gh.gamecenter.gamedetail.entity.ContentCardEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class GameDetailContentCardContentAdapter(
    context: Context,
    private val linkEntity: ContentCardEntity,
    private val isHighlightBg: Boolean = false
) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
        GameDetailContentCardContentItemViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameDetailContentCardContentItemViewHolder) {
            holder.binding.root.setTextColor(
                if (isHighlightBg) R.color.text_subtitle.toColor(mContext) else R.color.text_subtitleDesc.toColor(
                    mContext
                )
            )
            if (linkEntity.type == "func_server" && linkEntity.server != null && linkEntity.server?.calendar?.isNotEmpty() == true) {
                val calendarList = linkEntity.server!!.calendar
                val realPosition = position % calendarList.size
                calendarList.safelyGetInRelease(realPosition)?.let {
                    val serverTime =
                        if (TimeUtils.isToday(it.getTime()))
                            it.getFormatTime("今天 HH:mm")
                        else if (TimeUtils.isTomorrow(it.getTime()))
                            it.getFormatTime("明天 HH:mm")
                        else
                            it.getFormatTime("MM-dd HH:mm")
                    holder.binding.root.text = "$serverTime ${it.type}"
                }
            }
        }
    }

    override fun getItemCount(): Int = Int.MAX_VALUE

    class GameDetailContentCardContentItemViewHolder(var binding: ItemGameDetailContentCardContentBinding) :
        RecyclerView.ViewHolder(binding.root)
}