package com.gh.gamecenter.home.test_v2

import androidx.core.view.isVisible
import com.gh.common.databind.BindingAdapters
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.setDrawableStart
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.ItemHomeGameTestV2ItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.game.GameItemViewHolder
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author : liujiarui
 * date : 2023/3/6
 * description :
 */
class HomeGameTestV2GameListViewHolder(
    private val mBinding: ItemHomeGameTestV2ItemBinding
) : GameViewHolder(mBinding.root) {

    companion object {
        private val mDayList = TimeUtils.getTodayYesterdayAndTomorrow()
        private val mWeekList = TimeUtils.getCurrentPreviousAndNextWeek()
    }

    init {
        gameName = mBinding.gameName
        gameDownloadBtn = mBinding.downloadBtn
        gameDes = mBinding.gameDes
        gameRating = mBinding.gameRating
        recommendContainer = mBinding.recommendContainer
        recommendIv = mBinding.recommendIv
        multiVersionDownloadTv = mBinding.multiVersionDownloadTv
        gameDownloadTips = mBinding.downloadTipsLottie
    }

    fun bindGameItem(gameEntity: GameEntity) {
        mBinding.run {
            gameIconView.displayGameIcon(gameEntity)
            BindingAdapters.setGameName(gameName, gameEntity, false, null)
            BindingAdapters.setGameTags(labelList, gameEntity)

            GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, gameSubtitleTv)

            gameRating.textSize = if (gameEntity.commentCount > 3) 12F else 10F
            gameRating.setDrawableStart(if (gameEntity.commentCount > 3) R.drawable.game_horizontal_rating.toDrawable() else null)
            gameRating.text = if (gameEntity.commentCount > 3) {
                if (gameEntity.star == 10.0F) "10" else gameEntity.star.toString()
            } else ""
            gameRating.setPadding(0, 0, if (gameEntity.commentCount > 3) 8F.dip2px() else 0, 0)
            gameRating.setTextColor(
                if (gameEntity.commentCount > 3) {
                    R.color.theme_font.toColor(gameRating.context)
                } else {
                    R.color.theme.toColor(gameRating.context)
                }
            )

            //开服日期
            val gameTimeText = getGameTimeText(gameEntity)
            gameDes.text = gameTimeText
            gameDes.isVisible = gameTimeText.isNotEmpty()


            fixViewHolderTheme(gameEntity)
        }
    }

    /**
     * 修复切换深色模式后，条目复用颜色没有变化的问题
     */
    private fun fixViewHolderTheme(gameEntity: GameEntity) {
        val context = mBinding.root.context
        gameName.setTextColor(R.color.text_title.toColor(context))
        gameDownloadBtn.background = R.drawable.download_button_normal_style.toDrawable(context)
        gameDes.setTextColor(R.color.text_title.toColor(context))
        var gameRatingTextColor = R.color.theme.toColor(context)
        if (gameEntity.commentCount > 3) {
            gameRatingTextColor = R.color.theme_font.toColor(context)
        }
        gameRating?.setTextColor(gameRatingTextColor)
    }

    private fun getGameTimeText(gameEntity: GameEntity): String {
        val time = gameEntity.time?.time ?: return ""
        val timeText = getTimeText(time)
        val typeText = when (gameEntity.eventType) {
            "test" -> "开启测试"
            else -> "首发上线"
        }
        return "$timeText $typeText"
    }

    /**
     * @param time 接口返回的时间 单位：秒
     */
    private fun getTimeText(time: Long): String {
        val sdf = SimpleDateFormat("MM.dd", Locale.CHINA)
        return try {
            val date = sdf.format(time * 1000)
            val day = mDayList.find { it.first == date }
            val text = if (day != null && day.second in -1..1) {
                when (day.second) {
                    -1 -> "昨天"
                    0 -> "今天"
                    else -> "明天"
                }
            } else {
                mWeekList.find {
                    it.first == date
                }?.second ?: date
            }
            text + " " + getTimeString(time)
        } catch (e: Exception) {
            TimeUtils.getFormatDate(time)
        }
    }

    /**
     * 获取日期的时间
     * @param time 接口返回的时间 单位：秒
     */
    private fun getTimeString(time: Long): String {
        val sdf = SimpleDateFormat("HH:mm", Locale.CHINA)
        return try {
            sdf.format(time * 1000)
        } catch (e: Exception) {
            ""
        }
    }
}