package com.remax.visualnovel.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.content.withStyledAttributes
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.WidgetTagIconBinding
import com.remax.visualnovel.entity.response.HeartbeatLevelEnum
import com.remax.visualnovel.extension.getTemperatureTxt
import com.remax.visualnovel.extension.setMargin
import com.remax.visualnovel.extension.setSize
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextStyle
import com.remax.visualnovel.widget.uitoken.getGradientDrawableOrientation
import com.remax.visualnovel.widget.uitoken.handleUIToken
import com.dylanc.viewbinding.nonreflection.inflate

/**
 * Created by HJW on 2023/8/2
 */
@SuppressLint("SetTextI18n")
class TagIconView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    LinearLayout(context, attrs, defStyleAttr) {

    private var binding: WidgetTagIconBinding? = null

    private var type = GRADIENT_PRIMARY

    private var margin = 8

    init {
        binding = inflate(WidgetTagIconBinding::inflate)

        context.withStyledAttributes(attrs, R.styleable.TagIconView) {
            val content = getString(R.styleable.TagIconView_tagIconContent)
            val iconFont = getString(R.styleable.TagIconView_tagIconFont)
            type = getInt(R.styleable.TagIconView_tagIconType, GRADIENT_PRIMARY)
            val size = getInt(R.styleable.TagIconView_tagIconSize, SIZE_L)

            setTagContent(content, iconFont)
            setTagIconType(type)

            var iconSize = 16f

            var txtToken = R.string.txt_label_m

//            var groupPaddingV = 6.dp
//            var groupPaddingH = 8.dp

            var groupHeight = 32.dp

            when (size) {
                SIZE_L -> {

                }

                SIZE_M -> {
                    iconSize = 12f
                    margin = 4
                    txtToken = R.string.txt_label_s

//                    groupPaddingV = 2.dp
//                    groupPaddingH = 4.dp

                    groupHeight = 24.dp
                }

                SIZE_S -> {
                    iconSize = 12f
                    margin = 4
                    txtToken = R.string.txt_label_s
//
//                    groupPaddingV = 2.dp
//                    groupPaddingH = 2.dp

                    groupHeight = 20.dp
                }
            }

            binding?.run {
                group.setSize(height = groupHeight)
//                group.setPadding(groupPaddingH, groupPaddingV, groupPaddingH, groupPaddingV)

                with(textView) {
                    changeTextStyle {
                        textUITextToken = context.getString(txtToken)
                    }
                    setMargin(marginStart = margin.dp, marginEnd = margin.dp)
                }

                with(iconView) {
                    setTextSize(TypedValue.COMPLEX_UNIT_SP, iconSize)
                    setMargin(marginStart = margin.dp)
                }

            }
        }
    }

    fun getTagTextView() = binding!!.textView

    fun setTagContent(content: String?, iconFont: String? = null) {
        binding?.run {
            textView.text = content

            iconView.text = iconFont
            iconView.isVisible = iconFont != null
        }
    }

    fun setHeartbeatTag(heartbeatLevel: String? = null, heartbeatVal: Double? = null, showLevel: Boolean): String {
        var returnTxt = ""
        binding?.run {
            iconView.isVisible = false
            // 关系
            val currLevel =
                HeartbeatLevelEnum.entries.find { it.levelName == heartbeatLevel }
            val tagName = if (showLevel && currLevel != null) context.getString(currLevel.tagName) else null

            // 温度值，传入null就只显示关系
            val temperatureTxt = heartbeatVal?.getTemperatureTxt()

            returnTxt = listOfNotNull(tagName, temperatureTxt).joinToString(" · ")
            if (returnTxt.isNotEmpty()){
                returnTxt = " $returnTxt "
            }
            val tvWidth = textView.paint.measureText(returnTxt) + 1.dp
            textView.setSize(width = tvWidth.toInt())
            textView.text = returnTxt

            val currType = when (currLevel) {
                HeartbeatLevelEnum.LEVEL_3, HeartbeatLevelEnum.LEVEL_4 -> VIOLET
                HeartbeatLevelEnum.LEVEL_5, HeartbeatLevelEnum.LEVEL_6 -> ORANGE
                HeartbeatLevelEnum.LEVEL_7, HeartbeatLevelEnum.LEVEL_8 -> MAGENTA
                HeartbeatLevelEnum.LEVEL_9, HeartbeatLevelEnum.LEVEL_10 -> GRADIENT_PRIMARY
                else -> ELEMENT_DARK
            }
            if (currType != type) {
                setTagIconType(currType)
            }
        }
        return returnTxt
    }


    fun setTagIconType(type: Int) {
        this.type = type
        binding?.run {
            var textColorToken = context.getString(R.string.color_txt_primary_normal)
            var blurColor = ContextCompat.getColor(context, R.color.white_p15)
            var groupColorToken = R.color.white_p15

            group.setBackgroundResource(R.color.transparent)

            when (type) {
                ELEMENT -> {
                    blurColor = ContextCompat.getColor(context, R.color.glo_color_purple_0_p8)
                }

                ELEMENT_LIGHT -> {
                    blurColor = ContextCompat.getColor(context, R.color.white_p15)
                }

                ELEMENT_DARK -> {
                    blurColor = ContextCompat.getColor(context, R.color.black_p65)
                }

                MINT -> {
                    group.setBackgroundResource(R.color.glo_color_mint_40_p60)
                }

                BLUE -> {
                    group.setBackgroundResource(R.color.glo_color_blue_40_p60)
                }

                VIOLET -> {
                    group.setBackgroundResource(R.color.glo_color_violet_40_p60)
                }

                MAGENTA -> {
                    group.setBackgroundResource(R.color.glo_color_magenta_50_p60)
                }

                ORANGE -> {
                    group.setBackgroundResource(R.color.glo_color_orange_50_p60)
                }

                GRADIENT_PRIMARY -> {
                    context.handleUIToken(R.string.color_primary_gradient_normal)?.let {
                        val gradientDrawable = GradientDrawable()
                        gradientDrawable.shape = GradientDrawable.RECTANGLE
                        gradientDrawable.orientation = getGradientDrawableOrientation(it.deg)
                        gradientDrawable.colors = it.colors
                        group.background = gradientDrawable
                    }
                }

                GRADIENT_SECOND -> {
                    context.handleUIToken(R.string.color_context_vip_normal)?.let {
                        val gradientDrawable = GradientDrawable()
                        gradientDrawable.shape = GradientDrawable.RECTANGLE
                        gradientDrawable.orientation = getGradientDrawableOrientation(it.deg)
                        gradientDrawable.colors = it.colors
                        group.background = gradientDrawable
                    }
                    textColorToken = context.getString(R.string.color_background_default)
                }
            }

            blurView.setOverlayColor(blurColor)

            textView.changeTextColor {
                textUIColorToken = textColorToken
            }
            iconView.changeTextColor {
                textUIColorToken = textColorToken
            }
        }
    }

    companion object {
        const val ELEMENT = 1
        const val ELEMENT_LIGHT = 2
        const val ELEMENT_DARK = 3

        const val MINT = 4
        const val BLUE = 5
        const val VIOLET = 6
        const val MAGENTA = 7
        const val ORANGE = 8

        const val GRADIENT_PRIMARY = 9
        const val GRADIENT_SECOND = 10

        const val SIZE_L = 1
        const val SIZE_M = 2
        const val SIZE_S = 3
    }
}