package com.remax.visualnovel.widget.ui

import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import androidx.core.content.withStyledAttributes

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

    private val viewSize = 20.dp

    var isChecked = false
    private var normalDrawable = R.mipmap.radio_normal

    init {
        context.withStyledAttributes(attrs, R.styleable.RadioCheckButton) {
            isChecked = getBoolean(R.styleable.RadioCheckButton_radioCheck, false)
            normalDrawable = getResourceId(R.styleable.RadioCheckButton_radioNormalDrawable, R.mipmap.radio_normal)
        }
        viewChecked(isChecked)
    }

    fun viewChecked(isChecked: Boolean) {
        this.isChecked = isChecked
        if (isChecked) {
            setBackgroundResource(R.mipmap.icon_checked)
        } else {
            setBackgroundResource(normalDrawable)
        }
    }

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