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

    companion object {
        const val OFFICIAL = 1
        const val HOST = 2
        const val VIP_SERVICE = 3
    }

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

    fun setTagType(type: Int) {
        var colorToken = ""
        var textUIColorToken = ""
        when (type) {
            OFFICIAL -> {
                colorToken = context.getString(R.string.color_primary_normal)
                textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
            }

            HOST -> {
                colorToken = context.getString(R.string.color_primary_normal)
                textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
            }

            VIP_SERVICE -> {
                colorToken = context.getString(R.string.color_context_legends_normal)
                textUIColorToken = context.getString(R.string.color_background_default)
            }
        }
        val token = CustomViewToken(
            textUITextToken = context.getString(R.string.txt_label_s),
            textUIColorToken = textUIColorToken,
            radiusToken = context.getString(R.string.radius_xs),
            backgroundUIColorToken = colorToken,
        )

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

}