package com.adealink.weparty.commonui.widget

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import androidx.core.view.updateLayoutParams
import com.adealink.weparty.R
import com.adealink.weparty.commonui.drawabletoolbox.DrawableBuilder
import com.adealink.weparty.commonui.drawabletoolbox.FlipDrawable
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.dpf
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.ext.sp
import com.adealink.weparty.commonui.widget.util.getColorX
import com.adealink.weparty.databinding.LayoutCommonButtonBinding

class CommonButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {

    companion object {
        const val BUTTON_CANCEL = 0
        const val BUTTON_CONFIRM = 1
        const val BUTTON_NORMAL_1 = 2
        const val BUTTON_NORMAL_2 = 3
    }

    private val binding = LayoutCommonButtonBinding.inflate(LayoutInflater.from(context), this)

    private var leftDrawableMargin: Int = 0
    var leftDrawableRes: Int? = 0
        set(value) {
            field = value
            setLeftDrawable(value)
        }

    var leftDrawableWidth: Int? = null
        set(value) {
            field = value
            setLeftDrawableSize(value)
        }

    private var rightDrawableMargin: Int = 0

    var rightDrawableRes: Int? = 0
        set(value) {
            field = value
            setRightDrawable(value)
        }

    var rightDrawableWidth: Int? = null
        set(value) {
            field = value
            setRightDrawableSize(value)
        }

    var text: CharSequence? = null
        set(value) {
            field = value
            setButtonText(value)
        }

    var textSize: Float = 0f
        set(value) {
            field = value
            setButtonTextSize(value)
        }

    var textColor: Int = Color.WHITE
        set(value) {
            field = value
            setButtonTextColor(value)
        }


    var strokeWidth: Int = 1

    private var buttonRadius = 100f
    private var buttonType = BUTTON_CONFIRM

