package com.adealink.weparty.room.create

import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.text.filter.addNoWhitespaceInputFilter
import com.adealink.weparty.commonui.text.setMaxLength
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.KeyboardBottomDialogFragment
import com.adealink.weparty.constant.ROOM_NAME_MAX_LENGTH
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.DialogEditRoomNameBinding
import com.adealink.weparty.util.QMUIKeyboardHelper

class EditRoomNameDialog : KeyboardBottomDialogFragment(R.layout.dialog_edit_room_name) {

    private val binding by viewBinding(DialogEditRoomNameBinding::bind)
    private var callback: IEditNameCallback? = null

    private var roomName: String? = null

    fun setRoomName(name: String?) {
        this.roomName = name
    }

    fun setCallback(callback: IEditNameCallback?) {
        this.callback = callback
    }

    override fun initViews() {
        super.initViews()
        binding.tvTips.text = getCompatString(
            R.string.room_edit_room_name_limit,
            ROOM_NAME_MAX_LENGTH
        )
        binding.etInput.setMaxLength(ROOM_NAME_MAX_LENGTH)
        binding.etInput.setText(roomName)
        binding.btnSave.onClick {
            save()
        }

        binding.etInput.addNoWhitespaceInputFilter()
        binding.etInput.imeOptions = EditorInfo.IME_ACTION_DONE
        binding.etInput.setOnEditorActionListener(object : TextView.OnEditorActionListener {
            override fun onEditorAction(
                v: TextView?,
                actionId: Int,
                event: KeyEvent?
            ): Boolean {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    binding.etInput.clearFocus()
                    QMUIKeyboardHelper.hideKeyboard(binding.etInput)
                    return true
                }
                return false
            }
        })
        QMUIKeyboardHelper.showKeyboard(binding.etInput, true)
        binding.etInput.requestFocus()
    }

    private fun save() {
        val nickName = binding.etInput.text?.toString()?.trim()
        if (nickName.isNullOrEmpty()) {
            showToast(R.string.room_edit_room_name_empty)
            return
        }
        callback?.onEditName(nickName)
        dismiss()
    }

//    override fun onKeyboardChange(isShow: Boolean, keyboardHeight: Int) {
//        if (isShow) {
//            binding.btnSave.gone()
//        } else {
//            binding.btnSave.show()
//        }
//    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.etInput.clearFocus()
        QMUIKeyboardHelper.hideKeyboard(binding.etInput)
    }


    interface IEditNameCallback {
        fun onEditName(name: String)
    }
}