package com.gh.gamecenter.home.test_v2

import android.view.ViewGroup.MarginLayoutParams
import androidx.core.content.ContextCompat
import androidx.core.view.isInvisible
import androidx.core.view.updateLayoutParams
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ItemHomeGameTestV2TimePointBinding

/**
 * @author : liujiarui
 * date : 2023/3/3
 * description :
 */
class HomeGameTestV2TimePointViewHolder(
    val binding: ItemHomeGameTestV2TimePointBinding
) : BaseRecyclerViewHolder<Any>(binding.root) {

    fun bind(data: String, isStart: Boolean, isEnd: Boolean, isSelected: Boolean) {
        binding.tvTimePoint.text = data
        reset()
        if (isStart) isStartNode()
        if (isEnd) isEndNode()
        if (isSelected) isSelected()
    }

    private fun reset() {
        val context = binding.root.context
        binding.lineLeft.isInvisible = false
        binding.lineLeft.setBackgroundColor(R.color.btn_gray_light.toColor(context))
        binding.lineRight.isInvisible = false
        binding.lineRight.setBackgroundColor(R.color.btn_gray_light.toColor(context))
        binding.point.isEnabled = false
        binding.point.background =
            ContextCompat.getDrawable(context, R.drawable.ic_home_game_test_v2_time_point_dot)
        binding.tvTimePoint.isChecked = false
        binding.tvTimePoint.textSize = 12f
        binding.tvTimePoint.paint.isFakeBoldText = false
        val color = context.resources.getColorStateList(R.color.line_axis_text_selector)
        binding.tvTimePoint.setTextColor(color)
        binding.root.updateLayoutParams {
            (this as? MarginLayoutParams)?.leftMargin = 0f.dip2px()
        }
    }

    private fun isSelected() {
        binding.point.isEnabled = true
        binding.tvTimePoint.isChecked = true
        binding.tvTimePoint.textSize = 16f
        binding.tvTimePoint.paint.isFakeBoldText = true
    }

    private fun isStartNode() {
        binding.lineLeft.isInvisible = true
        binding.root.updateLayoutParams {
            (this as? MarginLayoutParams)?.leftMargin = 16f.dip2px()
        }
    }

    private fun isEndNode() {
        binding.lineRight.isInvisible = true
    }
}