    init {
        initAttrs(context, attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        if (isInEditMode) {
            context.withStyledAttributes(attrs, R.styleable.CommonButton) {
                buttonRadius = getDimension(
                    R.styleable.CommonButton_button_radius,
                    100f
                )
                text = getString(R.styleable.CommonButton_text)
                textSize = getDimensionPixelSize(R.styleable.CommonButton_textSize, 14).toFloat()
                textColor = getColor(R.styleable.CommonButton_textColor, Color.WHITE)
                buttonType = getInt(
                    R.styleable.CommonButton_common_button_type,
                    BUTTON_CONFIRM
                )
                leftDrawableRes = getResourceId(R.styleable.CommonButton_button_left_drawable, 0)
                leftDrawableWidth = getDimensionPixelSize(
                    R.styleable.CommonButton_button_left_drawable_width,
                    0
                )
                leftDrawableMargin =
                    getDimensionPixelSize(R.styleable.CommonButton_button_left_drawable_margin, 2)

                rightDrawableRes = getResourceId(R.styleable.CommonButton_button_right_drawable, 0)
                rightDrawableWidth = getDimensionPixelSize(
                    R.styleable.CommonButton_button_right_drawable_width,
                    0
                )
                rightDrawableMargin =
                    getDimensionPixelSize(R.styleable.CommonButton_button_right_drawable_margin, 2)
            }
            initView()
            return
        }
        context.withStyledAttributes(attrs, R.styleable.CommonButton) {
            buttonRadius = getDimension(
                R.styleable.CommonButton_button_radius,
                24.dpf()
            )
            text = getString(R.styleable.CommonButton_text)
            textSize =
                getDimensionPixelSize(R.styleable.CommonButton_textSize, 14.sp()).toFloat()
            textColor = getColor(R.styleable.CommonButton_textColor, Color.WHITE)
            buttonType = getInt(
                R.styleable.CommonButton_common_button_type,
                BUTTON_CONFIRM
            )
            leftDrawableRes = getResourceId(R.styleable.CommonButton_button_left_drawable, 0)
            leftDrawableWidth = getDimensionPixelSize(
                R.styleable.CommonButton_button_left_drawable_width,
                0
            )
            leftDrawableMargin =
                getDimensionPixelSize(
                    R.styleable.CommonButton_button_left_drawable_margin,
                    4.dp()
                )

            rightDrawableRes = getResourceId(R.styleable.CommonButton_button_right_drawable, 0)
            rightDrawableWidth = getDimensionPixelSize(
                R.styleable.CommonButton_button_right_drawable_width,
                0
            )
            rightDrawableMargin =
                getDimensionPixelSize(
                    R.styleable.CommonButton_button_right_drawable_margin,
                    4.dp()
                )
        }
        strokeWidth = 1.dp()
        initView()

    }

    private fun initView() {
        binding.tvName.updateLayoutParams<LayoutParams> {
            marginStart = leftDrawableMargin
            marginEnd = rightDrawableMargin
        }
        when (buttonType) {
            BUTTON_CANCEL -> {
                background = StateListDrawable().apply {
                    addState(
                        intArrayOf(-android.R.attr.state_enabled),
                        DrawableBuilder()
                            .cornerRadius(buttonRadius.toInt())
                            .solidColor(getColorX(R.color.white))
                            .strokeColor(getColorX(R.color.color_FF4E5969))
                            .strokeWidth(strokeWidth)
                            .build()
                    )
                    addState(
                        intArrayOf(android.R.attr.state_pressed),
                        DrawableBuilder()
                            .cornerRadius(buttonRadius.toInt())
                            .solidColor(getColorX(R.color.white))
                            .strokeColor(getColorX(R.color.color_FF4E5969))
                            .strokeWidth(strokeWidth)
                            .build()
                    )
                    addState(
                        intArrayOf(),
                        DrawableBuilder()
                            .cornerRadius(buttonRadius.toInt())
                            .solidColor(getColorX(R.color.white))
                            .strokeColor(getColorX(R.color.color_FF4E5969))
                            .strokeWidth(strokeWidth)
                            .build()
                    )
                }

                binding.tvName.setTextColor(
                    ColorStateList(
                        arrayOf(
                            intArrayOf(-android.R.attr.state_enabled), //不可点击
                            intArrayOf(android.R.attr.state_pressed), //按压
                            intArrayOf() //一般状态
                        ),
                        intArrayOf(
                            getColorX(R.color.color_FF4E5969),
                            getColorX(R.color.color_FF4E5969),
                            getColorX(R.color.color_FF4E5969),
                        )
                    )
                )
            }

            BUTTON_NORMAL_1 -> {
                background = StateListDrawable().apply {
                    addState(
                        intArrayOf(-android.R.attr.state_enabled),
                        DrawableBuilder()
                            .cornerRadius(buttonRadius.toInt())
                            .solidColor(getColorX(R.color.color_FFC9CDD4))
                            .build()
                    )
                    addState(
                        intArrayOf(android.R.attr.state_pressed),
                        DrawableBuilder()
                            .solidColor(getColorX(R.color.color_FF15E5E2))
                            .cornerRadius(buttonRadius.toInt())
                            .build()
                    )
                    addState(
                        intArrayOf(),
                        DrawableBuilder()
                            .solidColor(getColorX(R.color.color_FF15E5E2))
                            .cornerRadius(buttonRadius.toInt())
                            .build()
                    )
                }

                binding.tvName.setTextColor(
                    ColorStateList(
                        arrayOf(
                            intArrayOf(-android.R.attr.state_enabled), //不可点击
                            intArrayOf(android.R.attr.state_pressed), //按压
                            intArrayOf() //一般状态
                        ),
                        intArrayOf(
                            getColorX(R.color.white),
                            getColorX(R.color.white),
                            getColorX(R.color.white),
                        )
                    )
                )
            }

            else -> {
                // BUTTON_CONFIRM
                background = StateListDrawable().apply {
                    addState(
                        intArrayOf(-android.R.attr.state_enabled),
                        DrawableBuilder()
                            .cornerRadius(buttonRadius.toInt())
                            .solidColor(getColorX(R.color.color_FFC9CDD4))
                            .build()
                    )
                    addState(
                        intArrayOf(android.R.attr.state_pressed),
                        DrawableBuilder()
                            .gradient(true)
                            .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
                            .gradientRadius(buttonRadius)
                            .cornerRadius(buttonRadius.toInt())
                            .gradientColors(
                                getColorX(R.color.color_FF48E6C2),
                                getColorX(R.color.color_FF2A86E6),
                                null
                            )
                            .gradientType(GradientDrawable.LINEAR_GRADIENT)
                            .angle(0)
                            .build()
                    )
                    addState(
                        intArrayOf(),
                        DrawableBuilder()
                            .gradient(true)
                            .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
                            .gradientRadius(buttonRadius)
                            .cornerRadius(buttonRadius.toInt())
                            .gradientColors(
                                getColorX(R.color.color_FF50FFD8),
                                getColorX(R.color.color_FF2F95FF),
                                null
                            )
                            .gradientType(GradientDrawable.LINEAR_GRADIENT)
                            .angle(0)
                            .build()
                    )
                }

                binding.tvName.setTextColor(
                    ColorStateList(
                        arrayOf(
                            intArrayOf(-android.R.attr.state_enabled), //不可点击
                            intArrayOf(android.R.attr.state_pressed), //按压
                            intArrayOf() //一般状态
                        ),
                        intArrayOf(
                            getColorX(R.color.white),
                            getColorX(R.color.white),
                            getColorX(R.color.white),
                        )
                    )
                )
            }
        }
    }

    private fun setLeftDrawable(drawableRes: Int?) {
        if (drawableRes == null || drawableRes == 0) {
            binding.ivLeft.gone()
        } else {
            binding.ivLeft.show()
            binding.ivLeft.setImageResource(drawableRes)
        }
    }

    private fun setLeftDrawableSize(size: Int?) {
        if (size == null || size == 0) {
            binding.ivLeft.updateLayoutParams<LayoutParams> {
                this.width = LayoutParams.WRAP_CONTENT
                this.height = 0
            }
        } else {
            binding.ivLeft.updateLayoutParams<LayoutParams> {
                this.width = size
                this.height = size
            }
        }
    }

    private fun setRightDrawable(drawableRes: Int?) {
        if (drawableRes == null || drawableRes == 0) {
            binding.ivRight.gone()
        } else {
            binding.ivRight.show()
            binding.ivRight.setImageResource(drawableRes)
        }
    }


    private fun setRightDrawableSize(size: Int?) {
        if (size == null || size == 0) {
            binding.ivRight.updateLayoutParams<LayoutParams> {
                this.width = LayoutParams.WRAP_CONTENT
                this.height = 0
            }
        } else {
            binding.ivRight.updateLayoutParams<LayoutParams> {
                this.width = size
                this.height = size
            }
        }
    }

    private fun setButtonText(text: CharSequence?) {
        binding.tvName.text = text
    }

    private fun setButtonTextSize(pxSize: Float) {
        binding.tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, pxSize)
    }

    private fun setButtonTextColor(color: Int) {
        binding.tvName.setTextColor(color)
    }

}