package com.yidianling.medical.utils

import android.view.*
import android.widget.EditText
import android.widget.TextView
import java.lang.reflect.Field

class EditTextUtils {

    companion object{
        /**
         * 禁止输入框复制粘贴菜单
         */
        fun disableCopyAndPaste(editText: EditText?) {
            try {
                if (editText == null) {
                    return
                }
                editText.setOnLongClickListener(object : View.OnLongClickListener {
                    override fun onLongClick(v: View?): Boolean {
                        return true
                    }
                })
                editText.setLongClickable(false)
                editText.setOnTouchListener(object : View.OnTouchListener {
                    override fun onTouch(v: View?, event: MotionEvent): Boolean {
                        if (event.getAction() === MotionEvent.ACTION_DOWN) {
                            // setInsertionDisabled when user touches the view
                            setInsertionDisabled(editText)
                        }
                        return false
                    }
                })
                editText.setCustomSelectionActionModeCallback(object : ActionMode.Callback {
                    override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                        return false
                    }

                    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                        return false
                    }

                    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
                        return false
                    }

                    override fun onDestroyActionMode(mode: ActionMode?) {}
                })
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        private fun setInsertionDisabled(editText: EditText) {
            try {
                val editorField: Field = TextView::class.java.getDeclaredField("mEditor")
                editorField.setAccessible(true)
                val editorObject: Any = editorField.get(editText)

                // if this view supports insertion handles
                val editorClass = Class.forName("android.widget.Editor")
                val mInsertionControllerEnabledField: Field =
                    editorClass.getDeclaredField("mInsertionControllerEnabled")
                mInsertionControllerEnabledField.setAccessible(true)
                mInsertionControllerEnabledField.set(editorObject, false)

                // if this view supports selection handles
                val mSelectionControllerEnabledField: Field =
                    editorClass.getDeclaredField("mSelectionControllerEnabled")
                mSelectionControllerEnabledField.setAccessible(true)
                mSelectionControllerEnabledField.set(editorObject, false)
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }
    }

}