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.LayoutFontSetViewBinding


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

    companion object {
        private const val GAP = 2
        private const val MIN_VALUE = 12
        private const val MAX_VALUE = 20
    }

    private var mBinding: LayoutFontSetViewBinding =
        LayoutFontSetViewBinding.inflate(LayoutInflater.from(context), this, true)


    private var mFontValue = MIN_VALUE + GAP * 2

    init {
        setupClickListeners()
    }




    private fun setupClickListeners() {
        with (mBinding) {
            ivFontMinus.setOnClickListener {
                if (mFontValue > MIN_VALUE) {
                    mFontValue -= GAP
                }
                tvFontValue.text = mFontValue.toString()
                levelSeekbar.setLevel((mFontValue - MIN_VALUE)/GAP)
            }

            ivFontAdd.setOnClickListener {
                if (mFontValue < MAX_VALUE) {
                    mFontValue += GAP
                }
                tvFontValue.text = mFontValue.toString()
                levelSeekbar.setLevel((mFontValue - MIN_VALUE)/GAP)
            }

            levelSeekbar.setOnLevelChangeListener { level->
                mFontValue = MIN_VALUE + level * GAP
                if (mFontValue > MAX_VALUE) {
                    mFontValue = MAX_VALUE
                }
                if (mFontValue < MIN_VALUE) {
                    mFontValue = MIN_VALUE
                }
                tvFontValue.text = mFontValue.toString()
            }

        }
    }




}