package com.adealink.weparty.im.session.dialog

import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.isSuccess
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_NOTE_NAME_MAX_LENGTH
import com.adealink.weparty.im.R
import com.adealink.weparty.im.databinding.DialogEditNoteBinding
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserNote
import com.adealink.weparty.util.QMUIKeyboardHelper

class EditNoteDialog : KeyboardBottomDialogFragment(R.layout.dialog_edit_note) {

    private val binding by viewBinding(DialogEditNoteBinding::bind)

    private val profileViewModel by fastLazy { ProfileModule.getProfileViewModel(requireActivity()) }

    private var hint: String? = null
    private var userNote: UserNote? = null

    fun setUserHint(hint: String?) {
        this.hint = hint
    }

    fun setUserNote(note: UserNote?) {
        this.userNote = note
    }

    override fun initViews() {
        super.initViews()
        binding.etInput.hint = hint
        binding.etInput.setMaxLength(PROFILE_NOTE_NAME_MAX_LENGTH)
        binding.etInput.setText(userNote?.note)
        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
            }
        })
        binding.etInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {
            }

            override fun onTextChanged(
                s: CharSequence?,
                start: Int,
                before: Int,
                count: Int
            ) {
            }

            override fun afterTextChanged(s: Editable?) {
                val length = s?.length ?: 0
                binding.tvLimit.text = "${length}/${PROFILE_NOTE_NAME_MAX_LENGTH}"
            }
        })
        binding.tvLimit.text =
            "${binding.etInput.text?.length ?: 0}/${PROFILE_NOTE_NAME_MAX_LENGTH}"
        binding.btnSave.onClick {
            save()
        }

        QMUIKeyboardHelper.showKeyboard(binding.etInput, true)
        binding.etInput.requestFocus()
    }

    private fun save() {
        val userNote = userNote ?: return
        val noteName = binding.etInput.text?.toString()?.trim()
        showLoading()
        profileViewModel?.updateUserNote(userNote.uid, noteName)
            ?.observe(viewLifecycleOwner) { rlt ->
                dismissLoading()
                showToast(rlt)
                if (rlt.isSuccess) {
                    dismiss()
                }
            }
    }

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

}