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

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
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.expend.dsl.expand

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

    val customViewToken = CustomViewToken()

    init {
        /**
         * 自定义控件属性
         */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UITokenImageView)
        customViewToken.run {
            backgroundUIColorToken = typedArray.getString(R.styleable.UITokenImageView_backgroundColorToken) ?: ""
            backgroundUIPressedColorToken = typedArray.getString(R.styleable.UITokenImageView_backgroundPressedColorToken) ?: ""
            backgroundUIHoveredColorToken = typedArray.getString(R.styleable.UITokenImageView_backgroundHoveredColorToken) ?: ""
            backgroundUIDisabledColorToken = typedArray.getString(R.styleable.UITokenImageView_backgroundDisabledColorToken) ?: ""
            val radius = typedArray.getString(R.styleable.UITokenImageView_radiusToken) ?: ""
            radiusToken = radius
            topLeftRadiusToken = (typedArray.getString(R.styleable.UITokenImageView_topLeftRadiusToken) ?: "").ifEmpty { radius }
            topRightRadiusToken = (typedArray.getString(R.styleable.UITokenImageView_topRightRadiusToken) ?: "").ifEmpty { radius }
            bottomLeftRadiusToken = (typedArray.getString(R.styleable.UITokenImageView_bottomLeftRadiusToken) ?: "").ifEmpty { radius }
            bottomRightRadiusToken = (typedArray.getString(R.styleable.UITokenImageView_bottomRightRadiusToken) ?: "").ifEmpty { radius }
            strokeUIWidthToken = typedArray.getString(R.styleable.UITokenImageView_strokeWidthToken) ?: ""
            strokeUIColorToken = typedArray.getString(R.styleable.UITokenImageView_strokeColorToken) ?: ""
            strokeUIPressedColorToken = typedArray.getString(R.styleable.UITokenImageView_strokePressedColorToken) ?: ""
            strokeUIHoveredColorToken = typedArray.getString(R.styleable.UITokenImageView_strokeHoveredColorToken) ?: ""
            strokeUIDisabledColorToken = typedArray.getString(R.styleable.UITokenImageView_strokeDisabledColorToken) ?: ""
            outlineToken = typedArray.getString(R.styleable.UITokenImageView_outlineToken) ?: ""
            xClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenImageView_xClickExpend, 0)
            yClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenImageView_yClickExpend, 0)
            strokeDashWidth = typedArray.getDimensionPixelOffset(R.styleable.UITokenImageView_strokeDashWidth, 0).toFloat()
            strokeDashGap = typedArray.getDimensionPixelOffset(R.styleable.UITokenImageView_strokeDashGap, 0).toFloat()


            //<!-- new added 2025.10.29 -->
            advBgColor = typedArray.getColor(R.styleable.UITokenImageView_advBgColor, advBgColor)
            advRadius = typedArray.getDimension(R.styleable.UITokenImageView_advRadius, advRadius)
            advTopLeftRadius = typedArray.getDimension(R.styleable.UITokenImageView_advTopLeftRadius, advTopLeftRadius)
            advTopRightRadius = typedArray.getDimension(R.styleable.UITokenImageView_advTopRightRadius, advTopRightRadius)
            advBottomRightRadius = typedArray.getDimension(R.styleable.UITokenImageView_advBottomRightRadius, advBottomRightRadius)
            advBottomLeftRadius = typedArray.getDimension(R.styleable.UITokenImageView_advBottomLeftRadius, advBottomLeftRadius)
            advStrokeColor = typedArray.getColor(R.styleable.UITokenImageView_advStrokeColor, advStrokeColor)
            advStrokeWidth = typedArray.getDimension(R.styleable.UITokenImageView_advStrokeWidth, advStrokeWidth)
        }

        typedArray.recycle()

        changeBackground(customViewToken)
        changeOutline(customViewToken)
        if (customViewToken.xClickExpend != 0 || customViewToken.yClickExpend != 0) {
            expand(customViewToken.xClickExpend, customViewToken.yClickExpend)
        }
    }


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