package com.gh.gamecenter.home.discovercard

import android.content.Context
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.constant.Config
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.HomeDiscoverCardItemBinding
import com.gh.gamecenter.discovery.DiscoveryActivity
import com.gh.gamecenter.discovery.interestedgame.InterestedGameActivity
import com.gh.gamecenter.entity.DiscoveryCardEntity
import com.gh.gamecenter.entity.DiscoveryGameCardLabel
import com.gh.gamecenter.game.vertical.SpanCountPagerSnapHelper
import splitties.views.dsl.core.endMargin

class HomeDiscoverCardViewHolder(val binding: HomeDiscoverCardItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    fun bindView(
        entity: DiscoveryCardEntity,
        entrance: String, blockName: String = ""
    ): SpanCountPagerSnapHelper {
        val spanCount = 3
        val snapHelper = SpanCountPagerSnapHelper(spanCount, false)
        binding.run {
            val context = root.context
            labelHSV.background = R.drawable.background_shape_white_radius_6_bottom_only.toDrawable(context)
            prefsIv.goneIf(!Config.getUserInterestedGame()) {
                prefsIv.setOnClickListener {
                    context.ifLogin("${entrance}-发现页卡片") {
                        context.startActivity(InterestedGameActivity.getIntent(context, "${entrance}-发现页卡片"))
                        NewFlatLogUtils.logInterestedGameClick(entrance, blockName)
                    }
                }
            }
            allTv.setOnClickListener {
                context.startActivity(DiscoveryActivity.getIntent(context, "${entrance}-发现页卡片"))
                NewFlatLogUtils.logDiscoverDetailPageEnter(entrance, blockName)
            }
            recyclerView.run {
                clearOnScrollListeners()
                (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                onFlingListener = null

                if (layoutManager !is GridLayoutManager) {
                    layoutManager = GridLayoutManager(context, spanCount, RecyclerView.HORIZONTAL, false)
                }

                if (adapter is DiscoverCardGameAdapter) {
                    val discoverCardGameAdapter = adapter as DiscoverCardGameAdapter
                    discoverCardGameAdapter.checkResetData(entity.games)
                } else {
                    adapter = DiscoverCardGameAdapter(context, entrance, entity.games)
                }
                snapHelper.attachToRecyclerView(this)
                isNestedScrollingEnabled = false
            }

            val childCount = labelContainer.childCount
            entity.label.forEachIndexed { index, label ->
                if (index < childCount) {
                    val labelView = labelContainer.getChildAt(index) as TextView
                    updateLabelView(context, labelView, label, index == entity.label.size - 1)
                } else {
                    val newLabelView = TextView(context)
                    updateLabelView(context, newLabelView, label, index == entity.label.size - 1)
                    labelContainer.addView(newLabelView)
                }
            }
        }
        return snapHelper
    }

    private fun updateLabelView(
        context: Context,
        textView: TextView,
        label: DiscoveryGameCardLabel,
        isLast: Boolean
    ) {
        textView.background = R.drawable.bg_shape_space_radius_4.toDrawable(context)
        textView.setTextColor(R.color.text_subtitle.toColor(context))

        if (textView.text == label.title) return

        textView.run {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                36F.dip2px()
            ).apply { endMargin = if (isLast) 14F.dip2px() else 8F.dip2px() }
            setPadding(12F.dip2px(), 0, 12F.dip2px(), 0)
            gravity = Gravity.CENTER
            setDrawableEnd(R.drawable.ic_interest_arrow)
            compoundDrawablePadding = 8F.dip2px()
            textSize = 12F
            text = label.title
            label.text = label.linkText
            setOnClickListener {
                DirectUtils.directToLinkPage(context, label, "发现页卡片", "")
            }
        }
    }
}