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

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import androidx.core.content.withStyledAttributes
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.expend.dsl.expand
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView

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

    companion object {
        const val DefaultIconButton_Primary = 0
        const val DefaultIconButton_Secondary = 1
        const val DefaultIconButton_Tertiary = 2
        const val GhostIconButton = 3
        const val GhostIconButton_Secondary = 4
        const val ContrastButton_Secondary = 5
        const val ContrastButton_Tertiary_Dark = 6
        const val ContrastButton_Tertiary_Light = 7
        const val NavButton = 8
        const val NavButton_OnPic = 9

        const val LARGE = 0
        const val MEDIUM = 1
        const val SMALL = 2
        const val XSMALL = 3
        const val XXSMALL = 4

        const val RECTANGLE = 0
        const val ROUND = 1
    }

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

    private var overrideAttr = false

    init {
        context.withStyledAttributes(attrs, R.styleable.IconButtonView) {
            sizeType = getInt(R.styleable.IconButtonView_iconButtonSizeType, -1)
            radiusType = getInt(R.styleable.IconButtonView_iconButtonRadius, -1)
            buttonName = getInt(R.styleable.IconButtonView_iconButtonName, -1)
            overrideAttr = getBoolean(R.styleable.IconButtonView_buttonOverrideAttr, false)
        }
        gravity = Gravity.CENTER
        typeface = context.getIconFontType()
        setButtonStyle(sizeType, buttonName)
    }

    fun setButtonStyle(sizeType: Int = this.sizeType, buttonName: Int, radiusType: Int = this.radiusType) {
        customViewToken.run {
            var isSecondary = false
            var isNav = false

            when (radiusType) {
                RECTANGLE -> {
                    radiusToken = context.getString(R.string.radius_s)
                }

                ROUND -> {
                    radiusToken = context.getString(R.string.radius_pill)
                }
            }

            when (buttonName) {
                DefaultIconButton_Primary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_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_primary_gradient_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
                }

                DefaultIconButton_Secondary -> {
                    isSecondary = true
                    textUIColorToken = context.getString(R.string.color_primary_variant_normal)
                    textUIPressedColorToken = context.getString(R.string.color_primary_variant_press)
                    textUIHoveredColorToken = context.getString(R.string.color_primary_variant_hover)
                    textUIDisabledColorToken = context.getString(R.string.color_primary_variant_disabled)
                }

                DefaultIconButton_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)
                }

                GhostIconButton -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_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)
                }

                GhostIconButton_Secondary -> {
                    textUIColorToken = context.getString(R.string.color_txt_secondary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_secondary_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)
                }

                ContrastButton_Secondary -> {
                    isSecondary = true
                    textUIColorToken = context.getString(R.string.color_surface_white_normal)
                    textUIPressedColorToken = context.getString(R.string.color_surface_white_press)
                    textUIHoveredColorToken = context.getString(R.string.color_surface_white_hover)
                    textUIDisabledColorToken = context.getString(R.string.color_surface_white_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)
                    backgroundUIHoveredColorToken = context.getString(R.string.color_surface_element_dark_press)
                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_dark_hover)
                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_dark_disabled)
                }

                ContrastButton_Tertiary_Light -> {
                    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_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)
                }

                NavButton -> {
                    isNav = true
                    radiusToken = context.getString(R.string.radius_pill)

                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_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)
                }

                NavButton_OnPic -> {
                    isNav = true
                    radiusToken = context.getString(R.string.radius_pill)

                    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)
                }
            }

            when (sizeType) {
                LARGE -> {
                    textSize = if (isSecondary) 48f else 24f
//                    height = if (isSecondary) 48.dp else 40.dp
//                    width = if (isSecondary) 48.dp else 40.dp
                    height = 48.dp
                    width = 48.dp

                    val expendSize = when {
                        isSecondary -> {
                            0
                        }

                        isNav -> {
                            2.dp
                        }

                        else -> {
                            4.dp
                        }
                    }
                    xClickExpend = expendSize
                    yClickExpend = expendSize
                }

                MEDIUM -> {
                    textSize = if (isSecondary) 36f else 20f
                    height = 36.dp
                    width = 36.dp
                    val expendSize = when {
                        isNav -> {
                            4.dp
                        }

                        else -> {
                            6.dp
                        }
                    }
                    xClickExpend = expendSize
                    yClickExpend = expendSize

                }

                SMALL -> {
                    textSize = if (isSecondary) 32f else 16f
                    height = 32.dp
                    width = 32.dp
                    val expendSize = when {
                        isNav -> {
                            6.dp
                        }

                        else -> {
                            8.dp
                        }
                    }
                    xClickExpend = expendSize
                    yClickExpend = expendSize
                }

                XSMALL -> {
                    textSize = if (isSecondary) 24f else 12f
                    height = 24.dp
                    width = 24.dp
                    val expendSize = 10.dp
                    xClickExpend = expendSize
                    yClickExpend = expendSize
                }

                XXSMALL -> {
                    textSize = if (isSecondary) 16f else 8f
                    height = 16.dp
                    width = 16.dp
                    val expendSize = 14.dp
                    xClickExpend = expendSize
                    yClickExpend = expendSize
                }
            }

            if (overrideAttr) {
                obtainStyledAttributes()
            }

            changeTextFont(this)
            changeTextColor(this)
            changeBackground(this)
            changeOutline(this)
            if (xClickExpend != 0 || yClickExpend != 0) {
                expand(xClickExpend, yClickExpend)
            }
        }
    }
}