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

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

    val customViewToken = CustomViewToken()

    init {
        /**
         * 自定义控件属性
         */
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UITokenConstraintLayout)
        customViewToken.run {
            backgroundUIColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_backgroundColorToken) ?: ""
            backgroundUIPressedColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_backgroundPressedColorToken) ?: ""
            backgroundUIHoveredColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_backgroundHoveredColorToken) ?: ""
            backgroundUIDisabledColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_backgroundDisabledColorToken) ?: ""
            val radius = typedArray.getString(R.styleable.UITokenConstraintLayout_radiusToken) ?: ""
            radiusToken = radius
            topLeftRadiusToken = (typedArray.getString(R.styleable.UITokenConstraintLayout_topLeftRadiusToken) ?: "").ifEmpty { radius }
            topRightRadiusToken = (typedArray.getString(R.styleable.UITokenConstraintLayout_topRightRadiusToken) ?: "").ifEmpty { radius }
            bottomLeftRadiusToken = (typedArray.getString(R.styleable.UITokenConstraintLayout_bottomLeftRadiusToken) ?: "").ifEmpty { radius }
            bottomRightRadiusToken = (typedArray.getString(R.styleable.UITokenConstraintLayout_bottomRightRadiusToken) ?: "").ifEmpty { radius }
            strokeUIWidthToken = typedArray.getString(R.styleable.UITokenConstraintLayout_strokeWidthToken) ?: ""
            strokeUIColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_strokeColorToken) ?: ""
            strokeUIPressedColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_strokePressedColorToken) ?: ""
            strokeUIHoveredColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_strokeHoveredColorToken) ?: ""
            strokeUIDisabledColorToken = typedArray.getString(R.styleable.UITokenConstraintLayout_strokeDisabledColorToken) ?: ""
            outlineToken = typedArray.getString(R.styleable.UITokenConstraintLayout_outlineToken) ?: ""
            xClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenConstraintLayout_xClickExpend, 0)
            yClickExpend = typedArray.getDimensionPixelOffset(R.styleable.UITokenConstraintLayout_yClickExpend, 0)
            strokeDashWidth = typedArray.getDimensionPixelOffset(R.styleable.UITokenConstraintLayout_strokeDashWidth, 0).toFloat()
            strokeDashGap = typedArray.getDimensionPixelOffset(R.styleable.UITokenConstraintLayout_strokeDashGap, 0).toFloat()
        }

        typedArray.recycle()

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

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