package com.xty.common.util

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import java.lang.ref.WeakReference




class EditTextMonitor {
    private var mOnEditTextMonitor: OnEditTextMonitor? = null
    private val mEditTexts: MutableList<EditText> = ArrayList()

    /**
     * 设置需要监听的EditTexts，只能添加一次
     *
     * @param edits EditTexts
     */
    fun setEditTexts(vararg edits: EditText) {
        if (!isSetEditText) {
            mEditTexts.addAll(listOf(*edits))
        }
    }

    /**
     * 开启监听
     */
    fun startMonitor() {
        for (editText in mEditTexts) {
            if (editText.tag == null || editText.tag !is OnInputListener) {
                val onInputListener = OnInputListener(editText)
                editText.addTextChangedListener(onInputListener)
                editText.tag = onInputListener
            }
        }
    }

    /**
     * 关闭监听
     */
    fun stopMonitor() {
        for (editText in mEditTexts) {
            if (editText.tag != null && editText.tag is OnInputListener) {
                val measuresListener = editText.tag as OnInputListener
                editText.removeTextChangedListener(measuresListener)
                editText.tag = null
            }
        }
    }

    /**
     * 是否已设置了EditText
     *
     * @return boolean
     */
    val isSetEditText: Boolean
        get() = mEditTexts.size > 0

    /**
     * 主动清除内存
     */
    fun clear() {
        stopMonitor()
        mEditTexts.clear()
    }

    /**
     * 设置监听
     *
     * @param onEditTextMonitor OnEditTextMonitor
     */
    fun setEditTextListener(onEditTextMonitor: OnEditTextMonitor?) {
        mOnEditTextMonitor = onEditTextMonitor
    }

    interface OnEditTextMonitor {
        /**
         * 达成监听条件
         *
         * @param editText 监听完成的EditText
         */
        fun monitor(editText: EditText?)
    }

    private inner class OnInputListener(editText: EditText?) :
        TextWatcher {
        private val mInputHandler: InputHandler
        private val MSG_TAG = 0x520

        init {
            mInputHandler = InputHandler(Looper.getMainLooper(), editText)
        }

        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
        override fun afterTextChanged(s: Editable) {
            if (mInputHandler.hasMessages(MSG_TAG)) {
                mInputHandler.removeMessages(MSG_TAG)
            }
            val msg = Message()
            msg.what = MSG_TAG
            mInputHandler.sendMessageDelayed(msg, WAIT_TIME.toLong())
        }
    }

    private inner class InputHandler(looper: Looper, editText: EditText?) :
        Handler(looper) {
        private val mEditText: WeakReference<EditText?>

        init {
            mEditText = WeakReference(editText)
        }

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (mOnEditTextMonitor != null) {
                mOnEditTextMonitor!!.monitor(mEditText.get())
            }
        }
    }

    companion object {
        /**
         * 输入过程中，停顿时长，一旦达到该时长的停顿则回调监听EditText事件
         */
        const val WAIT_TIME = 600
    }
}