package com.mobiuz.app.dev.ui.global

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import com.mobiuz.app.R
import com.mobiuz.app.databinding.LayoutSenderBinding
import com.mobiuz.app.dev.utils.extensions.SingleBlock

class SenderDialog(
    private val txtDescription: String,
    private val status: Boolean
) : DialogFragment() {

    private var listener: SingleBlock<Unit>? = null
    private var _bn: LayoutSenderBinding? = null
    private val bn get() = _bn!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.CustomDialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _bn = LayoutSenderBinding.inflate(inflater, container, false)
        return bn.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isCancelable = false
        bn.apply {
            title.text = txtDescription
            image.isVisible = status
            if (!status) {
                btnOk.text = getString(R.string.login_register)
            }
            btnOk.setOnClickListener {
                listener?.invoke(Unit)
                dismiss()
            }
        }
    }

    fun setOnClickListener(block: SingleBlock<Unit>) {
        listener = block
    }

    override fun onDestroy() {
        super.onDestroy()
        _bn = null
    }
}
