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.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.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogAddKeywordBinding
import com.cheng.blzb.impl.TextWatcherImpl
import com.cheng.blzb.utils.KeyboardUtils
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.utils.ScreenUtils

class AddKeywordDialog : DialogFragment(), KeyboardUtils.OnSoftInputChangedListener {
    private var mOnBackListener: ((String) -> Unit)? = null     //回调事件

    lateinit var binding: DialogAddKeywordBinding

    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))
        setCancelable(false)
        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_add_keyword, null)

        binding = DialogAddKeywordBinding.bind(view)

        binding.tvTitle.typeface = Constants.douyinsansB

        binding.etKeyword.addTextChangedListener(textWatcher)

        binding.ivClose.onClick {
            dismiss()
        }
        binding.ivClearText.onClick {
            binding.etKeyword.setText("")
        }
        binding.btnNext.onClick {
            val keyword = binding.etKeyword.text.toString().trim()
            if (TextUtils.isEmpty(keyword)) {
                toast("请输入关键词")
                return@onClick
            }
            mOnBackListener?.invoke(keyword)
            dismiss()
        }

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

    fun setOnBackListener(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.etKeyword.removeTextChangedListener(textWatcher)
        super.onDestroyView()
    }

    companion object {
        fun newInstance(): AddKeywordDialog {
            val arg = Bundle()
            val fragment = AddKeywordDialog()
            fragment.arguments = arg
            return fragment
        }
    }
}