package com.gh.gamecenter.home.recommend

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.gh.common.util.DirectUtils
import com.gh.common.util.LogUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.setDebouncedClickListener
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.core.utils.PageSwitchDataHelper
import com.gh.gamecenter.databinding.ItemHomeRecommendListItemBinding
import com.gh.gamecenter.entity.HomeRecommend
import org.json.JSONException
import org.json.JSONObject

/**
 * @author : liujiarui
 * date : 2023/3/23
 * description :
 */
class HomeRecommendItemGridAdapter : BaseAdapter() {

    var mDataList: List<HomeRecommend> = mutableListOf()

    override fun getCount(): Int {
        return mDataList.size
    }

    override fun getItem(position: Int): Any {
        return mDataList[position]
    }

    override fun getItemId(position: Int): Long {
        return mDataList[position].linkId.hashCode().toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val data = mDataList.getOrNull(position)
        val binding: ItemHomeRecommendListItemBinding
        if (convertView == null) {
            binding = ItemHomeRecommendListItemBinding
                .inflate(LayoutInflater.from(parent.context), parent, false)
            binding.root.tag = binding
        } else {
            binding = convertView.tag as ItemHomeRecommendListItemBinding
        }
        if (data != null) {
            ImageUtils.display(binding.iconIv, data.icon)
            binding.nameTv.text = data.name
            binding.nameTv.setTextColor(R.color.text_title.toColor(binding.root.context))
        }
        initEvent(binding, position)
        return binding.root
    }

    private fun initEvent(binding: ItemHomeRecommendListItemBinding, position: Int) {
        //点击事件上报
        binding.root.setDebouncedClickListener {
            MtaHelper.onEvent("首页_新", "推荐入口_点击", (position + 1).toString())
            val recommend = mDataList[position]

            LogUtils.logRecommendClick(
                "首页",
                recommend.name,
                recommend.linkType,
                recommend.linkText,
                recommend.linkId,
                position
            )

            val trackEvent = JSONObject()
            try {
                trackEvent.put("position", position)
                trackEvent.put("Recommended_location_name", recommend.name)
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            SensorsBridge.trackEvent("RecommendedLocationClick", trackEvent)

            if (recommend.linkType == "top_game_comment") {
                MtaHelper.onEvent("安利墙", "进入", "首页推荐入口")
            }
            if (recommend.linkType == "common_collection") {
                NewLogUtils.logAccessToCommonCollectionDetail(recommend.linkId, recommend.linkText, "首页推荐入口")
            }
            PageSwitchDataHelper.pushCurrentPageData(hashMapOf(Pair(PageSwitchDataHelper.PAGE_BUSINESS_TYPE, "首页-推荐位")))
            DirectUtils.directToLinkPage(
                binding.root.context,
                recommend.transformLinkEntity(),
                "首页推荐入口",
                "新首页-推荐入口",
            )
        }
    }
}