package com.remax.visualnovel.widget.uitoken.view

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import com.remax.visualnovel.R
import com.remax.visualnovel.widget.uitoken.bean.CustomViewToken
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeOutline
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont

/**
 * Created by HJW on 2022/8/31
 *
 */
open class UITokenEditView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = androidx.appcompat.R.attr.editTextStyle) :
    AppCompatEditText(context, attrs, defStyleAttr), UIView {

    protected val customViewToken = CustomViewToken()

    init {
        includeFontPadding = false
        /**
         * 自定义控件属性
         */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UITokenEditView)
        customViewToken.run {
            textUITextToken = typedArray.getString(R.styleable.UITokenEditView_textToken) ?: ""
            textUIColorToken = typedArray.getString(R.styleable.UITokenEditView_textColorToken) ?: ""
            textUIHintColorToken = typedArray.getString(R.styleable.UITokenEditView_textHintColorToken) ?: ""
            textUIDisabledColorToken = typedArray.getString(R.styleable.UITokenEditView_textDisabledColorToken) ?: ""
            backgroundUIColorToken = typedArray.getString(R.styleable.UITokenEditView_backgroundColorToken) ?: ""
            backgroundUIPressedColorToken = typedArray.getString(R.styleable.UITokenEditView_backgroundPressedColorToken) ?: ""
            backgroundUIHoveredColorToken = typedArray.getString(R.styleable.UITokenEditView_backgroundHoveredColorToken) ?: ""
            backgroundUIDisabledColorToken = typedArray.getString(R.styleable.UITokenEditView_backgroundDisabledColorToken) ?: ""
            val radius = typedArray.getString(R.styleable.UITokenEditView_radiusToken) ?: ""
            radiusToken = radius
            topLeftRadiusToken = (typedArray.getString(R.styleable.UITokenEditView_topLeftRadiusToken) ?: "").ifEmpty { radius }
            topRightRadiusToken = (typedArray.getString(R.styleable.UITokenEditView_topRightRadiusToken) ?: "").ifEmpty { radius }
            bottomLeftRadiusToken = (typedArray.getString(R.styleable.UITokenEditView_bottomLeftRadiusToken) ?: "").ifEmpty { radius }
            bottomRightRadiusToken = (typedArray.getString(R.styleable.UITokenEditView_bottomRightRadiusToken) ?: "").ifEmpty { radius }
            strokeUIWidthToken = typedArray.getString(R.styleable.UITokenEditView_strokeWidthToken) ?: ""
            strokeUIColorToken = typedArray.getString(R.styleable.UITokenEditView_strokeColorToken) ?: ""
            strokeUIPressedColorToken = typedArray.getString(R.styleable.UITokenEditView_strokePressedColorToken) ?: ""
            strokeUIHoveredColorToken = typedArray.getString(R.styleable.UITokenEditView_strokeHoveredColorToken) ?: ""
            strokeUIDisabledColorToken = typedArray.getString(R.styleable.UITokenEditView_strokeDisabledColorToken) ?: ""

            //<!-- new added 2025.10.29 -->
            advBgColor = typedArray.getColor(R.styleable.UITokenEditView_advBgColor, advBgColor)
            advRadius = typedArray.getDimension(R.styleable.UITokenEditView_advRadius, advRadius)
            advTopLeftRadius = typedArray.getDimension(R.styleable.UITokenEditView_advTopLeftRadius, advTopLeftRadius)
            advTopRightRadius = typedArray.getDimension(R.styleable.UITokenEditView_advTopRightRadius, advTopRightRadius)
            advBottomRightRadius = typedArray.getDimension(R.styleable.UITokenEditView_advBottomRightRadius, advBottomRightRadius)
            advBottomLeftRadius = typedArray.getDimension(R.styleable.UITokenEditView_advBottomLeftRadius, advBottomLeftRadius)
            advStrokeColor = typedArray.getColor(R.styleable.UITokenEditView_advStrokeColor, advStrokeColor)
            advStrokeWidth = typedArray.getDimension(R.styleable.UITokenEditView_advStrokeWidth, advStrokeWidth)
        }

        typedArray.recycle()

        changeTextFont(customViewToken)
        changeTextColor(customViewToken)
        changeBackground(customViewToken)
        changeOutline(customViewToken)
    }


    override fun getUITokenView(): CustomViewToken {
        return customViewToken
    }
}