package com.develop.food.base.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.develop.base.R
import com.develop.base.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
        }
        when (starCount) {
            0 -> {
                binding.star1.setBackgroundResource(R.drawable.ic_star)
                binding.star2.setBackgroundResource(R.drawable.ic_star)
                binding.star3.setBackgroundResource(R.drawable.ic_star)
                binding.star4.setBackgroundResource(R.drawable.ic_star)
                binding.star5.setBackgroundResource(R.drawable.ic_star)
            }
            1 -> {
                binding.star1.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star2.setBackgroundResource(R.drawable.ic_star)
                binding.star3.setBackgroundResource(R.drawable.ic_star)
                binding.star4.setBackgroundResource(R.drawable.ic_star)
                binding.star5.setBackgroundResource(R.drawable.ic_star)
            }
            2 -> {
                binding.star1.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star2.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star3.setBackgroundResource(R.drawable.ic_star)
                binding.star4.setBackgroundResource(R.drawable.ic_star)
                binding.star5.setBackgroundResource(R.drawable.ic_star)
            }
            3 -> {
                binding.star1.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star2.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star3.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star4.setBackgroundResource(R.drawable.ic_star)
                binding.star5.setBackgroundResource(R.drawable.ic_star)
            }
            4 -> {
                binding.star1.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star2.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star3.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star4.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star5.setBackgroundResource(R.drawable.ic_star)
            }
            5 -> {
                binding.star1.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star2.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star3.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star4.setBackgroundResource(R.drawable.ic_star_selected)
                binding.star5.setBackgroundResource(R.drawable.ic_star_selected)
            }
        }
    }

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