package com.gh.gamecenter.home.custom.viewholder

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
import com.gh.common.util.HomePluggableHelper
import com.gh.gamecenter.R
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.GamePluginItemCustomBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.home.PluginDisplayStatus
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.adapter.CustomGamePluginAdapter
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.OtherItemEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomPluginItem
import java.util.*

/**
 * Created by khy on 15/08/17.
 * 插件化区域
 */
class CustomGamePluginViewHolder(
    private val makeItemBackgroundTransparent: Boolean,
    viewModel: CustomPageViewModel,
    val binding: GamePluginItemCustomBinding
) : BaseCustomViewHolder(viewModel, binding.root) {

    private val context: Context
        get() = itemView.context

    override val childEventHelper by lazy(LazyThreadSafetyMode.NONE) {
        OtherItemEventHelper(viewModel)
    }

    private var pluginDisplayStatus = PluginDisplayStatus.DEFAULT

    private var isManualChangedDisplayStatus = false

    override val onlyNotifyItemChanged: Boolean
        get() = true

    private val adapter by lazy {
        CustomGamePluginAdapter(
            context,
            viewModel,
            childEventHelper,
            object : CustomGamePluginAdapter.IUpdateDisplayType {
                override fun update(status: PluginDisplayStatus) {
                    if (!isManualChangedDisplayStatus) {
                        pluginDisplayStatus = status
                    }
                }
            })
    }

    init {
        binding.recyclerView.itemAnimator = null
    }

    fun bindView(item: CustomPageItem, notifyPluginCallback: () -> Unit) {
        super.bindView(item)
        if (item is CustomPluginItem) {
            fillExposureEvent(item)

            val pluginList = item.data
            if (pluginList.isEmpty()) {
                binding.pluginHead.goneIf(true)
                binding.pluginExtend.goneIf(true)
                binding.pluginExtendContainer.goneIf(true)
                binding.recyclerView.adapter = null
            } else {
                binding.pluginHead.goneIf(false)
                binding.pluginExtend.goneIf(false)
                binding.pluginExtendContainer.goneIf(false)
                if (makeItemBackgroundTransparent) {
                    itemView.setBackgroundColor(com.gh.gamecenter.common.R.color.transparent.toColor(context))
                }

                if (pluginDisplayStatus == PluginDisplayStatus.DEFAULT) {
                    pluginDisplayStatus = HomePluggableHelper.getPluginDisplayedStyle(pluginList)
                }

                if (binding.recyclerView.adapter == null) {
                    binding.recyclerView.adapter = adapter
                }
                adapter.openList(pluginDisplayStatus)
                adapter.setData(pluginList)

                if ((pluginDisplayStatus == PluginDisplayStatus.OPEN_AND_BUTTON || pluginDisplayStatus == PluginDisplayStatus.OPEN_TWO_AND_BUTTON)) {
                    binding.pluginExtendContainer.visibility = View.VISIBLE
                    binding.pluginHeadOpen.visibility = View.GONE
                    if (pluginDisplayStatus == PluginDisplayStatus.OPEN_TWO_AND_BUTTON) {
                        binding.pluginExtend.text = "展开"
                        binding.icPluginExtend.setImageResource(R.drawable.home_plugin_bottom_open)
                        binding.pluginExtend.setTextColor(com.gh.gamecenter.common.R.color.primary_theme.toColor(context))
                    } else {
                        binding.pluginExtend.text = "收起"
                        binding.icPluginExtend.setImageResource(R.drawable.home_plugin_bottom_close)
                        binding.pluginExtend.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
                    }
                } else {
                    binding.pluginExtendContainer.visibility = View.GONE
                    binding.pluginHeadOpen.visibility = View.VISIBLE
                }

                if (pluginDisplayStatus == PluginDisplayStatus.CLOSE) {
                    binding.pluginHeadOpen.setImageResource(R.drawable.home_plugin_open)
                    binding.pluginHeadIcon.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                        bottomMargin = 16F.dip2px()
                    }
                } else {
                    binding.pluginHeadOpen.setImageResource(R.drawable.home_plugin_close)
                    binding.pluginHeadIcon.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                        bottomMargin = 8F.dip2px()
                    }
                }

                binding.pluginHeadTitle.text =
                    String.format(Locale.getDefault(), "你有%d个游戏可以升级插件版", pluginList.size)
                val expandClick: (View) -> Unit = {
                    if (it.id != R.id.plugin_head || binding.pluginExtendContainer.visibility == View.GONE) {
                        if (pluginDisplayStatus == PluginDisplayStatus.CLOSE || pluginDisplayStatus == PluginDisplayStatus.OPEN_TWO_AND_BUTTON) {
                            if (pluginDisplayStatus == PluginDisplayStatus.CLOSE) pluginDisplayStatus =
                                PluginDisplayStatus.OPEN
                            if (pluginDisplayStatus == PluginDisplayStatus.OPEN_TWO_AND_BUTTON) pluginDisplayStatus =
                                PluginDisplayStatus.OPEN_AND_BUTTON
                        } else {
                            pluginDisplayStatus = PluginDisplayStatus.CLOSE
                        }
                        isManualChangedDisplayStatus = true
                        binding.recyclerView.recycledViewPool.clear()
                        notifyPluginCallback()
                    }

                    val text =
                        if (pluginDisplayStatus == PluginDisplayStatus.CLOSE || pluginDisplayStatus == PluginDisplayStatus.OPEN_TWO_AND_BUTTON) {
                            "收起"
                        } else {
                            "展开"
                        }
                    childEventHelper.trackPluginAreaClick(text, null)
                }
                binding.pluginExtendContainer.setOnClickListener(View.OnClickListener(function = expandClick))
                binding.pluginHead.setOnClickListener(View.OnClickListener(function = expandClick))
            }
        }
    }

    private fun fillExposureEvent(item: CustomPluginItem) {
        runOnIoThread(isLightWeightTask = true) {
            item.data.forEachIndexed { index, game ->
                game.pageLevelString = pageLocation.pageLevelString
                game.exposureEvent = createExposureEvent(
                    game,
                    listOf(ExposureSource("插件化", "")),
                    pageConfigure.exposureSourceList,
                    index,
                    item.componentPosition,
                    createTrackData(item)
                )
            }
        }
    }
}