package com.gh.gamecenter.game

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

import com.gh.common.exposure.*
import com.gh.common.util.*
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.databinding.ItemGameNavigationBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.GameNavigationEntity

class GameNavigationAdapter(val mContext: Context, val blockId: String, val blockName: String) :
    ListAdapter<GameNavigationEntity>(mContext) {
    var exposureEventList: ArrayList<ExposureEvent>? = null
    private var mRecordMap = SPUtils.getMap(Constants.SP_GAME_NAVIGATION)

    public override fun setListData(updateData: MutableList<GameNavigationEntity>?) {
        super.setListData(updateData)
    }

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

    override fun areContentsTheSame(oldItem: GameNavigationEntity?, newItem: GameNavigationEntity?): Boolean {
        return oldItem?.id == newItem?.id
                && oldItem?.hint == newItem?.hint
                && oldItem?.entryName == newItem?.entryName
                && oldItem?.linkEntity?.link == newItem?.linkEntity?.link
                && oldItem?.image == newItem?.image
                && oldItem?.hintStartTime == newItem?.hintStartTime
                && oldItem?.hintEndTime == newItem?.hintEndTime
                && oldItem?.time?.update == newItem?.time?.update
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameNavigationViewHolder {
        return GameNavigationViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GameNavigationViewHolder) {
            val entity = mEntityList[position]
            ImageUtils.display(holder.binding.navigationView, entity.image)
            holder.binding.navigationNameTv.text = entity.entryName

            if (entity.hint) {
                val record = mRecordMap[entity.id]
                if (record == null) {
                    holder.binding.redHintIv.visibility = View.VISIBLE
                } else {
                    val split = record.split(divider)//时间戳|点击次数
                    val updateTime = split[0].toLong()
                    val count = split[1].toInt()
                    //判断是否更新过
                    if (updateTime == entity.time?.update) {
                        holder.binding.redHintIv.visibility = View.GONE
                    } else {
                        val currentTime = System.currentTimeMillis() / 1000
                        holder.binding.redHintIv.goneIf(currentTime !in entity.hintStartTime..entity.hintEndTime || count >= 2)
                    }
                }
            } else {
                holder.binding.redHintIv.visibility = View.GONE
            }

            holder.itemView.setOnClickListener {
                entity.linkEntity?.let {
                    val record = mRecordMap[entity.id]
                    if (record == null) {
                        mRecordMap[entity.id] = "${entity.time?.update}${divider}1"
                    } else {
                        val split = record.split(divider)
                        val updateTime = split[0].toLong()
                        var count = split[1].toInt()
                        mRecordMap[entity.id] = "${updateTime}${divider}${++count}"
                    }
                    SPUtils.setMap(Constants.SP_GAME_NAVIGATION, mRecordMap)
                    notifyItemChanged(position)
                    PageSwitchDataHelper.pushCurrentPageData(
                        hashMapOf(
                            Pair(PageSwitchDataHelper.PAGE_BUSINESS_TYPE, "板块-内容列表->导航栏"),
                            Pair(PageSwitchDataHelper.PAGE_BUSINESS_ID, "${blockId}->${entity.id}"),
                            Pair(PageSwitchDataHelper.PAGE_BUSINESS_NAME, blockName)
                        )
                    )
                    DirectUtils.directToLinkPage(
                        mContext,
                        it,
                        "(首页游戏)",
                        "导航栏",
                    )

                    val traceEvent = exposureEventList?.safelyGetInRelease(position) ?: return@setOnClickListener
                    val clickEvent = ExposureEvent.createEvent(
                        GameEntity(
                            containerId = entity.id,
                            containerType = ExposureEntity.NAVIGATION_ID
                        ),
                        traceEvent.source,
                        ExposureTraceUtils.appendTrace(traceEvent),
                        ExposureType.CLICK
                    )
                    ExposureManager.log(clickEvent)
                }
            }
        }
    }

    override fun getItemCount(): Int = mEntityList.size

    companion object {
        const val divider = "|"
    }

    class GameNavigationViewHolder(val binding: ItemGameNavigationBinding) :
        BaseRecyclerViewHolder<RecyclerView.ViewHolder>(binding.root)
}