package com.kaixed.kchat.view.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.kaixed.kchat.databinding.ActivityTestBinding
import com.kaixed.kchat.utils.Constants
import com.tencent.mmkv.MMKV

class TestActivity : AppCompatActivity() {

    private lateinit var binding: ActivityTestBinding
    private lateinit var inputMethodManager: InputMethodManager

    var keyboard: Int = MMKV.mmkvWithID(Constants.MMKV_COMMON_DATA)
        .getInt("keyboardHeight", 200)


    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTestBinding.inflate(layoutInflater)
        enableEdgeToEdge()
        setContentView(binding.root)

        inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager

        // 处理 EditText 的触摸事件
        binding.etInput.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_UP && binding.clFunctionPanel.isShown) {
                lockContentViewHeight()
                hideEmojiPanel(true)
                unlockContentViewHeight()
            }
            false
        }

        // 处理 RecyclerView 的触摸事件
//        binding.recycleChatList.setOnTouchListener { _, motionEvent ->
//            if (motionEvent.action == MotionEvent.ACTION_UP) {
//                if (binding.clFunctionPanel.isShown) {
//                    hideEmojiPanel(false)
//                } else if (isSoftKeyboardShown()) {
//                    hideSoftKeyboard()
//                }
//            }
//            false
//        }

        // 表情面板切换按钮点击事件
        binding.ivEmoji.setOnClickListener {
            if (binding.clFunctionPanel.isShown) {
                lockContentViewHeight()
                hideEmojiPanel(true)
                unlockContentViewHeight()
            } else {
                if (isSoftKeyboardShown()) {
                    lockContentViewHeight()
                    showEmojiPanel()
                    unlockContentViewHeight()
                } else {
                    showEmojiPanel()
                }
            }
        }
    }

    private fun lockContentViewHeight() {
        val layoutParams = binding.recycleChatList.layoutParams as LinearLayout.LayoutParams
        layoutParams.height = binding.recycleChatList.height
        layoutParams.weight = 0f
        binding.recycleChatList.layoutParams = layoutParams
    }

    private fun unlockContentViewHeight() {
        binding.recycleChatList.postDelayed({
            val layoutParams = binding.recycleChatList.layoutParams as LinearLayout.LayoutParams
            layoutParams.weight = 1f
            binding.recycleChatList.layoutParams = layoutParams
        }, 200)
    }

    private fun getSoftKeyboardHeight(): Int {
        return keyboard
    }

    private fun getSoftKeyboardHeightLocalValue(): Int {
        return keyboard
    }

    private fun isSoftKeyboardShown(): Boolean {
        return getSoftKeyboardHeight() != 0
    }

    private fun showSoftKeyboard(saveSoftKeyboardHeight: Boolean) {
        binding.etInput.requestFocus()
        inputMethodManager.showSoftInput(binding.etInput, 0)
        if (saveSoftKeyboardHeight) {
            binding.etInput.postDelayed({
                getSoftKeyboardHeight()
            }, 200)
        }
    }

    private fun hideSoftKeyboard() {
        inputMethodManager.hideSoftInputFromWindow(binding.etInput.windowToken, 0)
    }

    private fun showEmojiPanel() {
        var softKeyboardHeight = getSoftKeyboardHeight()
        if (softKeyboardHeight == 0) {
            softKeyboardHeight = getSoftKeyboardHeightLocalValue()
        } else {
            hideSoftKeyboard()
        }
        binding.clFunctionPanel.layoutParams.height = softKeyboardHeight
        binding.clFunctionPanel.visibility = View.VISIBLE
    }

    private fun hideEmojiPanel(showSoftKeyboard: Boolean) {
        if (binding.clFunctionPanel.isShown) {
            binding.clFunctionPanel.visibility = View.GONE
            if (showSoftKeyboard) {
                showSoftKeyboard(false)
            }
        }
    }

    override fun onBackPressed() {
        if (binding.clFunctionPanel.isShown) {
            hideEmojiPanel(false)
        } else {
            super.onBackPressed()
        }
    }
}
