package com.gh.gamecenter.common.view

import android.content.Context
import android.content.res.ColorStateList
import android.text.Editable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View.MeasureSpec.EXACTLY
import android.widget.LinearLayout
import androidx.core.widget.TextViewCompat
import androidx.core.widget.addTextChangedListener
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.databinding.LayoutGhSearchBinding
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.visibleIf

/**
 * 通用组件：搜索栏
 * 搜索输入控件
 */
class GhSearch @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val binding: LayoutGhSearchBinding
    private var searchStyle = 0

    val searchKey: String
        get() = binding.etContent.text?.toString() ?: ""

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

        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.GhSearch)
        val hint = typedArray.getString(R.styleable.GhSearch_search_hint)
        binding.etContent.setHint(hint)

        searchStyle = typedArray.getInt(R.styleable.GhSearch_search_style, 0)

        if (searchStyle == STYLE_REGULAR) {
            TextViewCompat.setTextAppearance(binding.etContent, R.style.TextBody2)
        } else {
            TextViewCompat.setTextAppearance(binding.etContent, R.style.TextBody3)
        }

        val case = typedArray.getInt(R.styleable.GhSearch_search_case, 0)
        if (case == CASE_REGULAR) {
            setBackgroundResource(R.drawable.bg_shape_ui_container_2_radius_999)
            binding.ivStart.imageTintList = ColorStateList.valueOf(R.color.text_instance.toColor(context))
            binding.etContent.setHintTextColor(R.color.text_instance.toColor(context))
            binding.etContent.setTextColor(R.color.text_primary.toColor(context))
            binding.ivEnd.imageTintList = ColorStateList.valueOf(R.color.text_instance.toColor(context))
        } else {
            setBackgroundResource(R.drawable.bg_shape_white_alpha_20_radius_999)
            binding.ivStart.imageTintList = ColorStateList.valueOf(R.color.text_aw_primary.toColor(context))
            binding.etContent.setHintTextColor(R.color.text_aw_tertiary.toColor(context))
            binding.etContent.setTextColor(R.color.text_aw_primary.toColor(context))
            binding.ivEnd.imageTintList = ColorStateList.valueOf(R.color.text_aw_primary.toColor(context))
        }

        binding.etContent.addTextChangedListener {
            val text = it?.toString() ?: ""
            binding.gEnd.goneIf(text.isEmpty())
        }

        binding.vEnd.setOnClickListener {
            clear()
        }

        typedArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val heightSize = if (searchStyle == STYLE_REGULAR) {
            32F.dip2px()
        } else {
            28F.dip2px()
        }
        val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, EXACTLY)
        super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
    }

    fun addTextChangedListener(after: (text: Editable?) -> Unit = {}) {
        binding.etContent.addTextChangedListener(afterTextChanged = after)
    }

    fun setEditTextOnFocusChangeListener(listener: OnFocusChangeListener) {
        binding.etContent.onFocusChangeListener = listener
    }

    fun clear() {
        binding.etContent.setText("")
    }

    companion object {
        private const val STYLE_REGULAR = 0
        private const val STYLE_SMALL = 1

        private const val CASE_REGULAR = 0
        private const val CASE_AW = 1
    }
}