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 evaluateEnable = true

    private var maxScore = 10 //最高评分
    private var curScore = 10 //当前评分

    private var itemViewSize = 10
    private var itemDivide = 4

    private var normalResourceId: Int = R.drawable.common_evaluate_star_normal_ic

    private var halfSelectedResourceId: Int = R.drawable.common_evaluate_star_half_selected_ic
    private var selectedResourceId: Int = R.drawable.common_evaluate_star_selected_ic

    private var listener: OnEvaluateListener? = null

    init {
        context.withStyledAttributes(
            attrs,
            R.styleable.EvaluateView
        ) {
            evaluateEnable = getBoolean(
                R.styleable.EvaluateView_evaluate_enable,
                true
            )

            val maxScore = getInt(
                R.styleable.EvaluateView_evaluate_max,
                maxScore
            )
            this@EvaluateView.maxScore = maxScore * 2
            val score = getInt(
                R.styleable.EvaluateView_evaluate_current,
                0
            )
            this@EvaluateView.curScore = score * 2

            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
            )
            halfSelectedResourceId = getResourceId(
                R.styleable.EvaluateView_evaluate_half_selected_src,
                R.drawable.common_evaluate_star_half_selected_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: Float) {
        if (score < 0) {
            return
        }
        val toMaxScore = (score * 2).toInt()
        if (maxScore != toMaxScore) {
            inflateEvaluateView()
        }
    }

    fun setScore(score: Float) {
        val toScore = (score * 2).toInt()
        setCurScore(toScore)
    }

    private fun setCurScore(score: Int) {
        if (curScore == score) {
            return
        }
        curScore = min(score, maxScore)
        for (i in 0 until childCount) {
            val itemView = getChildAt(i) as? AppCompatImageView ?: return
            val iFrom = i * 2
            val iTo = i * 2 + 2
            if (curScore > iFrom && curScore < iTo) {
                itemView.setImageResource(halfSelectedResourceId)
            } else if (curScore >= iTo) {
                itemView.setImageResource(selectedResourceId)
            } else {
                itemView.setImageResource(normalResourceId)
            }
        }
        listener?.onEvaluate(curScore * 1f / 2)
    }

    private fun inflateEvaluateView() {
        removeAllViews()
        val childCount = maxScore / 2
        for (i in 0 until childCount) {
            val itemView = AppCompatImageView(context)
            itemView.tag = (i + 1) * 2
            if (evaluateEnable) {
                itemView.setOnClickListener {
                    setCurScore(getNextScore((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)
            }
        }
    }

    private fun getNextScore(score: Int): Int {
        return if (this.curScore == score) {
            score - 1
        } else if (this.curScore == score - 1) {
            score
        } else {
            score
        }
    }

    fun getScore(): Float {
        return curScore * 0.5f
    }

    interface OnEvaluateListener {
        fun onEvaluate(score: Float)
    }

}