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 var GAP = 2
    }

    private var mBinding: LayoutFontSetViewBinding

    private var mFontValue = 16  // 12, 14, 16, 18, 20
    private val mFontMinValue = 16
    private val mFontMaxValue = 20

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




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

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

            levelSeekbar.setOnLevelChangeListener(object : LevelSeekBar.OnLevelChangeListener {
                override fun onLevelChanged(
                    seekBar: LevelSeekBar,
                    level: Int,
                    fromUser: Boolean
                ) {
                    mFontValue = 16 + level * GAP
                    if (mFontValue > mFontMaxValue) {
                        mFontValue = mFontMaxValue
                    }
                    if (mFontValue < mFontMinValue) {
                        mFontValue = mFontMinValue
                    }
                    tvFontValue.text = mFontValue.toString()
                }

                override fun onStartTrackingTouch(seekBar: LevelSeekBar) {

                }

                override fun onStopTrackingTouch(seekBar: LevelSeekBar) {

                }

            })

        }
    }




}