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

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
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 UITokenFrameLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr), UIView {

    val customViewToken = CustomViewToken()

    init {
        /**
         * 自定义控件属性
         */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UITokenFrameLayout)
        customViewToken.run {
            backgroundUIColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_backgroundColorToken) ?: ""
            backgroundUIPressedColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_backgroundPressedColorToken) ?: ""
            backgroundUIHoveredColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_backgroundHoveredColorToken) ?: ""
            backgroundUIDisabledColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_backgroundDisabledColorToken) ?: ""
            val radius = typedArray.getString(R.styleable.UITokenFrameLayout_radiusToken) ?: ""
            radiusToken = radius
            topLeftRadiusToken = (typedArray.getString(R.styleable.UITokenFrameLayout_topLeftRadiusToken) ?: "").ifEmpty { radius }
            topRightRadiusToken = (typedArray.getString(R.styleable.UITokenFrameLayout_topRightRadiusToken) ?: "").ifEmpty { radius }
            bottomLeftRadiusToken = (typedArray.getString(R.styleable.UITokenFrameLayout_bottomLeftRadiusToken) ?: "").ifEmpty { radius }
            bottomRightRadiusToken = (typedArray.getString(R.styleable.UITokenFrameLayout_bottomRightRadiusToken) ?: "").ifEmpty { radius }
            strokeUIWidthToken = typedArray.getString(R.styleable.UITokenFrameLayout_strokeWidthToken) ?: ""
            strokeUIColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_strokeColorToken) ?: ""
            strokeUIPressedColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_strokePressedColorToken) ?: ""
            strokeUIHoveredColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_strokeHoveredColorToken) ?: ""
            strokeUIDisabledColorToken = typedArray.getString(R.styleable.UITokenFrameLayout_strokeDisabledColorToken) ?: ""
            outlineToken = typedArray.getString(R.styleable.UITokenFrameLayout_outlineToken) ?: ""
            xClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenFrameLayout_xClickExpend, 0)
            yClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenFrameLayout_yClickExpend, 0)
            strokeDashWidth = typedArray.getDimensionPixelOffset(R.styleable.UITokenFrameLayout_strokeDashWidth, 0).toFloat()
            strokeDashGap = typedArray.getDimensionPixelOffset(R.styleable.UITokenFrameLayout_strokeDashGap, 0).toFloat()

            //<!-- new added 2025.10.29 -->
            advBgColor = typedArray.getColor(R.styleable.UITokenFrameLayout_advBgColor, advBgColor)
            advRadius = typedArray.getDimension(R.styleable.UITokenFrameLayout_advRadius, advRadius)
            advTopLeftRadius = typedArray.getDimension(R.styleable.UITokenFrameLayout_advTopLeftRadius, advTopLeftRadius)
            advTopRightRadius = typedArray.getDimension(R.styleable.UITokenFrameLayout_advTopRightRadius, advTopRightRadius)
            advBottomRightRadius = typedArray.getDimension(R.styleable.UITokenFrameLayout_advBottomRightRadius, advBottomRightRadius)
            advBottomLeftRadius = typedArray.getDimension(R.styleable.UITokenFrameLayout_advBottomLeftRadius, advBottomLeftRadius)
            advStrokeColor = typedArray.getColor(R.styleable.UITokenFrameLayout_advStrokeColor, advStrokeColor)
            advStrokeWidth = typedArray.getDimension(R.styleable.UITokenFrameLayout_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
    }
}