package com.cheng.blzb.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.InputType
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.DialogFragment
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.utils.ScreenUtils
import com.cheng.blzb.R
import com.cheng.blzb.databinding.DialogEditTextBinding
import com.cheng.blzb.impl.TextWatcherImpl
import com.cheng.blzb.utils.KeyboardUtils

class EditTextDialog : DialogFragment(), KeyboardUtils.OnSoftInputChangedListener {
    private val title by lazy { arguments?.getString("title") ?: "" }
    private val content by lazy { arguments?.getString("content") ?: "" }
    private val hint by lazy { arguments?.getString("hint") }
    private val inputType by lazy { arguments?.getInt("input_type") ?: InputType.TYPE_CLASS_TEXT }
    private var mOnBackListener: ((String) -> Unit)? = null     //回调事件

    lateinit var binding: DialogEditTextBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = ScreenUtils.getWindowSize().x
        windowParams?.gravity = Gravity.BOTTOM
        windowParams?.windowAnimations = R.style.dialog_bottom
        dialog?.window?.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        KeyboardUtils.registerSoftInputChangedListener(requireActivity(), this)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    private val textWatcher = object : TextWatcherImpl() {
        @SuppressLint("SetTextI18n")
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            binding.ivClearText.visibility = if (TextUtils.isEmpty(s)) View.GONE else View.VISIBLE
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_edit_text, null)

        binding = DialogEditTextBinding.bind(view)

        binding.etContent.addTextChangedListener(textWatcher)

        binding.tvTitle.text = title
        binding.etContent.hint = hint ?: "请输入"
        binding.etContent.setText(content)
        binding.etContent.inputType = inputType

        binding.ivClose.onClick {
            dismiss()
        }
        binding.ivClearText.onClick {
            binding.etContent.setText("")
        }
        binding.btnNext.onClick {
            val content = binding.etContent.text?.trim().toString()
            if (TextUtils.isEmpty(content)) {
                toast(hint ?: "请输入内容")
                return@onClick
            }
            mOnBackListener?.invoke(content)
            dismiss()
        }

        val dialog = Dialog(requireContext())
        dialog.setContentView(view)
        return dialog
    }

    fun setOnTextListener(listener: ((String) -> Unit)) {
        mOnBackListener = listener
    }

    override fun onSoftInputChanged(height: Int) {
        val lp = binding.layoutContent.layoutParams as ConstraintLayout.LayoutParams
        lp.bottomMargin = height
        binding.layoutContent.layoutParams = lp
    }

    override fun onDestroyView() {
        KeyboardUtils.unregisterSoftInputChangedListener(requireActivity().window)
        binding.etContent.removeTextChangedListener(textWatcher)
        super.onDestroyView()
    }

    companion object {
        fun newInstance(
            title: String? = null,
            content: String? = null,
            hint: String? = null,
            inputType: Int = InputType.TYPE_CLASS_TEXT
        ): EditTextDialog {
            val arg = Bundle()
            val fragment = EditTextDialog()
            arg.putString("title", title)
            arg.putString("content", content)
            arg.putString("hint", hint)
            arg.putInt("input_type", inputType)
            fragment.arguments = arg
            return fragment
        }
    }
}