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

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

    val customViewToken = CustomViewToken()

    init {
        /**
         * 自定义控件属性
         */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UITokenLinearLayout)
        customViewToken.run {
            backgroundUIColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_backgroundColorToken) ?: ""
            backgroundUIPressedColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_backgroundPressedColorToken) ?: ""
            backgroundUIHoveredColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_backgroundHoveredColorToken) ?: ""
            backgroundUIDisabledColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_backgroundDisabledColorToken) ?: ""
            val radius = typedArray.getString(R.styleable.UITokenLinearLayout_radiusToken) ?: ""
            radiusToken = radius
            topLeftRadiusToken = (typedArray.getString(R.styleable.UITokenLinearLayout_topLeftRadiusToken) ?: "").ifEmpty { radius }
            topRightRadiusToken = (typedArray.getString(R.styleable.UITokenLinearLayout_topRightRadiusToken) ?: "").ifEmpty { radius }
            bottomLeftRadiusToken = (typedArray.getString(R.styleable.UITokenLinearLayout_bottomLeftRadiusToken) ?: "").ifEmpty { radius }
            bottomRightRadiusToken = (typedArray.getString(R.styleable.UITokenLinearLayout_bottomRightRadiusToken) ?: "").ifEmpty { radius }
            strokeUIWidthToken = typedArray.getString(R.styleable.UITokenLinearLayout_strokeWidthToken) ?: ""
            strokeUIColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_strokeColorToken) ?: ""
            strokeUIPressedColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_strokePressedColorToken) ?: ""
            strokeUIHoveredColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_strokeHoveredColorToken) ?: ""
            strokeUIDisabledColorToken = typedArray.getString(R.styleable.UITokenLinearLayout_strokeDisabledColorToken) ?: ""
            outlineToken = typedArray.getString(R.styleable.UITokenLinearLayout_outlineToken) ?: ""
            xClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenLinearLayout_xClickExpend, 0)
            yClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenLinearLayout_yClickExpend, 0)
            strokeDashWidth = typedArray.getDimensionPixelOffset(R.styleable.UITokenLinearLayout_strokeDashWidth, 0).toFloat()
            strokeDashGap = typedArray.getDimensionPixelOffset(R.styleable.UITokenLinearLayout_strokeDashGap, 0).toFloat()

            //<!-- new added 2025.10.29 -->
            advBgColor = typedArray.getColor(R.styleable.UITokenLinearLayout_advBgColor, advBgColor)
            advRadius = typedArray.getDimension(R.styleable.UITokenLinearLayout_advRadius, advRadius)
            advTopLeftRadius = typedArray.getDimension(R.styleable.UITokenLinearLayout_advTopLeftRadius, advTopLeftRadius)
            advTopRightRadius = typedArray.getDimension(R.styleable.UITokenLinearLayout_advTopRightRadius, advTopRightRadius)
            advBottomRightRadius = typedArray.getDimension(R.styleable.UITokenLinearLayout_advBottomRightRadius, advBottomRightRadius)
            advBottomLeftRadius = typedArray.getDimension(R.styleable.UITokenLinearLayout_advBottomLeftRadius, advBottomLeftRadius)
            advStrokeColor = typedArray.getColor(R.styleable.UITokenLinearLayout_advStrokeColor, advStrokeColor)
            advStrokeWidth = typedArray.getDimension(R.styleable.UITokenLinearLayout_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
    }
}