package com.remax.visualnovel.widget.ui

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView
import androidx.core.content.withStyledAttributes

/**
 * Created by HJW on 2022/8/31
 *
 */
class RadioOnPicButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenTextView(context, attrs, defStyleAttr) {

    var isChecked = false

    init {
        context.withStyledAttributes(attrs, R.styleable.RadioOnPicButton) {
            isChecked = getBoolean(R.styleable.RadioOnPicButton_radioCheck, false)
        }
        changeTextColor {
            textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
        }
        changeTextFont {
            textUITextToken = context.getString(R.string.txt_numMonotype_xs)
        }
        changeBackground {
            radiusToken = context.getString(R.string.radius_pill)
        }
        viewChecked(isChecked)
        height = 20.dp
        width = 20.dp
        gravity = Gravity.CENTER
    }

    fun viewChecked(isChecked: Boolean) {
        this.isChecked = isChecked
        changeBackground {
            backgroundUIColorToken = if (isChecked) {
                context.getString(R.string.color_primary_normal)
            } else {
                context.getString(R.string.color_surface_element_dark_normal)
            }
        }
    }

//    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        if (View.MeasureSpec.getMode(widthMeasureSpec) == View.MeasureSpec.EXACTLY && View.MeasureSpec.getMode(heightMeasureSpec) == View.MeasureSpec.EXACTLY) {
//            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec)
//        } else {
//            setMeasuredDimension(viewSize, viewSize)
//        }
//    }
}