package com.remax.visualnovel.ui.chat.ui

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.remax.visualnovel.databinding.LayoutMaxNumViewBinding

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

    private val mFixedValueList = intArrayOf(200, 400, 600, 1200, 2400)
    private var mCurIndex: Int = mFixedValueList.size / 2
    private var mCurValue = mFixedValueList[mCurIndex]

    private var mBinding: LayoutMaxNumViewBinding
    private var mEventListener: IEventListener? = null


    interface IEventListener {
        fun onValueChanged(value: Int)
    }

    init {
        mBinding = LayoutMaxNumViewBinding.inflate(LayoutInflater.from(context), this, true)
        setupClickListeners()
    }


    fun setEventListener(listener: IEventListener) {
        mEventListener = listener
    }


    private fun setupClickListeners() {
        with (mBinding) {
            ivLeftIcon.setOnClickListener {
                mCurIndex = mCurIndex.takeIf { it > 0 }?.minus(1) ?: mCurIndex
                mCurValue = mFixedValueList[mCurIndex]
                tvCenter.text = mCurValue.toString()
                mEventListener?.onValueChanged(mCurValue)
            }

            ivRightIcon.setOnClickListener {
                mCurIndex = mCurIndex.takeIf { it < mFixedValueList.size - 1 }?.plus(1) ?: mCurIndex
                mCurValue = mFixedValueList[mCurIndex]
                tvCenter.text = mCurValue.toString()
                mEventListener?.onValueChanged(mCurValue)
            }
        }
    }





}