package com.adealink.weparty.commonui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import com.adealink.weparty.R
import com.adealink.weparty.commonui.widget.util.getColorX
import com.adealink.weparty.databinding.LayoutNumberIncreaseViewBinding

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

    companion object {
        private const val MIN_NUMBER = 1
        private const val MAX_NUMBER = 99
    }

    private val binding =
        LayoutNumberIncreaseViewBinding.inflate(LayoutInflater.from(context), this)

    private var numberTextColor = getColorX(R.color.color_FF1D2129)

    private var numberAddIc = R.drawable.common_number_add_ic
    private var numberMinusIc = R.drawable.common_number_minus_ic
    private var numberMinusDisableIc = R.drawable.common_number_minus_diable_ic

    private var number = 0

    private var listener: OnNumberChangedListener? = null

    init {
        context.withStyledAttributes(
            attrs,
            R.styleable.NumberIncreaseView
        ) {
            number = getInt(
                R.styleable.NumberIncreaseView_default_number,
                0
            ).coerceIn(MIN_NUMBER, MAX_NUMBER)

            numberTextColor = getColor(
                R.styleable.NumberIncreaseView_number_text_color,
                numberTextColor
            )

            numberAddIc = getResourceId(
                R.styleable.NumberIncreaseView_number_add_src,
                numberAddIc
            )

            numberMinusIc = getResourceId(
                R.styleable.NumberIncreaseView_number_minus_src,
                numberMinusIc
            )

            numberMinusDisableIc = getResourceId(
                R.styleable.NumberIncreaseView_number_minus_disable_src,
                numberMinusDisableIc
            )
        }

        binding.tvNumber.text = number.toString()
        binding.tvNumber.setTextColor(numberTextColor)

        binding.ivMinus.setImageResource(numberMinusIc)
        binding.ivMinus.setOnClickListener {
            clickMinus()
        }

        binding.ivAdd.setImageResource(numberAddIc)
        binding.ivAdd.setOnClickListener {
            clickAdd()
        }
    }

    private fun clickMinus() {
        setNumber(number - 1)
    }

    private fun clickAdd() {
        setNumber(number + 1)
    }

    fun getNumber(): Int {
        return number
    }

    fun setNumber(number: Int) {
        val nextNumber = number.coerceIn(MIN_NUMBER, MAX_NUMBER)
        if (this.number == nextNumber) {
            return
        }
        this.number = nextNumber
        binding.tvNumber.text = number.toString()
        if (this.number <= MIN_NUMBER) {
            binding.ivMinus.setImageResource(numberMinusDisableIc)
        } else {
            binding.ivMinus.setImageResource(numberMinusIc)
        }
        listener?.onNumberChanged(nextNumber)
    }

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

    interface OnNumberChangedListener {
        fun onNumberChanged(number: Int)
    }

}