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

import android.content.Context
import android.util.AttributeSet
import androidx.core.content.withStyledAttributes
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.ui.IconFontTextView
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont

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

    companion object {
        const val START = 1
        const val END = 2
    }

    private var iconDirection = END
    private var iconFont: String = ""

    init {
        context.withStyledAttributes(attrs, R.styleable.RemovableChipView) {
            iconFont = getString(R.styleable.RemovableChipView_iconFont) ?: context.getString(R.string.icon_delete_2)
            iconDirection = getInt(R.styleable.RemovableChipView_chipIconDirection, END)
        }

        //初始化固定token
        customViewToken.run {
            textUITextToken = context.getString(R.string.txt_label_m)
            textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)
            textUIPressedColorToken = context.getString(R.string.color_txt_primary_normal)
            backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
            radiusToken = context.getString(R.string.radius_round)
        }
        changeTextFont(customViewToken)
        setInActive()
        setPaddingRelative(16.dp, 6.dp, 12.dp, 6.dp)
    }

    /**
     * 筛选状态
     */
    fun setActive() {
        customViewToken.run {
            strokeUIWidthToken = context.getString(R.string.border_s)
            //normal
            backgroundUIColorToken = context.getString(R.string.color_primary_normal)
            strokeUIColorToken = context.getString(R.string.color_primary_variant_normal)
            //disable
            backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
            strokeUIDisabledColorToken = context.getString(R.string.color_outline_disabled)
            //press
            backgroundUIPressedColorToken = context.getString(R.string.color_primary_press)
            strokeUIPressedColorToken = context.getString(R.string.color_primary_variant_press)
        }
        changeTextColor(customViewToken)
        changeBackground(customViewToken)
        when (iconDirection) {
            START -> {
                setIconFontDrawable(
                    startIconFont = iconFont,
                    iconPadding = 8,
                    iconSize = 16,
                    iconColorToken = if (isEnabled) context.getString(R.string.color_txt_primary_specialmap_normal) else context.getString(R.string.color_txt_secondary_disabled)
                )
            }

            END -> {
                setIconFontDrawable(
                    endIconFont = iconFont,
                    iconPadding = 8,
                    iconSize = 16,
                    iconColorToken = if (isEnabled) context.getString(R.string.color_txt_primary_specialmap_normal) else context.getString(R.string.color_txt_secondary_disabled)
                )
            }
        }

    }

    /**
     * 无筛选状态
     */
    fun setInActive() {
        customViewToken.run {
            //无筛选状态将描边恢复默认
            strokeUIWidthToken = ""
            strokeUIColorToken = ""
            strokeUIDisabledColorToken = ""
            strokeUIPressedColorToken = ""
            //normal
            textUIColorToken = context.getString(R.string.color_txt_primary_normal)
            backgroundUIColorToken = context.getString(R.string.color_surface_element_normal)
            //disable
            backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
            //press
            backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
        }
        changeTextColor(customViewToken)
        changeBackground(customViewToken)
        when (iconDirection) {
            START -> {
                setIconFontDrawable(
                    startIconFont = iconFont,
                    iconPadding = 8,
                    iconSize = 16,
                    iconColorToken = if (isEnabled) context.getString(R.string.color_txt_secondary_normal) else context.getString(R.string.color_txt_secondary_disabled)
                )
            }

            END -> {
                setIconFontDrawable(
                    endIconFont = iconFont,
                    iconPadding = 8,
                    iconSize = 16,
                    iconColorToken = if (isEnabled) context.getString(R.string.color_txt_secondary_normal) else context.getString(R.string.color_txt_secondary_disabled)
                )
            }
        }

    }
}