package com.adealink.weparty.profile.edit.dialog

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.PROFILE_NICKNAME_MAX_LENGTH
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogEditNicknameBinding
import com.adealink.weparty.util.QMUIKeyboardHelper

class EditNickNameDialog : KeyboardBottomDialogFragment(R.layout.dialog_edit_nickname) {

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

    private var nickName: String? = null

    fun setNickName(name: String?) {
        this.nickName = name
    }

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

    override fun initViews() {
        super.initViews()
        binding.tvTips.text = getCompatString(
            R.string.profile_edit_person_nickname_limit,
            PROFILE_NICKNAME_MAX_LENGTH
        )
        binding.etInput.setMaxLength(PROFILE_NICKNAME_MAX_LENGTH)
        binding.etInput.setText(nickName)
        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.profile_edit_nickname_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)
    }
}