package com.adealink.weparty.commonui.widget

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.content.withStyledAttributes
import com.adealink.weparty.R
import kotlin.math.min

class EvaluateView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayoutCompat(context, attrs, defStyleAttr) {

    private var maxScore = 5 //最高评分
    private var curScore = 5 //当前评分
    private var itemViewSize = 10
    private var itemDivide = 4

    private var normalResourceId: Int = R.drawable.common_evaluate_star_normal_ic
    private var selectedResourceId: Int = R.drawable.common_evaluate_star_selected_ic

    private var listener: OnEvaluateListener? = null

    init {
        context.withStyledAttributes(
            attrs,
            R.styleable.EvaluateView
        ) {
            maxScore = getInt(
                R.styleable.EvaluateView_evaluate_max,
                maxScore
            )
            curScore = getInt(
                R.styleable.EvaluateView_evaluate_current,
                curScore
            )
            itemViewSize = getDimensionPixelSize(
                R.styleable.EvaluateView_evaluate_width,
                itemViewSize
            )
            itemDivide = getDimensionPixelSize(
                R.styleable.EvaluateView_evaluate_divide,
                itemDivide
            )
            normalResourceId = getResourceId(
                R.styleable.EvaluateView_evaluate_normal_src,
                R.drawable.common_evaluate_star_normal_ic
            )
            selectedResourceId = getResourceId(
                R.styleable.EvaluateView_evaluate_selected_src,
                R.drawable.common_evaluate_star_selected_ic
            )
        }

        inflateEvaluateView()
    }

    fun setListener(listener: OnEvaluateListener) {
        this.listener = listener
    }

    fun setMaxScore(score: Int) {
        if (score < 0) {
            return
        }
        if (maxScore != score) {
            inflateEvaluateView()
        }
    }

    fun setCurScore(score: Float) {
        setCurScore(score.toInt())
    }

    fun setCurScore(score: Int) {
        if (curScore == score) {
            return
        }
        curScore = min(score, childCount)
        for (i in 0 until childCount) {
            val itemView = getChildAt(i) as? AppCompatImageView ?: return
            if (i < score) {
                itemView.setImageResource(selectedResourceId)
            } else {
                itemView.setImageResource(normalResourceId)
            }
        }
        listener?.onEvaluate(curScore)
    }

    private fun inflateEvaluateView() {
        removeAllViews()
        for (i in 0 until maxScore) {
            val itemView = AppCompatImageView(context)
            itemView.tag = (i + 1)
            itemView.setOnClickListener {
                setCurScore((itemView.tag as? Int) ?: 0)
            }
            //添加控件
            if (i > 0) {
                addView(itemView, LayoutParams(itemViewSize, itemViewSize).apply {
                    marginStart = itemDivide
                })
            } else {
                addView(itemView, LayoutParams(itemViewSize, itemViewSize))
            }

            if (i < curScore) {
                itemView.setImageResource(selectedResourceId)
            } else {
                itemView.setImageResource(normalResourceId)
            }
        }
    }

    fun getScore(): Int {
        return curScore
    }

    interface OnEvaluateListener {
        fun onEvaluate(score: Int)
    }

}