package com.develop.common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.develop.base.ext.load
import com.develop.base.ext.updateText
import com.develop.common.R
import com.develop.common.databinding.StarViewBinding


class StarView : LinearLayout {
    private var binding: StarViewBinding

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    init {
        val root = View.inflate(context, R.layout.star_view, this)
        binding = StarViewBinding.bind(root)
    }

    fun setStarCount(count: Int) {
        var starCount = count
        if (starCount > 5 || starCount < 0) {
            starCount = 0
        }
        binding.star1.load(R.drawable.ic_star_unselected)
        binding.star2.load(R.drawable.ic_star_unselected)
        binding.star3.load(R.drawable.ic_star_unselected)
        binding.star4.load(R.drawable.ic_star_unselected)
        binding.star5.load(R.drawable.ic_star_unselected)
        when (starCount) {
            1 -> {
                binding.star1.load(R.drawable.ic_star_selected)
            }
            2 -> {
                binding.star1.load(R.drawable.ic_star_selected)
                binding.star2.load(R.drawable.ic_star_selected)
            }
            3 -> {
                binding.star1.load(R.drawable.ic_star_selected)
                binding.star2.load(R.drawable.ic_star_selected)
                binding.star3.load(R.drawable.ic_star_selected)
            }
            4 -> {
                binding.star1.load(R.drawable.ic_star_selected)
                binding.star2.load(R.drawable.ic_star_selected)
                binding.star3.load(R.drawable.ic_star_selected)
                binding.star4.load(R.drawable.ic_star_selected)
            }
            5 -> {
                binding.star1.load(R.drawable.ic_star_selected)
                binding.star2.load(R.drawable.ic_star_selected)
                binding.star3.load(R.drawable.ic_star_selected)
                binding.star4.load(R.drawable.ic_star_selected)
                binding.star5.load(R.drawable.ic_star_selected)
            }
        }

    }

    fun setCommentNumber(number: String) {
        binding.tvNumber.updateText("(${number})")
    }

    fun setCommentTextColorGray() {
        binding.tvNumber.setTextColor(resources.getColor(R.color.color_A3A3A2))
    }
}