package com.remax.visualnovel.widget.ui.chips

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView

/**
 * create by wl on 2022/9/8
 * 筛选chip
 */
class FilterChipView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : UITokenTextView(context, attrs, defStyleAttr) {

    var isActive = false
        private set

    init {
        //初始化固定token
        customViewToken.run {
            radiusToken = context.getString(R.string.radius_round)
            textUITextToken = context.getString(R.string.txt_label_m)
            textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)
        }
        changeTextFont(customViewToken)
        setInActive()
        setPaddingRelative(16.dp, 0, 16.dp, 0)
        setSingleLine()
        ellipsize = TextUtils.TruncateAt.END
        includeFontPadding = false
        height = 32.dp
        gravity = Gravity.CENTER
    }

    /**
     * 筛选状态
     */
    fun setActive() {
        isActive = true
        setActiveStatus()
    }

    /**
     * 无筛选状态
     */
    fun setInActive() {
        isActive = false
        setActiveStatus()
    }

    private fun setActiveStatus() {
        if (isActive) {
            changeTextColor {
                textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                textUIPressedColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
            }

            changeBackground {
                strokeUIWidthToken = context.getString(R.string.border_s)

                backgroundUIColorToken = context.getString(R.string.color_primary_normal)
                strokeUIColorToken = context.getString(R.string.color_outline_normal)

                backgroundUIDisabledColorToken = context.getString(R.string.color_emphasis_disabled)
                strokeUIDisabledColorToken = context.getString(R.string.color_outline_disabled)

                backgroundUIPressedColorToken = context.getString(R.string.color_primary_press)
                strokeUIPressedColorToken = context.getString(R.string.color_primary_variant_normal)
            }

        } else {
            changeTextColor {
                textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                textUIPressedColorToken = context.getString(R.string.color_txt_primary_normal)
                textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)
            }

            changeBackground {
                strokeUIWidthToken = ""

                backgroundUIColorToken = context.getString(R.string.color_surface_element_normal)

                backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)

                backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
            }
        }
    }

    fun setActive(isActive: Boolean?) {
        this.isActive = isActive == true
        setActiveStatus()
    }

    fun getActive(): Boolean {
        return isActive
    }
}