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

    companion object {
        const val DEFAULT = 1
        const val WHITE_ON_COLOR = 2
        const val BLACK_ON_COLOR = 3
    }

    private var sizeType = TagSize.SMALL
    private var tagType = DEFAULT

    init {
        includeFontPadding = false
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.PrimaryTagView)
        val size = typedArray.getInt(R.styleable.PrimaryTagView_tagViewSize, TagSize.SMALL)
        val type = typedArray.getInt(R.styleable.PrimaryTagView_primaryTagType, DEFAULT)
        typedArray.recycle()
        val padding = 8.dp
        setPadding(padding, 0, padding, 0)
        ellipsize = TextUtils.TruncateAt.END
        maxLines = 1
        setTagType(type, size)
        gravity = Gravity.CENTER
    }

    fun setTagType(type: Int = tagType, size: Int = sizeType) {
        tagType = type
        sizeType = size
        var colorToken = ""

        when (sizeType) {
            TagSize.SMALL -> {
                height = 24.dp
            }

            TagSize.LARGE -> {
                height = 32.dp
            }
        }

        when (type) {
            DEFAULT -> {
                colorToken = context.getString(R.string.color_surface_element_normal)
            }

            WHITE_ON_COLOR -> {
                colorToken = context.getString(R.string.color_surface_element_light_normal)
            }

            BLACK_ON_COLOR -> {
                colorToken = context.getString(R.string.color_surface_element_dark_normal)
            }
        }
        val token = CustomViewToken(
            textUITextToken = context.getString(if (sizeType == TagSize.SMALL) R.string.txt_label_s else R.string.txt_label_m),
            textUIColorToken = context.getString(R.string.color_txt_primary_normal),
            radiusToken = context.getString(R.string.radius_xs),
            backgroundUIColorToken = colorToken,
        )

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

}