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.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 ContextTagView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    AppCompatTextView(context, attrs, defStyleAttr) {

    companion object {
        const val PRICE_PROMOTION = 1
        const val SUBSCRIBE = 2
        const val LEGENDS = 3
        const val LEGENDS_B = 4
    }

    init {
        includeFontPadding = false
        gravity = Gravity.CENTER
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ContextTagView)
        val type = typedArray.getInt(R.styleable.ContextTagView_contextTagType, PRICE_PROMOTION)
        typedArray.recycle()
        val padding = 8.dp
        setPadding(padding, 0, padding, 0)
        height = 24.dp
        setTagType(type)
        gravity = Gravity.CENTER_VERTICAL
        maxLines = 1
        ellipsize = TextUtils.TruncateAt.END
    }

    fun setTagType(type: Int) {
        val token = CustomViewToken(
            textUITextToken = context.getString(R.string.txt_label_s),
            textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal),
            radiusToken = context.getString(R.string.radius_xs)
        )
        val colorToken = when (type) {
            PRICE_PROMOTION -> {
                context.getString(R.string.color_primary_gradient_normal)
            }

            SUBSCRIBE -> {
                context.getString(R.string.color_context_subscribe_normal)
            }

            LEGENDS -> {
                token.apply {
                    strokeUIWidthToken = context.getString(R.string.border_s)
                    strokeUIColorToken = context.getString(R.string.color_context_legends_normal)
                    textUIColorToken = context.getString(R.string.color_context_legends_normal)
                }
                context.getString(R.string.color_surface_element_dark_normal)
            }

            LEGENDS_B -> {
                token.apply {
                    strokeUIWidthToken = context.getString(R.string.border_s)
                    strokeUIColorToken = context.getString(R.string.color_context_legends_variant_normal)
                    textUIColorToken = context.getString(R.string.color_context_legends_variant_normal)
                }
                context.getString(R.string.color_surface_element_dark_normal)
            }

            else -> {
                context.getString(R.string.color_transparent)
            }
        }
        token.backgroundUIColorToken = colorToken

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

}