package com.mime.dualscreenview.view

import android.content.Context
import android.util.AttributeSet
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton
import androidx.constraintlayout.widget.ConstraintLayout
import com.mime.dualscreenview.R

import java.lang.Exception

open class SideButtonTextView : ConstraintLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)

    var leftButtonTitle : String = ""
        set(value) {
            field = value
            btn_decrement?.text = field
        }

    var rightButtonTitle : String = ""
        set(value) {
            field = value
            btn_increment?.text = field
        }

    open var value : Int = 14
        set(newValue) {
            field = newValue
            text_value?.text = displayFormat.format(field)
            mValueChange?.invoke(field)
        }

    var displayFormat = "글자 크기 : %d"
        set(newValue) {
            field = newValue
            try {
                text_value?.text = displayFormat.format(value)
            } catch (e : Exception) {

            }
        }

    var mValueChange : ValueChange? = null
        set(newValue) {
            field = newValue
            if (newValue != null) {
                newValue(value)
            }
        }

    var btn_decrement : AppCompatButton? = null
    var btn_increment : AppCompatButton? = null
    var text_value : TextView? = null

    init {
        inflate(context, R.layout.layout_steps_editor,this)
        btn_increment = findViewById(R.id.btn_increment)
        btn_decrement = findViewById(R.id.btn_decrement)
        text_value = findViewById(R.id.text_value)
        btn_decrement?.setOnClickListener { value = value.dec() }
        btn_increment?.setOnClickListener { value = value.inc() }
    }
}