package com.pan_american.android.util

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.pan_american.android.OASystem
import com.pan_american.android.R

class ScrollEditText(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    private val view: View

    private val editTextField: EditText

    private val textField: TextView

    private var isClick = true

    private var isEnabled = true

    init {
        view = LayoutInflater.from(context).inflate(R.layout.layout_scroll_edit_text, this)
        editTextField = view.findViewById(R.id.edit_text_field)
        editTextField.setSelection(editTextField.text.length)
        textField = view.findViewById(R.id.text_field)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                if (isEnabled) {
                    if (editTextField.isFocused) {
                        if (editTextField.height > this.height) {
                            parent.requestDisallowInterceptTouchEvent(true)
                        } else {
                            parent.requestDisallowInterceptTouchEvent(false)
                        }
                        isClick = true
                    }

                } else {
                    if (textField.height > this.height) {
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                }
            }

            MotionEvent.ACTION_MOVE -> {
                if (isEnabled) {
                    if (editTextField.height > this.height) {
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                } else {
                    if (textField.height > this.height) {
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                }

                isClick = false
            }

            MotionEvent.ACTION_UP -> {
                if (isEnabled) {
                    if (isClick) {
                        editTextField.isFocusable = true
                        editTextField.isFocusableInTouchMode = true
                        editTextField.requestFocus()

                        val inputMethodManager =
                            OASystem.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                        inputMethodManager.showSoftInput(
                            editTextField, InputMethodManager.SHOW_IMPLICIT
                        )
                    } else {
                        parent.parent.requestDisallowInterceptTouchEvent(false)
                    }
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    fun setText(remark: String) {
        if (isEnabled) {
            editTextField.setText(remark)
        } else {
            textField.text = remark
        }
    }

    fun getText(): String = editTextField.text.toString()

    fun setInputType(inputType: Int) {
        editTextField.inputType = inputType
    }

    fun setSingleLine(flag: Boolean) {
        editTextField.isSingleLine = flag
    }

    fun isEnabled(flag: Boolean) {

        isEnabled = flag

        if (!isEnabled) {

            if (editTextField.text.isNotBlank()) {
                val content = editTextField.text.toString()
                textField.text = content
            }

            editTextField.visibility = View.GONE
            textField.visibility = View.VISIBLE
        }
    }

    fun addTextChangedListener(
        beforeChanged: () -> Unit, onChanged: () -> Unit, afterChanged: () -> Unit
    ) {
        editTextField.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                beforeChanged()
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                onChanged()
            }

            override fun afterTextChanged(p0: Editable?) {
                afterChanged()
            }

        })
    }
}