package com.adealink.weparty.profile.dynamic.fragment.dialog

import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.activityViewModels
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
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.DYNAMIC_COMMENT_MAX_LENGTH
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogDynamicEditTalkBinding
import com.adealink.weparty.profile.dynamic.viewmodel.DynamicViewModel
import com.adealink.weparty.util.QMUIKeyboardHelper
import com.adealink.weparty.R as APP_R

class DynamicTalkDialog : KeyboardBottomDialogFragment(R.layout.dialog_dynamic_edit_talk) {

    private val binding by viewBinding(DialogDynamicEditTalkBinding::bind)

    private val dynamicViewModel by activityViewModels<DynamicViewModel>()

    private var dynamicId: String? = null

    fun setDynamicId(id: String) {
        dynamicId = id
    }

    override fun initViews() {
        super.initViews()
        binding.etInput.setMaxLength(DYNAMIC_COMMENT_MAX_LENGTH)
        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 input = s?.trim()?.toString()
                binding.btnSend.isEnabled = !input.isNullOrEmpty()

                if (binding.etInput.lineCount > 1) {
                    binding.clInput.setBackgroundResource(APP_R.drawable.common_input_edit_bg)
                } else {
                    binding.clInput.setBackgroundResource(APP_R.drawable.common_input_single_edit_bg)
                }
            }
        })
        binding.btnSend.isEnabled = false
        binding.btnSend.onClick {
            clickSend()
        }

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

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

    private fun clickSend() {
        val dynamicId = dynamicId ?: return
        showLoading()
        dynamicViewModel.comment(dynamicId, binding.etInput.text?.toString() ?: "")
            .observe(viewLifecycleOwner) { rlt ->
                dismissLoading()
                showToast(rlt)
                when (rlt) {
                    is Rlt.Failed -> {
                        //Ntd.
                    }

                    is Rlt.Success<*> -> {
                        dismiss()
                    }
                }
            }
    }

}