package com.gh.gamecenter.game.horizontal

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.GameHorizontalItemBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.SubjectEntity

class GameHorizontalItemViewHolder(val binding: GameHorizontalItemBinding) :
    BaseRecyclerViewHolder<GameEntity>(binding.root) {

    fun bindGameHorizontalItem(gameEntity: GameEntity, subjectEntity: SubjectEntity) {
        binding.gameRating.textSize = if (gameEntity.commentCount > 3) 12F else 10F
        binding.gameRating.goneIf("star" != subjectEntity.typeStyle, visibleCallback = {
            binding.gameRating.setDrawableStart(if (gameEntity.commentCount > 3) R.drawable.game_horizontal_rating.toDrawable() else null)
            binding.gameRating.text =
                if (gameEntity.commentCount > 3) (if (gameEntity.star == 10.0f) "10" else gameEntity.star.toString()) else ""
        })
        binding.firstRemark.text = gameEntity.assignRemark.firstLine
        binding.firstRemark.setTextColor(
            if (gameEntity.assignRemark.markedRed) Color.parseColor("#F10000") else R.color.text_title.toColor(
                binding.root.context
            )
        )
        binding.secondRemark.text = gameEntity.assignRemark.secondLine
        binding.remarkLine.setBackgroundColor(R.color.btn_gray_light.toColor(binding.root.context))
        setRemarkView(subjectEntity, gameEntity)
    }

    /**
     * 备注显示规则如下:
     * 一个专题中,只要有一个或以上的备注信息存在则显示或者隐藏占位,没有则隐藏
     */
    private fun setRemarkView(subjectEntity: SubjectEntity, gameEntity: GameEntity) {
        if (subjectEntity.typeStyle == "remark") {
            var isHasFirstRemarkDate = false
            var isHasSecondRemarkDate = false
            for (entity in subjectEntity.data!!) {
                if (!isHasFirstRemarkDate && entity.assignRemark.firstLine.isNotEmpty()) isHasFirstRemarkDate = true
                if (!isHasSecondRemarkDate && entity.assignRemark.secondLine.isNotEmpty()) isHasSecondRemarkDate = true
                if (isHasFirstRemarkDate && isHasSecondRemarkDate) break
            }
            if (isHasFirstRemarkDate) {
                if (gameEntity.assignRemark.firstLine.isNotEmpty()) {
                    binding.firstRemark.visibility = View.VISIBLE
                    binding.lineContainer.visibility = View.VISIBLE

                    binding.firstRemarkBadge.goneIf(!gameEntity.assignRemark.recommend)

                    val startTime = gameEntity.assignRemark.startTime * 1000
                    val endTime = gameEntity.assignRemark.endTime * 1000
                    val currentTime = System.currentTimeMillis()

                    val isDarkModeOn = DarkModeUtils.isDarkModeOn(binding.root.context)
                    if (startTime < currentTime
                        && (endTime > currentTime || startTime > endTime)
                    ) {
                        binding.remarkDot.setRoundedColorBackground(R.color.theme, 5F)
                    } else if (startTime == 0L && endTime > currentTime) {
                        binding.remarkDot.setRoundedColorBackground(R.color.theme, 5F)
                    } else {
                        if (isDarkModeOn) {
                            binding.remarkDot.background = GradientDrawable().apply {
                                cornerRadius = 5F.dip2px().toFloat()
                                setColor(Color.parseColor("#555555"))
                            }
                        } else {
                            binding.remarkDot.setRoundedColorBackground(R.color.text_body, 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 (isHasSecondRemarkDate) {
                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
        }
    }
}