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

import android.content.Context
import android.graphics.Paint
import android.util.AttributeSet
import androidx.core.content.withStyledAttributes
import com.remax.visualnovel.R
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 TextButtonView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenTextView(context, attrs, defStyleAttr) {

    companion object {
        const val TextButton_Primary = 0
        const val TextButton_Secondary = 1
        const val TextButton_Tertiary = 2

        const val LARGE = 0
        const val MEDIUM = 1
        const val SMALL = 2
    }

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

    init {
        context.withStyledAttributes(attrs, R.styleable.TextButtonView) {
            sizeType = getInt(R.styleable.TextButtonView_textButtonSizeType, -1)
            buttonName = getInt(R.styleable.TextButtonView_textButtonName, -1)
            val isUnderLine = getBoolean(R.styleable.TextButtonView_textButtonUnderLine,false)
            if (isUnderLine){
                paint.flags = Paint.UNDERLINE_TEXT_FLAG
                paint.isAntiAlias = true
            }
        }
        setButtonStyle(sizeType, buttonName)
    }

    fun setButtonStyle(sizeType: Int, buttonName: Int) {
        customViewToken.run {
            when (sizeType) {
                LARGE -> {
                    textUITextToken = context.getString(R.string.txt_label_l)
                }

                MEDIUM -> {
                    textUITextToken = context.getString(R.string.txt_label_m)
                }

                SMALL -> {
                    textUITextToken = context.getString(R.string.txt_label_s)
                }
            }

            when (buttonName) {
                TextButton_Primary -> {
                    textUIColorToken = context.getString(R.string.color_primary_variant_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_primary_disabled)
                    textUIPressedColorToken = context.getString(R.string.color_primary_variant_press)
                    textUIHintColorToken = context.getString(R.string.color_primary_variant_hover)
                }

                TextButton_Secondary -> {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIPressedColorToken = context.getString(R.string.color_txt_primary_press)
                    textUIHintColorToken = context.getString(R.string.color_txt_primary_hover)
                }

                TextButton_Tertiary -> {
                    textUIColorToken = context.getString(R.string.color_txt_secondary_normal)
                    textUIPressedColorToken = context.getString(R.string.color_txt_secondary_press)
                    textUIHintColorToken = context.getString(R.string.color_txt_secondary_hover)
                }
            }
            changeTextFont(this)
            changeTextColor(this)
            changeOutline(this)
            if (xClickExpend != 0 || yClickExpend != 0) {
                expand(xClickExpend, yClickExpend)
            }
        }
    }
}