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.DialogSeatAuthBinding
import com.cheng.blzb.impl.TextWatcherImpl
import com.cheng.blzb.utils.KeyboardUtils
import com.example.base.extensions.getColor
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.utils.ScreenUtils
import com.example.base.utils.SpanUtils

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

    private lateinit var binding: DialogSeatAuthBinding

    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.INVISIBLE else View.VISIBLE
        }
    }

    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)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_seat_auth, null)

        binding = DialogSeatAuthBinding.bind(view)

        binding.tvTitle.typeface = Constants.douyinsansB

        SpanUtils.with(binding.tvTip)
            .append("注：授权者需提前到")
            .append("设置-绑定账号-手机号绑定")
            .setForegroundColor(getColor(R.color.color_ff8518))
            .append("才能正常授权")
            .create()

        binding.etPhone.addTextChangedListener(textWatcher)

        binding.ivClearText.onClick {
            binding.etPhone.setText("")
        }

        binding.ivClose.onClick { dismiss() }

        binding.btnNext.onClick {
            val phone = binding.etPhone.text.toString()
            if (TextUtils.isEmpty(phone)) {
                toast("请输入手机号")
                return@onClick
            }
            mOnBackListener?.invoke(phone)
            dismiss()
        }

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

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

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

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

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