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

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.GameHorizontalItemCustomBinding
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.home.custom.adapter.CustomViewExt

class CustomGameHorizontalItemViewHolder(val binding: GameHorizontalItemCustomBinding)
    : BaseRecyclerViewHolder<GameEntity>(binding.root), IExposureProvider
{
    private var boundedGameEntity: GameEntity? = null

    fun bindGameHorizontalItem(
        gameEntity: GameEntity,
        subjectEntity: SubjectEntity,
        isShowFirstLine: Boolean,
        isShowSecondLine: Boolean
    ) {
        val showStar = subjectEntity.typeStyle == "star"
                && subjectEntity.data?.any { it.commentCount > 3 } ?: false
        CustomViewExt.setGameRattingWithSubject(binding.gameRating, showStar, gameEntity)

        boundedGameEntity = gameEntity

        binding.firstRemark.text = gameEntity.assignRemark.firstLine
        binding.firstRemark.setTextColor(
            if (gameEntity.assignRemark.markedRed) Color.parseColor("#F10000") else com.gh.gamecenter.common.R.color.text_primary.toColor(
                binding.root.context
            )
        )
        binding.secondRemark.text = gameEntity.assignRemark.secondLine
        binding.remarkLine.setBackgroundColor(com.gh.gamecenter.common.R.color.btn_gray_light.toColor(binding.root.context))
        setRemarkView(subjectEntity, gameEntity, isShowFirstLine, isShowSecondLine)
    }

    /**
     * 备注显示规则如下:
     * 一个专题中,只要有一个或以上的备注信息存在则显示或者隐藏占位,没有则隐藏
     */
    private fun setRemarkView(
        subjectEntity: SubjectEntity,
        gameEntity: GameEntity,
        isShowFirstLine: Boolean,
        isShowSecondLine: Boolean
    ) {
        if (subjectEntity.typeStyle == "remark") {
            if (isShowFirstLine) {
                if (gameEntity.assignRemark.firstLine.isNotEmpty()) {
                    binding.firstRemark.visibility = View.VISIBLE
                    binding.lineContainer.visibility = View.VISIBLE

                    binding.firstRemarkBadge.goneIf(!gameEntity.assignRemark.recommend) {
                        binding.firstRemarkBadge.setImageResource(com.gh.gamecenter.R.drawable.ic_recommend)
                    }

                    val startTime = gameEntity.assignRemark.startTime * 1000
                    val endTime = gameEntity.assignRemark.endTime * 1000
                    val currentTime = System.currentTimeMillis()
                    val isDarkModeOn = DarkModeUtils.isDarkModeOn(binding.root.context)
                    when {
                        startTime < currentTime && (endTime > currentTime || startTime > endTime) -> {
                            binding.remarkDot.setRoundedColorBackground(com.gh.gamecenter.common.R.color.primary_theme, 5F)
                        }

                        startTime == 0L && endTime > currentTime -> {
                            binding.remarkDot.setRoundedColorBackground(com.gh.gamecenter.common.R.color.primary_theme, 5F)
                        }

                        isDarkModeOn -> {
                            binding.remarkDot.background = GradientDrawable().apply {
                                cornerRadius = 5F.dip2px().toFloat()
                                setColor(Color.parseColor("#555555"))
                            }
                        }

                        else -> {
                            binding.remarkDot.setRoundedColorBackground(com.gh.gamecenter.common.R.color.text_instance, 5F)
                        }
                    }
                } else {
                    binding.firstRemark.visibility = View.INVISIBLE
                    binding.firstRemarkBadge.visibility = View.INVISIBLE
                    binding.lineContainer.visibility = View.INVISIBLE
                }
            } else {
                binding.firstRemark.visibility = View.GONE
                binding.firstRemarkBadge.visibility = View.GONE
                binding.lineContainer.visibility = View.GONE
            }

            if (isShowSecondLine) {
                if (gameEntity.assignRemark.secondLine.isNotEmpty()) {
                    binding.secondRemark.visibility = View.VISIBLE
                } else {
                    binding.secondRemark.visibility = View.INVISIBLE
                }
            } else {
                binding.secondRemark.visibility = View.GONE
            }
        } else {
            binding.firstRemark.visibility = View.GONE
            binding.firstRemarkBadge.visibility = View.GONE
            binding.secondRemark.visibility = View.GONE
            binding.lineContainer.visibility = View.GONE
        }
    }

    override fun provideExposureData(): ExposureEvent? {
        return boundedGameEntity?.exposureEvent?.getFreshExposureEvent()
    }
}