package cn.yyxx.eyuangame.core.ui

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import cn.yyxx.support.ResUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/18
 */
class EventEditText : ConstraintLayout, View.OnFocusChangeListener {
    lateinit var focusView: View
        private set
    lateinit var leftImageView: ImageView
        private set
    lateinit var rightImageView: ImageView
        private set
    lateinit var editText: EditText
        private set

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(context)
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_event_edit_text", "layout"), this)

        focusView = findViewById(ResUtils.getResId(context, "yyxx_eet_focus_view", "id"))
        focusView.visibility = View.GONE

        leftImageView = findViewById(ResUtils.getResId(context, "yyxx_eet_iv_left", "id"))
        leftImageView.visibility = View.GONE

        editText = findViewById(ResUtils.getResId(context, "yyxx_eet_input", "id"))
        editText.onFocusChangeListener = this

        rightImageView = findViewById(ResUtils.getResId(context, "yyxx_eet_iv_right", "id"))
        rightImageView.visibility = View.GONE

    }

    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        if (hasFocus) {
            focusView.visibility = View.VISIBLE
        } else {
            focusView.visibility = View.GONE
        }
    }
}