package com.remax.visualnovel.widget.ui.tags

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.ui.tags.constant.TagSize
import com.remax.visualnovel.widget.uitoken.bean.CustomViewToken
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont

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

    companion object {
        const val DEFAULT = 0
        const val PRIMARY = 1
        const val WARNING = 2
        const val POSITIVE = 3
        const val EMPHASIS = 4
        const val IMPORTANT = 5
    }

    private var sizeType = TagSize.SMALL

    init {
        includeFontPadding = false
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorSupportTagView)
        sizeType = typedArray.getInt(R.styleable.ColorSupportTagView_tagViewSize, TagSize.SMALL)
        val colorType = typedArray.getInt(R.styleable.ColorSupportTagView_tagBackgroundColorType, -1)
        val background = typedArray.getString(R.styleable.ColorSupportTagView_tagBackgroundColorToken) ?: ""
        typedArray.recycle()
        val padding = 8.dp
        setPadding(padding, 0, padding, 0)
        setTagType(sizeType, background, colorType)
        maxLines = 1
        ellipsize = TextUtils.TruncateAt.END
        gravity = Gravity.CENTER
    }

    fun setTagType(type: Int = sizeType, colorToken: String = "", colorType: Int = -1) {
        var textUITextToken = ""
        when (type) {
            TagSize.SMALL -> {
                textUITextToken = context.getString(R.string.txt_label_s)
                height = 24.dp
            }

            TagSize.LARGE -> {
                textUITextToken = context.getString(R.string.txt_label_m)
                height = 32.dp
            }
        }

        val token = CustomViewToken(
            textUITextToken = textUITextToken,
            textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal),
            radiusToken = context.getString(R.string.radius_xs)
        )
        val backgroundUIColorToken = when (colorType) {
            DEFAULT -> {
                token.textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                context.getString(R.string.color_surface_element_normal)
            }

            PRIMARY -> {
                context.getString(R.string.color_primary_normal)
            }

            WARNING -> {
                context.getString(R.string.color_warning_normal)
            }

            POSITIVE -> {
                context.getString(R.string.color_positive_normal)
            }

            EMPHASIS -> {
                context.getString(R.string.color_emphasis_normal)
            }

            IMPORTANT -> {
                context.getString(R.string.color_important_normal)
            }

            else -> {
                colorToken
            }
        }
        token.backgroundUIColorToken = backgroundUIColorToken

        changeBackground(token)
        changeTextColor(token)
        changeTextFont(token)
    }

}