package com.xty.common.weight

import android.app.Dialog
import android.content.Context
import android.os.Handler
import android.os.Message
import android.widget.TextView
import com.xty.common.LogUtils
import com.xty.common.databinding.DialogLoadingBinding

class LoadingView(context: Context, themeResId: Int) : Dialog(context, themeResId) {
    constructor(context: Context):this(context,0)


    val binding by lazy{DialogLoadingBinding.inflate(layoutInflater)}

    /**
     * 设置超时消失
     *
     * @param time 毫秒
     * @return
     */
    fun show(time : Long) {
        mHandler.sendEmptyMessageDelayed(1, time)
        super.show()
    }

    private val mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            if (isShowing){
                dismiss()
            }
        }
    }

    class Builder(private val context: Context) {
        private var message=""
        private var isShowMessage = true
        private var isCancelable = false
        private var isCancelOutside = false

        /**
         * 设置提示信息
         *
         * @param message
         * @return
         */
        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        /**
         * 设置是否显示提示信息
         *
         * @param isShowMessage
         * @return
         */
        fun setShowMessage(isShowMessage: Boolean): Builder {
            this.isShowMessage = isShowMessage
            return this
        }


        /**
         * 设置是否能够按返回键取消
         *
         * @param isCancelable
         * @return
         */
        fun setCancelable(isCancelable: Boolean): Builder {
            this.isCancelable = isCancelable
            return this
        }

        /**
         * 设置是否能够取消
         *
         * @param isCancelOutside
         * @return
         */
        fun setCancelOutside(isCancelOutside: Boolean): Builder {
            this.isCancelOutside = isCancelOutside
            return this
        }
        fun cteated():LoadingView{
            val loadingDailog = LoadingView(context)
            val msgText: TextView =loadingDailog.binding.tipTextView
             if (message.isNotEmpty()) {
                 msgText.text = message
            }
            loadingDailog.setContentView(loadingDailog.binding.root)
            loadingDailog.setCancelable(isCancelable)
            loadingDailog.setCanceledOnTouchOutside(isCancelOutside)
            return loadingDailog
        }


    }



}