package com.rc.core.ui.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.*
import com.rc.core.R
import com.rc.core.databinding.DialogLoadingBinding

/**
 * info
 *
 * @author ReiChin_
 */
class LoadingDialog(context: Context, private val message: String? = null) :
    Dialog(context, R.style.LoadingDialog) {

    fun setMessage(message: String?) {
        viewBinding.message.visibility = if (message.isNullOrEmpty()) View.GONE else View.VISIBLE
        viewBinding.message.text = message
    }

    private val viewBinding: DialogLoadingBinding by lazy {
        DialogLoadingBinding.inflate(LayoutInflater.from(context))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(viewBinding.root)

        setMessage(message)
    }

}

//class LoadingDialog : DialogFragment() {
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setStyle(STYLE_NO_TITLE, R.style.LoadingDialog)
//    }
//
//    private lateinit var mViewBinding: DialogLoadingBinding
//
//    override fun onCreateView(
//        inflater: LayoutInflater,
//        container: ViewGroup?,
//        savedInstanceState: Bundle?
//    ): View {
//        mViewBinding = DialogLoadingBinding.inflate(inflater, container, false)
//        return mViewBinding.root
//    }
//
//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        super.onViewCreated(view, savedInstanceState)
//
//        mViewBinding.message.visibility = if (mMessage.isNullOrEmpty()) View.GONE else View.VISIBLE
//        mViewBinding.message.text = mMessage
//    }
//
//    override fun onStart() {
//        super.onStart()
//        initWindowConfig()
//    }
//
//    private fun initWindowConfig() {
//        dialog?.window?.apply {
//            setWindowAnimations(R.style.PopWindowAnimStyle)
//            setGravity(Gravity.CENTER)
//            attributes.width = WindowManager.LayoutParams.MATCH_PARENT
//            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT
//        }
//    }
//
//    private var mMessage: CharSequence? = null
//
//    fun setMessage(message: CharSequence?): LoadingDialog {
//        this.mMessage = message
//        return this
//    }
//
//    private var addTag = false
//
//    fun show(manager: FragmentManager): LoadingDialog {
//        if (addTag) return this
//        addTag = true
//
//        if (this.isAdded || null != manager.findFragmentByTag(LoadingDialog::class.simpleName)) {
//            manager.beginTransaction().remove(this).commit()
//        }
//        show(manager, LoadingDialog::class.simpleName)
//
//        Handler().post {
//            addTag = false
//        }
//
//        return this
//    }
//
//}