package com.gh.gamecenter.common.view

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.view.forEach
import androidx.core.view.updateLayoutParams
import androidx.core.widget.TextViewCompat
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.databinding.LayoutChipsBinding
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor

/**
 * 标签多选项
 * 通用组件：标签按钮
 */
class Chips @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val binding: LayoutChipsBinding

    private var chipsStyle = STYLE_REGULAR
    private var chipsState = STATE_DEFAULT

    val startIcon
        get() = binding.ivStart

    val endIcon
        get() = binding.ivEnd


    init {
        val inflater = LayoutInflater.from(context)
        binding = LayoutChipsBinding.inflate(inflater, this, true)

        initAttrs(context, attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Chips)

        val style = typedArray.getInt(R.styleable.Chips_chips_style, STYLE_REGULAR)
        setStyle(style)

        val state = typedArray.getInt(R.styleable.Chips_chips_state, STATE_DEFAULT)
        setState(state)

        val startIcon = typedArray.getResourceId(R.styleable.Chips_start_icon, -1)
        setIcon(binding.ivStart, startIcon)

        val text = typedArray.getText(R.styleable.Chips_chips_text)
        binding.tvContent.text = text

        val endIcon = typedArray.getResourceId(R.styleable.Chips_end_icon, -1)
        setIcon(binding.ivEnd, endIcon)

        typedArray.recycle()
    }

    private fun setIcon(ivIcon: ImageView, resourceId: Int) {
        if (resourceId != -1) {
            ivIcon.visibility = VISIBLE
            ivIcon.setImageResource(resourceId)
        }
    }

    fun setText(text: String) {
        binding.tvContent.text = text
    }

    private fun setStyle(newStyle: Int) {
        chipsStyle = newStyle
        when (newStyle) {
            STYLE_REGULAR -> {
                binding.root.updateLayoutParams {
                    height = 32F.dip2px()
                }
                binding.root.setPadding(12F.dip2px(), 0, 12F.dip2px(), 0)
                TextViewCompat.setTextAppearance(binding.tvContent, R.style.TextCaption1)
                8F.dip2px()
            }

            STYLE_SMALL -> {
                binding.root.updateLayoutParams {
                    height = 28F.dip2px()
                }
                binding.root.setPadding(10F.dip2px(), 0, 10F.dip2px(), 0)
                TextViewCompat.setTextAppearance(binding.tvContent, R.style.TextCaption2)
                6F.dip2px()
            }

            else -> {
                binding.root.updateLayoutParams {
                    height = 24F.dip2px()
                }
                binding.root.setPadding(8F.dip2px(), 0, 8F.dip2px(), 0)
                TextViewCompat.setTextAppearance(binding.tvContent, R.style.TextCaption2)
                6F.dip2px()
            }
        }
    }

    fun setState(state: Int) {
        chipsState = state
        val (backgroundResId, colorInt) = when (state) {
            STATE_DEFAULT -> {
                R.drawable.bg_chips_default to R.color.text_secondary.toColor(context)
            }

            STATE_FILL -> {
                R.drawable.bg_chips_fill to R.color.text_secondary.toColor(context)
            }

            STATE_SELECTED -> {
                R.drawable.bg_chips_selected to R.color.text_theme.toColor(context)
            }

            else -> {
                R.drawable.bg_chips_disable to R.color.text_secondary.toColor(context)
            }
        }

        //设置背景
        val radius = if (chipsStyle == STYLE_REGULAR) 8F.dip2px() else 6F.dip2px()
        val drawable = AppCompatResources.getDrawable(context, backgroundResId)
        if (drawable is GradientDrawable) {
            drawable.cornerRadius = radius.toFloat()
            binding.root.background = drawable
        }

        // 设置透明度
        val alpha = if (state == STATE_DISABLE) 0.4F else 1.0F
        binding.root.forEach { it.alpha = alpha }

        // 设置内容颜色
        binding.ivStart.imageTintList = ColorStateList.valueOf(colorInt)
        binding.tvContent.setTextColor(colorInt)
        binding.ivEnd.imageTintList = ColorStateList.valueOf(colorInt)
    }

    companion object {
        // style
        private const val STYLE_REGULAR = 0
        private const val STYLE_SMALL = 1
        private const val STYLE_MINI = 2

        // state
        const val STATE_DEFAULT = 0
        const val STATE_FILL = 1
        const val STATE_SELECTED = 2
        const val STATE_DISABLE = 3
    }
}