package com.remax.visualnovel.widget.ui.buttons

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import androidx.core.widget.TextViewCompat
import com.remax.visualnovel.R
import com.remax.visualnovel.extension.getIconFontType
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeOutline
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView
import androidx.core.content.withStyledAttributes

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

    companion object {
        const val DefaultButton_Primary = 0
        const val DefaultButton_Secondary = 1
        const val DefaultButton_Tertiary = 2
        const val DefaultButton_Destructive = 3
        const val ContrastButton_Primary = 4
        const val ContrastButton_Secondary = 5
        const val ContrastButton_Tertiary_Light = 6
        const val ContrastButton_Tertiary_Dark = 7
        const val GhostButton_Primary = 8
        const val GhostButton_Secondary = 9
        const val ContextButton_Subscribe = 10
        const val IconButton_Primary = 11
        const val IconButton_Tertiary = 12
        const val DefaultButton_VIP = 13

        const val LARGE = 0
        const val SMALL = 1
    }

    private var sizeType = -1
    private var buttonName = -1

    private var overrideAttr = false

    init {
        context.withStyledAttributes(attrs, R.styleable.ButtonView) {
            sizeType = getInt(R.styleable.ButtonView_buttonSizeType, -1)
            buttonName = getInt(R.styleable.ButtonView_buttonName, -1)
            overrideAttr = getBoolean(R.styleable.ButtonView_buttonOverrideAttr, false)
        }
        customViewToken.run {
            radiusToken = context.getString(R.string.radius_pill)
        }
        setButtonStyle(sizeType, buttonName)
        gravity = Gravity.CENTER
        maxLines = 1
        ellipsize = TextUtils.TruncateAt.END

        runCatching {
            val minSize = 10.dp
            val maxSize = textSize.toInt()
            if (minSize < maxSize) {
                TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
                TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this, minSize, maxSize, 2, TypedValue.COMPLEX_UNIT_PX)
            }
        }

    }

    fun setButtonStyle(buttonName: Int) {
        setButtonStyle(sizeType, buttonName)
    }

    fun getMeasureText() = paint.measureText(text.toString()) + paddingStart + paddingEnd

    fun setButtonStyle(sizeType: Int, buttonName: Int) {
        this.buttonName = buttonName
        customViewToken.run {
            val isIconFont = buttonName == IconButton_Primary || buttonName == IconButton_Tertiary
            /**
             * 设置size大小
             */
            when (sizeType) {
                LARGE -> {
                    height = 48.dp
                    val padding = 24.dp
                    setPadding(padding, 0, padding, 0)
                    if (isIconFont) {
                        typeface = context.getIconFontType()
                        textSize = 24f
                    } else {
                        textUITextToken = context.getString(R.string.txt_label_l)
                    }
                }

                SMALL -> {
                    height = 32.dp
                    val padding = 16.dp
                    setPadding(padding, 0, padding, 0)
                    if (minWidth == 0) {
                        minWidth = 60.dp
                    }
                    if (isIconFont) {
                        typeface = context.getIconFontType()
                        textSize = 20f
                    } else {
                        textUITextToken = context.getString(R.string.txt_label_s)
                    }
                }
            }
            /**
             * 设置state颜色/描边
             */
            when (buttonName) {
                DefaultButton_VIP -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_context_vip_normal)
                    backgroundUIPressedColorToken = ""
                    backgroundUIHoveredColorToken = ""
                    backgroundUIDisabledColorToken = ""
                }

                DefaultButton_Primary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_primary_gradient_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_primary_gradient_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_txt_primary_normal)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_primary_disabled)
                }

                DefaultButton_Secondary -> {
                    textUIColorToken = context.getString(R.string.color_primary_variant_normal)
                    textUIPressedColorToken = context.getString(R.string.color_primary_variant_press)
                    textUIHintColorToken = context.getString(R.string.color_primary_variant_hover)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_transparent)
                    backgroundUIPressedColorToken = context.getString(R.string.color_transparent)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_transparent)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_transparent)

                    strokeUIWidthToken = context.getString(R.string.border_m)
                    strokeUIColorToken = context.getString(R.string.color_primary_variant_normal)
                    strokeUIPressedColorToken = context.getString(R.string.color_primary_variant_press)
                    strokeUIHoveredColorToken = context.getString(R.string.color_primary_variant_hover)
                    strokeUIDisabledColorToken = context.getString(R.string.color_primary_variant_disabled)
                }

                DefaultButton_Tertiary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_surface_element_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
                }

                DefaultButton_Destructive -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_important_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_important_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_important_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_important_disabled)
                }

                ContrastButton_Primary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_specialmap_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_surface_base_specialmap_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_base_specialmap_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_base_specialmap_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_base_specialmap_disabled)
                }

                ContrastButton_Secondary -> {
                    textUIColorToken = context.getString(R.string.color_surface_white_normal)
                    textUIPressedColorToken = context.getString(R.string.color_surface_white_press)
                    textUIHintColorToken = context.getString(R.string.color_surface_white_hover)
                    textUIDisabledColorToken = context.getString(R.string.color_surface_white_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_transparent)
                    backgroundUIPressedColorToken = context.getString(R.string.color_transparent)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_transparent)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_transparent)

                    strokeUIWidthToken = context.getString(R.string.border_m)
                    strokeUIColorToken = context.getString(R.string.color_surface_white_normal)
                    strokeUIPressedColorToken = context.getString(R.string.color_surface_white_press)
                    strokeUIHoveredColorToken = context.getString(R.string.color_surface_white_hover)
                    strokeUIDisabledColorToken = context.getString(R.string.color_surface_white_disabled)
                }

                ContrastButton_Tertiary_Light -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_specialmap_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_surface_element_light_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_light_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_light_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_light_disabled)
                }

                ContrastButton_Tertiary_Dark -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_specialmap_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_surface_element_dark_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_dark_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_dark_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_dark_disabled)
                }

                GhostButton_Primary -> {
                    textUIColorToken = context.getString(R.string.color_primary_variant_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_transparent)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_transparent)
                }

                GhostButton_Secondary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_transparent)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_transparent)
                }

                ContextButton_Subscribe -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_context_subscribe_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_context_subscribe_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_context_subscribe_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_context_subscribe_disabled)
                }

                IconButton_Primary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_primary_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_primary_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_primary_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_primary_disabled)
                }

                IconButton_Tertiary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_disabled)

                    backgroundUIColorToken = context.getString(R.string.color_surface_element_normal)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
                }
            }
            if (overrideAttr) {
                obtainStyledAttributes()
            }
            if (!isIconFont) {
                changeTextFont(this)
            }
            changeTextColor(this)
            changeBackground(this)
            changeOutline(this)
        }

    }
}