package com.example.watch.ui.activity

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialog
import com.example.watch.R

class LoadingDialog @JvmOverloads constructor(builder: com.example.watch.ui.activity.LoadingDialog.Builder, context: Context? = builder.context, theme: Int = R.style.DialogStyle) : AppCompatDialog(context, theme) {
    private val mRoot: View
    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentView(mRoot)
    }

    class Builder private constructor(val context: Context) {
        var message: String? = null
        fun setMessage(message: String?): com.example.watch.ui.activity.LoadingDialog.Builder {
            this.message = message
            return this
        }

        fun build(): LoadingDialog {
            return LoadingDialog(this)
        }
    }

    companion object {
        fun Builder(activity: Activity?): com.example.watch.ui.activity.LoadingDialog.Builder {
            return com.example.watch.ui.activity.LoadingDialog.Builder(activity)
        }
    }

    init {
        mRoot = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null)
        val tvLoading = mRoot.findViewById<TextView>(R.id.tv_loading)
        if (!TextUtils.isEmpty(builder.message)) {
            tvLoading.visibility = View.VISIBLE
            tvLoading.setText(builder.message)
        } else {
            tvLoading.visibility = View.GONE
        }
        setCanceledOnTouchOutside(false)
        setCancelable(false)
    }
}